Example usage for org.apache.poi.xslf.usermodel XSLFTextRun setBold

List of usage examples for org.apache.poi.xslf.usermodel XSLFTextRun setBold

Introduction

In this page you can find the example usage for org.apache.poi.xslf.usermodel XSLFTextRun setBold.

Prototype

@Override
    public void setBold(boolean bold) 

Source Link

Usage

From source file:com.hp.autonomy.frontend.reports.powerpoint.PowerPointServiceImpl.java

License:MIT License

/**
 * Internal implementation to add a topic map to a slide.
 * @param slide the slide to add to.//from w ww  .ja  v a2 s .co  m
 * @param anchor bounding rectangle to draw onto, in PowerPoint coordinates.
 * @param data the topic map data.
 */
private static void addTopicMap(final XSLFSlide slide, final Rectangle2D.Double anchor,
        final TopicMapData data) {
    for (final TopicMapData.Path reqPath : data.getPaths()) {
        final XSLFFreeformShape shape = slide.createFreeform();
        final Path2D.Double path = new Path2D.Double();

        boolean first = true;

        for (double[] point : reqPath.getPoints()) {
            final double x = point[0] * anchor.getWidth() + anchor.getMinX();
            final double y = point[1] * anchor.getHeight() + anchor.getMinY();
            if (first) {
                path.moveTo(x, y);
                first = false;
            } else {
                path.lineTo(x, y);
            }
        }
        path.closePath();

        shape.setPath(path);
        shape.setStrokeStyle(2);
        shape.setLineColor(Color.GRAY);
        shape.setHorizontalCentered(true);
        shape.setVerticalAlignment(VerticalAlignment.MIDDLE);
        shape.setTextAutofit(TextShape.TextAutofit.NORMAL);

        final XSLFTextParagraph text = shape.addNewTextParagraph();
        final XSLFTextRun textRun = text.addNewTextRun();
        textRun.setText(reqPath.name);
        textRun.setFontColor(Color.WHITE);
        textRun.setBold(true);

        final CTShape cs = (CTShape) shape.getXmlObject();

        double max = 100, min = 1, scale = 100;
        final CTTextNormalAutofit autoFit = cs.getTxBody().getBodyPr().getNormAutofit();
        final double availHeight = path.getBounds2D().getHeight();
        final int RESIZE_ATTEMPTS = 7;

        for (int attempts = 0; attempts < RESIZE_ATTEMPTS; ++attempts) {
            // PowerPoint doesn't resize the text till you edit it once, which means the text initially looks too
            //   large when you first view the slide; so we binary-chop to get a sensible initial approximation.
            // OpenOffice does the text resize on load so it doesn't have this problem.
            autoFit.setFontScale(Math.max(1, (int) (scale * 1000)));

            final double textHeight = shape.getTextHeight();
            if (textHeight < availHeight) {
                min = scale;
                scale = 0.5 * (min + max);
            } else if (textHeight > availHeight) {
                max = scale;
                scale = 0.5 * (min + max);
            } else {
                break;
            }
        }

        final int opacity = (int) (100000 * reqPath.getOpacity());
        final Color c1 = Color.decode(reqPath.getColor());
        final Color c2 = Color.decode(reqPath.getColor2());

        final CTGradientFillProperties gFill = cs.getSpPr().addNewGradFill();
        gFill.addNewLin().setAng(3300000);
        final CTGradientStopList list = gFill.addNewGsLst();

        final CTGradientStop stop1 = list.addNewGs();
        stop1.setPos(0);
        final CTSRgbColor color1 = stop1.addNewSrgbClr();
        color1.setVal(new byte[] { (byte) c1.getRed(), (byte) c1.getGreen(), (byte) c1.getBlue() });
        color1.addNewAlpha().setVal(opacity);

        final CTGradientStop stop2 = list.addNewGs();
        stop2.setPos(100000);
        final CTSRgbColor color2 = stop2.addNewSrgbClr();
        color2.setVal(new byte[] { (byte) c2.getRed(), (byte) c2.getGreen(), (byte) c2.getBlue() });
        color2.addNewAlpha().setVal(opacity);

        if (reqPath.level > 0) {
            // The nodes which aren't leaf nodes can be clicked on to hide them so you can see the nodes underneath.
            // This only works in PowerPoint; OpenOffice doesn't seem to support it. OpenOffice has its own syntax
            //   to do something similar, but we don't use it since PowerPoint treats it as corrupt.
            final String shapeId = Integer.toString(shape.getShapeId());
            final CTSlide slXML = slide.getXmlObject();

            final CTTimeNodeList childTnLst;
            final CTBuildList bldLst;
            if (!slXML.isSetTiming()) {
                final CTSlideTiming timing = slXML.addNewTiming();
                final CTTLCommonTimeNodeData ctn = timing.addNewTnLst().addNewPar().addNewCTn();
                ctn.setDur("indefinite");
                ctn.setRestart(STTLTimeNodeRestartTypeImpl.NEVER);
                ctn.setNodeType(STTLTimeNodeType.TM_ROOT);
                childTnLst = ctn.addNewChildTnLst();
                bldLst = timing.addNewBldLst();
            } else {
                final CTSlideTiming timing = slXML.getTiming();
                childTnLst = timing.getTnLst().getParArray(0).getCTn().getChildTnLst();
                bldLst = timing.getBldLst();
            }

            final CTTLTimeNodeSequence seq = childTnLst.addNewSeq();
            seq.setConcurrent(true);
            seq.setNextAc(STTLNextActionType.SEEK);
            final CTTLCommonTimeNodeData common = seq.addNewCTn();

            common.setRestart(STTLTimeNodeRestartType.WHEN_NOT_ACTIVE);
            common.setFill(STTLTimeNodeFillType.HOLD);
            common.setEvtFilter("cancelBubble");
            common.setNodeType(STTLTimeNodeType.INTERACTIVE_SEQ);

            final CTTLTimeConditionList condList = common.addNewStCondLst();
            final CTTLTimeCondition cond = condList.addNewCond();
            cond.setEvt(STTLTriggerEvent.ON_CLICK);
            cond.setDelay(0);
            cond.addNewTgtEl().addNewSpTgt().setSpid(shapeId);

            final CTTLTimeCondition endSync = common.addNewEndSync();
            endSync.setEvt(STTLTriggerEvent.END);
            endSync.setDelay(0);
            endSync.addNewRtn().setVal(STTLTriggerRuntimeNode.ALL);

            // These holdCtn* 'hold' transitions with zero delay might seem redundant; but they're exported in the
            //   PowerPoint XML, and the online PowerPoint Office365 viewer won't support the click animations
            //   unless they're present (e.g. from the 'Start Slide Show' button in the browser).
            final CTTLCommonTimeNodeData holdCtn1 = common.addNewChildTnLst().addNewPar().addNewCTn();

            holdCtn1.setFill(STTLTimeNodeFillType.HOLD);
            holdCtn1.addNewStCondLst().addNewCond().setDelay(0);

            final CTTLCommonTimeNodeData holdCtn2 = holdCtn1.addNewChildTnLst().addNewPar().addNewCTn();

            holdCtn2.setFill(STTLTimeNodeFillType.HOLD);
            holdCtn2.addNewStCondLst().addNewCond().setDelay(0);

            final CTTLCommonTimeNodeData clickCtn = holdCtn2.addNewChildTnLst().addNewPar().addNewCTn();

            clickCtn.setPresetID(10);
            clickCtn.setPresetClass(STTLTimeNodePresetClassType.EXIT);
            clickCtn.setPresetSubtype(0);
            clickCtn.setFill(STTLTimeNodeFillType.HOLD);
            clickCtn.setGrpId(0);
            clickCtn.setNodeType(STTLTimeNodeType.CLICK_EFFECT);

            clickCtn.addNewStCondLst().addNewCond().setDelay(0);

            final CTTimeNodeList clickChildTnList = clickCtn.addNewChildTnLst();

            final CTTLAnimateEffectBehavior animEffect = clickChildTnList.addNewAnimEffect();
            animEffect.setTransition(STTLAnimateEffectTransition.OUT);
            animEffect.setFilter("fade");
            final CTTLCommonBehaviorData cBhvr = animEffect.addNewCBhvr();
            final CTTLCommonTimeNodeData bhvrCtn = cBhvr.addNewCTn();

            bhvrCtn.setDur(500);
            cBhvr.addNewTgtEl().addNewSpTgt().setSpid(shapeId);

            final CTTLSetBehavior clickSet = clickChildTnList.addNewSet();
            final CTTLCommonBehaviorData clickSetBhvr = clickSet.addNewCBhvr();
            final CTTLCommonTimeNodeData hideCtn = clickSetBhvr.addNewCTn();

            hideCtn.setDur(1);
            hideCtn.setFill(STTLTimeNodeFillType.HOLD);
            hideCtn.addNewStCondLst().addNewCond().setDelay(499);

            clickSetBhvr.addNewTgtEl().addNewSpTgt().setSpid(shapeId);
            clickSetBhvr.addNewAttrNameLst().addAttrName("style.visibility");
            clickSet.addNewTo().addNewStrVal().setVal("hidden");

            final CTTLBuildParagraph bldP = bldLst.addNewBldP();
            bldP.setSpid(shapeId);
            bldP.setGrpId(0);
            bldP.setAnimBg(true);
        }
    }
}

