Search in sources :

Example 36 with HSLFSlideShow

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();
}
Also used : HSLFTextRun(org.apache.poi.hslf.usermodel.HSLFTextRun) HSLFShape(org.apache.poi.hslf.usermodel.HSLFShape) HSLFTextParagraph(org.apache.poi.hslf.usermodel.HSLFTextParagraph) ArrayList(java.util.ArrayList) HSLFTextShape(org.apache.poi.hslf.usermodel.HSLFTextShape) HSLFSlideShow(org.apache.poi.hslf.usermodel.HSLFSlideShow) HSLFSlide(org.apache.poi.hslf.usermodel.HSLFSlide)

Example 37 with HSLFSlideShow

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);
        }
    }
}
Also used : HSLFTextRun(org.apache.poi.hslf.usermodel.HSLFTextRun) HSLFShape(org.apache.poi.hslf.usermodel.HSLFShape) HSLFTextBox(org.apache.poi.hslf.usermodel.HSLFTextBox) HSLFSlideShow(org.apache.poi.hslf.usermodel.HSLFSlideShow) HSLFSlide(org.apache.poi.hslf.usermodel.HSLFSlide) Test(org.junit.Test)

Example 38 with HSLFSlideShow

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());
}
Also used : HSLFTextRun(org.apache.poi.hslf.usermodel.HSLFTextRun) HSLFTextBox(org.apache.poi.hslf.usermodel.HSLFTextBox) ByteArrayInputStream(java.io.ByteArrayInputStream) HSLFTextParagraph(org.apache.poi.hslf.usermodel.HSLFTextParagraph) Rectangle2D(java.awt.geom.Rectangle2D) ByteArrayOutputStream(java.io.ByteArrayOutputStream) HSLFSlideShow(org.apache.poi.hslf.usermodel.HSLFSlideShow) HSLFSlide(org.apache.poi.hslf.usermodel.HSLFSlide) Test(org.junit.Test)

Example 39 with HSLFSlideShow

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();
    }
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) HSLFSlideShow(org.apache.poi.hslf.usermodel.HSLFSlideShow) Before(org.junit.Before)

Example 40 with HSLFSlideShow

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();
}
Also used : HSLFTextRun(org.apache.poi.hslf.usermodel.HSLFTextRun) HSLFTextBox(org.apache.poi.hslf.usermodel.HSLFTextBox) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) HSLFSlideShow(org.apache.poi.hslf.usermodel.HSLFSlideShow) HSLFSlide(org.apache.poi.hslf.usermodel.HSLFSlide) Test(org.junit.Test)

Aggregations

HSLFSlideShow (org.apache.poi.hslf.usermodel.HSLFSlideShow)69 Test (org.junit.Test)42 HSLFSlide (org.apache.poi.hslf.usermodel.HSLFSlide)39 HSLFShape (org.apache.poi.hslf.usermodel.HSLFShape)17 ByteArrayInputStream (java.io.ByteArrayInputStream)12 ByteArrayOutputStream (java.io.ByteArrayOutputStream)12 InputStream (java.io.InputStream)10 FileOutputStream (java.io.FileOutputStream)9 HSLFTextRun (org.apache.poi.hslf.usermodel.HSLFTextRun)9 HSLFTextParagraph (org.apache.poi.hslf.usermodel.HSLFTextParagraph)8 Rectangle2D (java.awt.geom.Rectangle2D)7 FileInputStream (java.io.FileInputStream)7 HSLFPictureData (org.apache.poi.hslf.usermodel.HSLFPictureData)7 HSSFWorkbook (org.apache.poi.hssf.usermodel.HSSFWorkbook)7 HWPFDocument (org.apache.poi.hwpf.HWPFDocument)7 HSLFSlideShowImpl (org.apache.poi.hslf.usermodel.HSLFSlideShowImpl)6 HSLFTextBox (org.apache.poi.hslf.usermodel.HSLFTextBox)5 POIDataSamples (org.apache.poi.POIDataSamples)4 Record (org.apache.poi.hslf.record.Record)4 HSLFHyperlink (org.apache.poi.hslf.usermodel.HSLFHyperlink)4