use of org.apache.poi.sl.usermodel.TextRun in project poi by apache.
the class DrawTextShape method drawParagraphs.
/**
* paint the paragraphs starting from top left (x,y)
*
* @return the vertical advance, i.e. the cumulative space occupied by the text
*/
public double drawParagraphs(Graphics2D graphics, double x, double y) {
DrawFactory fact = DrawFactory.getInstance(graphics);
double y0 = y;
//noinspection RedundantCast
@SuppressWarnings("cast") Iterator<? extends TextParagraph<?, ?, ? extends TextRun>> paragraphs = (Iterator<? extends TextParagraph<?, ?, ? extends TextRun>>) getShape().iterator();
boolean isFirstLine = true;
for (int autoNbrIdx = 0; paragraphs.hasNext(); autoNbrIdx++) {
TextParagraph<?, ?, ? extends TextRun> p = paragraphs.next();
DrawTextParagraph dp = fact.getDrawable(p);
BulletStyle bs = p.getBulletStyle();
if (bs == null || bs.getAutoNumberingScheme() == null) {
autoNbrIdx = -1;
} else {
Integer startAt = bs.getAutoNumberingStartAt();
if (startAt == null)
startAt = 1;
// TODO: handle reset auto number indexes
if (startAt > autoNbrIdx)
autoNbrIdx = startAt;
}
dp.setAutoNumberingIdx(autoNbrIdx);
dp.breakText(graphics);
if (!isFirstLine) {
// the amount of vertical white space before the paragraph
Double spaceBefore = p.getSpaceBefore();
if (spaceBefore == null)
spaceBefore = 0d;
if (spaceBefore > 0) {
// positive value means percentage spacing of the height of the first line, e.g.
// the higher the first line, the bigger the space before the paragraph
y += spaceBefore * 0.01 * dp.getFirstLineHeight();
} else {
// negative value means the absolute spacing in points
y += -spaceBefore;
}
}
isFirstLine = false;
dp.setPosition(x, y);
dp.draw(graphics);
y += dp.getY();
if (paragraphs.hasNext()) {
Double spaceAfter = p.getSpaceAfter();
if (spaceAfter == null)
spaceAfter = 0d;
if (spaceAfter > 0) {
// positive value means percentage spacing of the height of the last line, e.g.
// the higher the last line, the bigger the space after the paragraph
y += spaceAfter * 0.01 * dp.getLastLineHeight();
} else {
// negative value means the absolute spacing in points
y += -spaceAfter;
}
}
}
return y - y0;
}
use of org.apache.poi.sl.usermodel.TextRun in project poi by apache.
the class ApacheconEU08 method slide4.
public static void slide4(SlideShow<?, ?> ppt) throws IOException {
Slide<?, ?> slide = ppt.createSlide();
String[][] txt1 = { { "Note" }, { "This presentation was created programmatically using POI HSLF" } };
TableShape<?, ?> table1 = slide.createTable(2, 1);
for (int i = 0; i < txt1.length; i++) {
for (int j = 0; j < txt1[i].length; j++) {
TableCell<?, ?> cell = table1.getCell(i, j);
cell.setText(txt1[i][j]);
TextRun rt = cell.getTextParagraphs().get(0).getTextRuns().get(0);
rt.setFontSize(10d);
rt.setFontFamily("Arial");
rt.setBold(true);
if (i == 0) {
rt.setFontSize(32d);
rt.setFontColor(Color.white);
cell.setFillColor(new Color(0, 153, 204));
} else {
rt.setFontSize(28d);
cell.setFillColor(new Color(235, 239, 241));
}
cell.setVerticalAlignment(VerticalAlignment.MIDDLE);
}
}
DrawTableShape dts = new DrawTableShape(table1);
dts.setAllBorders(1.0, Color.black);
dts.setOutsideBorders(4.0);
table1.setColumnWidth(0, 450);
table1.setRowHeight(0, 50);
table1.setRowHeight(1, 80);
Dimension dim = ppt.getPageSize();
Rectangle2D oldAnchor = table1.getAnchor();
table1.setAnchor(new Rectangle2D.Double((dim.width - 450) / 2d, 100, oldAnchor.getWidth(), oldAnchor.getHeight()));
TextBox<?, ?> box1 = slide.createTextBox();
box1.setHorizontalCentered(true);
box1.getTextParagraphs().get(0).getTextRuns().get(0).setFontSize(24d);
box1.setText("The source code is available at\r" + "http://people.apache.org/~yegor/apachecon_eu08/");
box1.setAnchor(new Rectangle(80, 356, 553, 65));
}
use of org.apache.poi.sl.usermodel.TextRun in project poi by apache.
the class ApacheconEU08 method slide6.
public static void slide6(SlideShow<?, ?> ppt) throws IOException {
Slide<?, ?> slide = ppt.createSlide();
TextBox<?, ?> box1 = slide.createTextBox();
box1.setTextPlaceholder(TextPlaceholder.TITLE);
box1.setText("HSLF in Action - 2");
box1.setAnchor(new Rectangle(36, 20, 648, 90));
TextBox<?, ?> box2 = slide.createTextBox();
box2.getTextParagraphs().get(0).getTextRuns().get(0).setFontSize(18d);
box2.setText("Creating a simple presentation from scratch");
box2.setAnchor(new Rectangle(170, 100, 364, 30));
TextBox<?, ?> box3 = slide.createTextBox();
TextRun rt3 = box3.getTextParagraphs().get(0).getTextRuns().get(0);
rt3.setFontFamily("Courier New");
rt3.setFontSize(8d);
box3.setText("SlideShow ppt = new SlideShow();" + "Slide slide = ppt.createSlide();" + "" + "TextBox box2 = new TextBox();" + "box2.setHorizontalAlignment(TextBox.AlignCenter);" + "box2.setVerticalAlignment(TextBox.AnchorMiddle);" + "box2.getTextRun().setText(\"Java Code\");" + "box2.getFill().setForegroundColor(new Color(187, 224, 227));" + "box2.setLineColor(Color.black);" + "box2.setLineWidth(0.75);" + "box2.setAnchor(new Rectangle(66, 243, 170, 170));" + "slide.addShape(box2);" + "" + "TextBox box3 = new TextBox();" + "box3.setHorizontalAlignment(TextBox.AlignCenter);" + "box3.setVerticalAlignment(TextBox.AnchorMiddle);" + "box3.getTextRun().setText(\"*.ppt file\");" + "box3.setLineWidth(0.75);" + "box3.setLineColor(Color.black);" + "box3.getFill().setForegroundColor(new Color(187, 224, 227));" + "box3.setAnchor(new Rectangle(473, 243, 170, 170));" + "slide.addShape(box3);" + "" + "AutoShape box4 = new AutoShape(ShapeTypes.Arrow);" + "box4.getFill().setForegroundColor(new Color(187, 224, 227));" + "box4.setLineWidth(0.75);" + "box4.setLineColor(Color.black);" + "box4.setAnchor(new Rectangle(253, 288, 198, 85));" + "slide.addShape(box4);" + "" + "FileOutputStream out = new FileOutputStream(\"hslf-demo.ppt\");" + "ppt.write(out);" + "out.close();");
box3.setAnchor(new Rectangle(30, 150, 618, 411));
box3.setHorizontalCentered(true);
}
use of org.apache.poi.sl.usermodel.TextRun in project poi by apache.
the class ApacheconEU08 method slide9.
public static void slide9(SlideShow<?, ?> ppt) throws IOException {
Slide<?, ?> slide = ppt.createSlide();
TextBox<?, ?> box1 = slide.createTextBox();
box1.setTextPlaceholder(TextPlaceholder.TITLE);
box1.setText("HSLF in Action - 3");
box1.setAnchor(new Rectangle(36, 20, 648, 50));
TextBox<?, ?> box2 = slide.createTextBox();
box2.getTextParagraphs().get(0).getTextRuns().get(0).setFontSize(18d);
box2.setText("PPGraphics2D: PowerPoint Graphics2D driver");
box2.setAnchor(new Rectangle(178, 70, 387, 30));
TextBox<?, ?> box3 = slide.createTextBox();
TextRun rt3 = box3.getTextParagraphs().get(0).getTextRuns().get(0);
rt3.setFontFamily("Courier New");
rt3.setFontSize(8d);
box3.setText("//bar chart data. The first value is the bar color, the second is the width" + "Object[] def = new Object[]{" + " Color.yellow, new Integer(100)," + " Color.green, new Integer(150)," + " Color.gray, new Integer(75)," + " Color.red, new Integer(200)," + "};" + "" + "SlideShow ppt = new SlideShow();" + "Slide slide = ppt.createSlide();" + "" + "ShapeGroup group = new ShapeGroup();" + "//define position of the drawing in the slide" + "Rectangle bounds = new java.awt.Rectangle(200, 100, 350, 300);" + "group.setAnchor(bounds);" + "slide.addShape(group);" + "Graphics2D graphics = new PPGraphics2D(group);" + "" + "//draw a simple bar graph" + "int x = bounds.x + 50, y = bounds.y + 50;" + "graphics.setFont(new Font(\"Arial\", Font.BOLD, 10));" + "for (int i = 0, idx = 1; i < def.length; i+=2, idx++) {" + " graphics.setColor(Color.black);" + " int width = ((Integer)def[i+1]).intValue();" + " graphics.drawString(\"Q\" + idx, x-20, y+20);" + " graphics.drawString(width + \"%\", x + width + 10, y + 20);" + " graphics.setColor((Color)def[i]);" + " graphics.fill(new Rectangle(x, y, width, 30));" + " y += 40;" + "}" + "graphics.setColor(Color.black);" + "graphics.setFont(new Font(\"Arial\", Font.BOLD, 14));" + "graphics.draw(bounds);" + "graphics.drawString(\"Performance\", x + 70, y + 40);" + "" + "FileOutputStream out = new FileOutputStream(\"hslf-demo.ppt\");" + "ppt.write(out);" + "out.close();");
box3.setAnchor(new Rectangle(96, 110, 499, 378));
box3.setHorizontalCentered(true);
}
use of org.apache.poi.sl.usermodel.TextRun in project poi by apache.
the class TestBugs method bug45124.
@Test
public void bug45124() throws IOException {
SlideShow<?, ?> ppt = open("bug45124.ppt");
Slide<?, ?> slide1 = ppt.getSlides().get(1);
TextBox<?, ?> res = slide1.createTextBox();
res.setAnchor(new java.awt.Rectangle(60, 150, 700, 100));
res.setText("I am italic-false, bold-true inserted text");
TextParagraph<?, ?, ?> tp = res.getTextParagraphs().get(0);
TextRun rt = tp.getTextRuns().get(0);
rt.setItalic(false);
assertTrue(rt.isBold());
tp.setBulletStyle(Color.red, 'A');
SlideShow<?, ?> ppt2 = HSLFTestDataSamples.writeOutAndReadBack((HSLFSlideShow) ppt);
ppt.close();
res = (TextBox<?, ?>) ppt2.getSlides().get(1).getShapes().get(1);
tp = res.getTextParagraphs().get(0);
rt = tp.getTextRuns().get(0);
assertFalse(rt.isItalic());
assertTrue(rt.isBold());
PaintStyle ps = tp.getBulletStyle().getBulletFontColor();
assertTrue(ps instanceof SolidPaint);
Color actColor = DrawPaint.applyColorTransform(((SolidPaint) ps).getSolidColor());
assertEquals(Color.red, actColor);
assertEquals("A", tp.getBulletStyle().getBulletCharacter());
ppt2.close();
}
Aggregations