From source file:com.hp.autonomy.frontend.reports.powerpoint.PowerPointServiceImpl.java

License:MIT License

/**
 * Utility function to render a TextData object as multiple text runs on the screen in a single text paragraph.
 * Note that newlines are not added automatically; this is so we can support adjacent text with different formatting.
 * @param slide the slide to add to.// w w w  .j av  a2  s.co m
 * @param anchor bounding rectangle to draw onto, in PowerPoint coordinates.
 * @param data the text data to render.
 */
private void addTextData(final XSLFSlide slide, final Rectangle2D.Double anchor, final TextData data) {
    final XSLFTextBox textBox = slide.createTextBox();
    textBox.setAnchor(anchor);
    textBox.clearText();

    final XSLFTextParagraph para = textBox.addNewTextParagraph();

    for (final TextData.Paragraph runData : data.getText()) {
        final XSLFTextRun run = para.addNewTextRun();
        run.setText(runData.getText());
        run.setFontSize(runData.getFontSize());
        run.setBold(runData.isBold());
        run.setItalic(runData.isItalic());
        run.setFontColor(Color.decode(runData.getColor()));

        if (textBox.getTextHeight() > anchor.getHeight()) {
            // Try removing words from the last box until we find something that fits, or we run out of words
            final String trimmedText = runData.getText().trim();
            run.setText(trimmedText);

            for (final StringBuilder text = new StringBuilder(trimmedText); textBox.getTextHeight() > anchor
                    .getHeight() && text.length() > 0;) {
                final int lastSpaceIdx = Math.max(text.lastIndexOf(" "), text.lastIndexOf("\n"));

                if (lastSpaceIdx < 0) {
                    break;
                }

                text.delete(lastSpaceIdx, text.length());
                // Add a trailing ellipsis unless it's empty or already contained a trailing ellipsis or "..." at the final truncated position.
                run.setText(
                        text.length() > 0 ? text.toString().replaceFirst("(\\s*(\\.{3}|\u2026))?$", "\u2026")
                                : "");
            }

            // The font metrics aren't going to be perfect (due to unavailability of fonts etc.) so we force the truncated text to fit.
            textBox.setTextAutofit(TextShape.TextAutofit.NORMAL);

            break;
        }
    }
}

