Search in sources :

Example 26 with HSLFSlide

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);
        }
    }
}
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 27 with HSLFSlide

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());
}
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 28 with HSLFSlide

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();
}
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)

Example 29 with HSLFSlide

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

Example 30 with HSLFSlide

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

Aggregations

HSLFSlide (org.apache.poi.hslf.usermodel.HSLFSlide)43 HSLFSlideShow (org.apache.poi.hslf.usermodel.HSLFSlideShow)39 Test (org.junit.Test)32 HSLFShape (org.apache.poi.hslf.usermodel.HSLFShape)17 ByteArrayInputStream (java.io.ByteArrayInputStream)9 ByteArrayOutputStream (java.io.ByteArrayOutputStream)9 HSLFTextRun (org.apache.poi.hslf.usermodel.HSLFTextRun)9 HSLFTextParagraph (org.apache.poi.hslf.usermodel.HSLFTextParagraph)8 Rectangle2D (java.awt.geom.Rectangle2D)7 FileOutputStream (java.io.FileOutputStream)6 HSLFPictureData (org.apache.poi.hslf.usermodel.HSLFPictureData)6 HSLFTextBox (org.apache.poi.hslf.usermodel.HSLFTextBox)6 InputStream (java.io.InputStream)5 FileInputStream (java.io.FileInputStream)4 HeadersFooters (org.apache.poi.hslf.model.HeadersFooters)4 HSLFHyperlink (org.apache.poi.hslf.usermodel.HSLFHyperlink)4 HSLFLine (org.apache.poi.hslf.usermodel.HSLFLine)4 HSLFTable (org.apache.poi.hslf.usermodel.HSLFTable)4 HSLFAutoShape (org.apache.poi.hslf.usermodel.HSLFAutoShape)3 Dimension (java.awt.Dimension)2