use of org.apache.poi.hslf.usermodel.HSLFSlideShow in project poi by apache.
the class TestShapes method textBoxSet.
private void textBoxSet(String filename) throws IOException {
HSLFSlideShow ss = new HSLFSlideShow(_slTests.openResourceAsStream(filename));
for (HSLFSlide sld : ss.getSlides()) {
ArrayList<String> lst1 = new ArrayList<String>();
for (List<HSLFTextParagraph> txt : sld.getTextParagraphs()) {
for (HSLFTextParagraph p : txt) {
for (HSLFTextRun r : p) {
lst1.add(r.getRawText());
}
}
}
ArrayList<String> lst2 = new ArrayList<String>();
for (HSLFShape sh : sld.getShapes()) {
if (sh instanceof HSLFTextShape) {
HSLFTextShape tbox = (HSLFTextShape) sh;
for (HSLFTextParagraph p : tbox.getTextParagraphs()) {
for (HSLFTextRun r : p) {
lst2.add(r.getRawText());
}
}
}
}
assertTrue(lst1.containsAll(lst2));
assertTrue(lst2.containsAll(lst1));
}
ss.close();
}
use of org.apache.poi.hslf.usermodel.HSLFSlideShow 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.HSLFSlideShow 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.HSLFSlideShow in project poi by apache.
the class TestShapes method setUp.
@Before
public void setUp() throws Exception {
InputStream is1 = null, is2 = null;
try {
is1 = _slTests.openResourceAsStream("empty.ppt");
ppt = new HSLFSlideShow(is1);
is2 = _slTests.openResourceAsStream("empty_textbox.ppt");
pptB = new HSLFSlideShow(is2);
} finally {
is1.close();
is2.close();
}
}
use of org.apache.poi.hslf.usermodel.HSLFSlideShow 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();
}
Aggregations