From source file:info.opencards.util.playground.SlidesAndShapes.java

License:Apache License

public static void main(String[] args) throws Exception {
    XMLSlideShow ppt = new XMLSlideShow();
    ppt.setPageSize(new Dimension(792, 612));

    XSLFSlide slide1 = ppt.createSlide();
    XSLFTextBox textBox = slide1.createTextBox();
    XSLFTextRun r1 = textBox.addNewTextParagraph().addNewTextRun();
    r1.setBold(true);
    r1.setItalic(true);/*  w  w w. j ava 2  s.co m*/
    r1.setFontColor(Color.yellow);
    r1.setFontFamily("Arial");
    r1.setFontSize(24);
    r1.setText("Apache");
    XSLFTextRun r2 = textBox.addNewTextParagraph().addNewTextRun();
    r2.setStrikethrough(true);
    r2.setUnderline(true);
    r2.setText("POI\u2122");
    XSLFTextRun r3 = textBox.addNewTextParagraph().addNewTextRun();
    r3.setFontFamily("Wingdings");
    r3.setText(" Version 3.8");

    textBox.setAnchor(new Rectangle(50, 50, 200, 100));
    textBox.setLineColor(Color.black);
    textBox.setFillColor(Color.orange);

    XSLFAutoShape shape2 = slide1.createAutoShape();

    shape2.setAnchor(new Rectangle(100, 100, 200, 200));

    XSLFFreeformShape shape3 = slide1.createFreeform();
    Rectangle rect = new Rectangle(150, 150, 300, 300);
    GeneralPath path = new GeneralPath(rect);
    path.append(new Ellipse2D.Double(200, 200, 100, 50), false);
    shape3.setPath(path);
    shape3.setAnchor(path.getBounds2D());
    shape3.setLineColor(Color.black);
    shape3.setFillColor(Color.lightGray);

    XSLFSlide slide2 = ppt.createSlide();
    XSLFGroupShape group = slide2.createGroup();

    group.setAnchor(new Rectangle(0, 0, 792, 612));
    group.setInteriorAnchor(new Rectangle(-10, -10, 20, 20));

    XSLFAutoShape shape4 = group.createAutoShape();
    shape4.setAnchor(new Rectangle(0, 0, 5, 5));
    shape4.setLineWidth(5);
    shape4.setLineColor(Color.black);

    FileOutputStream out = new FileOutputStream("xslf-demo.pptx");
    ppt.write(out);
    out.close();
}

