use of org.apache.poi.hslf.usermodel.HSLFSlide in project poi by apache.
the class TestShapes method textBoxRead.
/**
* Verify that we can read TextBox shapes
* @throws Exception
*/
@Test
public void textBoxRead() throws IOException {
ppt = new HSLFSlideShow(_slTests.openResourceAsStream("with_textbox.ppt"));
HSLFSlide sl = ppt.getSlides().get(0);
for (HSLFShape sh : sl.getShapes()) {
assertTrue(sh instanceof HSLFTextBox);
HSLFTextBox txtbox = (HSLFTextBox) sh;
String text = txtbox.getText();
assertNotNull(text);
assertEquals(txtbox.getTextParagraphs().get(0).getTextRuns().size(), 1);
HSLFTextRun rt = txtbox.getTextParagraphs().get(0).getTextRuns().get(0);
if (text.equals("Hello, World!!!")) {
assertEquals(32, rt.getFontSize(), 0);
assertTrue(rt.isBold());
assertTrue(rt.isItalic());
} else if (text.equals("I am just a poor boy")) {
assertEquals(44, rt.getFontSize(), 0);
assertTrue(rt.isBold());
} else if (text.equals("This is Times New Roman")) {
assertEquals(16, rt.getFontSize(), 0);
assertTrue(rt.isBold());
assertTrue(rt.isItalic());
assertTrue(rt.isUnderlined());
} else if (text.equals("Plain Text")) {
assertEquals(18, rt.getFontSize(), 0);
}
}
}
use of org.apache.poi.hslf.usermodel.HSLFSlide in project poi by apache.
the class TestShapes method testParagraphs.
@SuppressWarnings("unused")
@Test
public void testParagraphs() throws IOException {
HSLFSlideShow ss = new HSLFSlideShow();
HSLFSlide slide = ss.createSlide();
HSLFTextBox shape = new HSLFTextBox();
HSLFTextRun p1r1 = shape.setText("para 1 run 1. ");
HSLFTextRun p1r2 = shape.appendText("para 1 run 2.", false);
HSLFTextRun p2r1 = shape.appendText("para 2 run 1. ", true);
HSLFTextRun p2r2 = shape.appendText("para 2 run 2. ", false);
p1r1.setFontColor(Color.black);
p1r2.setFontColor(Color.red);
p2r1.setFontColor(Color.yellow);
p2r2.setStrikethrough(true);
// run 3 has same text properties as run 2 and will be merged when saving
HSLFTextRun p2r3 = shape.appendText("para 2 run 3.", false);
shape.setAnchor(new Rectangle2D.Double(100, 100, 100, 10));
slide.addShape(shape);
shape.resizeToFitText();
ByteArrayOutputStream bos = new ByteArrayOutputStream();
ss.write(bos);
ss = new HSLFSlideShow(new ByteArrayInputStream(bos.toByteArray()));
slide = ss.getSlides().get(0);
HSLFTextBox tb = (HSLFTextBox) slide.getShapes().get(0);
List<HSLFTextParagraph> para = tb.getTextParagraphs();
HSLFTextRun tr = para.get(0).getTextRuns().get(0);
assertEquals("para 1 run 1. ", tr.getRawText());
assertTrue(sameColor(Color.black, tr.getFontColor()));
tr = para.get(0).getTextRuns().get(1);
assertEquals("para 1 run 2.\r", tr.getRawText());
assertTrue(sameColor(Color.red, tr.getFontColor()));
tr = para.get(1).getTextRuns().get(0);
assertEquals("para 2 run 1. ", tr.getRawText());
assertTrue(sameColor(Color.yellow, tr.getFontColor()));
tr = para.get(1).getTextRuns().get(1);
assertEquals("para 2 run 2. para 2 run 3.", tr.getRawText());
assertTrue(sameColor(Color.black, tr.getFontColor()));
assertTrue(tr.isStrikethrough());
}
use of org.apache.poi.hslf.usermodel.HSLFSlide in project poi by apache.
the class TestShapes method textBoxWriteBytes.
/**
* Verify that we can add TextBox shapes to a slide
* and set some of the style attributes
*/
@Test
public void textBoxWriteBytes() throws IOException {
ppt = new HSLFSlideShow();
HSLFSlide sl = ppt.createSlide();
HSLFTextRun rt;
String val = "Hello, World!";
// Create a new textbox, and give it lots of properties
HSLFTextBox txtbox = new HSLFTextBox();
rt = txtbox.getTextParagraphs().get(0).getTextRuns().get(0);
txtbox.setText(val);
rt.setFontFamily("Arial");
rt.setFontSize(42d);
rt.setBold(true);
rt.setItalic(true);
rt.setUnderlined(false);
rt.setFontColor(Color.red);
sl.addShape(txtbox);
// Check it before save
rt = txtbox.getTextParagraphs().get(0).getTextRuns().get(0);
assertEquals(val, rt.getRawText());
assertEquals(42, rt.getFontSize(), 0);
assertTrue(rt.isBold());
assertTrue(rt.isItalic());
assertFalse(rt.isUnderlined());
assertEquals("Arial", rt.getFontFamily());
assertTrue(sameColor(Color.red, rt.getFontColor()));
// Serialize and read again
ByteArrayOutputStream out = new ByteArrayOutputStream();
ppt.write(out);
out.close();
HSLFSlideShow ppt2 = new HSLFSlideShow(new ByteArrayInputStream(out.toByteArray()));
sl = ppt2.getSlides().get(0);
txtbox = (HSLFTextBox) sl.getShapes().get(0);
rt = txtbox.getTextParagraphs().get(0).getTextRuns().get(0);
// Check after save
assertEquals(val, rt.getRawText());
assertEquals(42, rt.getFontSize(), 0);
assertTrue(rt.isBold());
assertTrue(rt.isItalic());
assertFalse(rt.isUnderlined());
assertEquals("Arial", rt.getFontFamily());
assertTrue(sameColor(Color.red, rt.getFontColor()));
ppt2.close();
}
use of org.apache.poi.hslf.usermodel.HSLFSlide in project poi by apache.
the class TestShapes method emptyTextBox.
/**
* Test with an empty text box
*/
@Test
public void emptyTextBox() {
assertEquals(2, pptB.getSlides().size());
HSLFSlide s1 = pptB.getSlides().get(0);
HSLFSlide s2 = pptB.getSlides().get(1);
// Check we can get the shapes count
assertEquals(2, s1.getShapes().size());
assertEquals(2, s2.getShapes().size());
}
use of org.apache.poi.hslf.usermodel.HSLFSlide in project poi by apache.
the class TestShapes method graphics.
@Test
public void graphics() throws IOException {
HSLFSlide slide = ppt.createSlide();
HSLFLine line = new HSLFLine();
java.awt.Rectangle lineAnchor = new java.awt.Rectangle(100, 200, 50, 60);
line.setAnchor(lineAnchor);
line.setLineWidth(3);
line.setLineDash(LineDash.DASH);
line.setLineColor(Color.red);
slide.addShape(line);
HSLFAutoShape ellipse = new HSLFAutoShape(ShapeType.ELLIPSE);
Rectangle2D ellipseAnchor = new Rectangle2D.Double(320, 154, 55, 111);
ellipse.setAnchor(ellipseAnchor);
ellipse.setLineWidth(2);
ellipse.setLineDash(LineDash.SOLID);
ellipse.setLineColor(Color.green);
ellipse.setFillColor(Color.lightGray);
slide.addShape(ellipse);
ByteArrayOutputStream out = new ByteArrayOutputStream();
ppt.write(out);
out.close();
//read ppt from byte array
HSLFSlideShow ppt2 = new HSLFSlideShow(new ByteArrayInputStream(out.toByteArray()));
assertEquals(1, ppt2.getSlides().size());
slide = ppt2.getSlides().get(0);
List<HSLFShape> shape = slide.getShapes();
assertEquals(2, shape.size());
//group shape
assertTrue(shape.get(0) instanceof HSLFLine);
//group shape
assertEquals(lineAnchor, shape.get(0).getAnchor());
//group shape
assertTrue(shape.get(1) instanceof HSLFAutoShape);
//group shape
assertEquals(ellipseAnchor, shape.get(1).getAnchor());
ppt2.close();
}
Aggregations