List of usage examples for org.apache.poi.xslf.usermodel XSLFTextRun setItalic
@Override
public void setItalic(boolean italic)
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./*from ww w . j a va 2 s . c o 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);/*from w w w. j a va2 s . c om*/ r1.setItalic(true); 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(); }