From source file:poi.xslf.usermodel.Tutorial4.java

License:Apache License

public static void main(String[] args) throws IOException {
    XMLSlideShow ppt = new XMLSlideShow();

    // XSLFSlide#createSlide() with no arguments creates a blank slide
    XSLFSlide slide = ppt.createSlide();

    XSLFTable tbl = slide.createTable();
    tbl.setAnchor(new Rectangle2D.Double(50, 50, 450, 300));

    int numColumns = 3;
    int numRows = 5;
    XSLFTableRow headerRow = tbl.addRow();
    headerRow.setHeight(50);//  w ww  .j  ava2s  .  c o m
    // header
    for (int i = 0; i < numColumns; i++) {
        XSLFTableCell th = headerRow.addCell();
        XSLFTextParagraph p = th.addNewTextParagraph();
        p.setTextAlign(TextAlign.CENTER);
        XSLFTextRun r = p.addNewTextRun();
        r.setText("Header " + (i + 1));
        r.setBold(true);
        r.setFontColor(Color.white);
        th.setFillColor(new Color(79, 129, 189));
        th.setBorderBottom(2);
        th.setBorderBottomColor(Color.white);

        tbl.setColumnWidth(i, 150); // all columns are equally sized
    }

    // rows

    for (int rownum = 0; rownum < numRows; rownum++) {
        XSLFTableRow tr = tbl.addRow();
        tr.setHeight(50);
        // header
        for (int i = 0; i < numColumns; i++) {
            XSLFTableCell cell = tr.addCell();
            XSLFTextParagraph p = cell.addNewTextParagraph();
            XSLFTextRun r = p.addNewTextRun();

            r.setText("Cell " + (i + 1));
            if (rownum % 2 == 0)
                cell.setFillColor(new Color(208, 216, 232));
            else
                cell.setFillColor(new Color(233, 247, 244));

        }

    }

    FileOutputStream out = new FileOutputStream("table.pptx");
    ppt.write(out);
    out.close();
}