Search in sources :

Example 1 with HSLFTextBox

use of org.apache.poi.hslf.usermodel.HSLFTextBox in project poi by apache.

the class BulletsDemo method main.

public static void main(String[] args) throws IOException {
    HSLFSlideShow ppt = new HSLFSlideShow();
    try {
        HSLFSlide slide = ppt.createSlide();
        HSLFTextBox shape = new HSLFTextBox();
        HSLFTextParagraph rt = shape.getTextParagraphs().get(0);
        rt.getTextRuns().get(0).setFontSize(42d);
        rt.setBullet(true);
        //bullet offset
        rt.setIndent(0d);
        //text offset (should be greater than bullet offset)
        rt.setLeftMargin(50d);
        //bullet character
        rt.setBulletChar('☺');
        shape.setText("January\r" + "February\r" + "March\r" + "April");
        slide.addShape(shape);
        //position of the text box in the slide
        shape.setAnchor(new java.awt.Rectangle(50, 50, 500, 300));
        slide.addShape(shape);
        FileOutputStream out = new FileOutputStream("bullets.ppt");
        ppt.write(out);
        out.close();
    } finally {
        ppt.close();
    }
}
Also used : HSLFTextBox(org.apache.poi.hslf.usermodel.HSLFTextBox) HSLFTextParagraph(org.apache.poi.hslf.usermodel.HSLFTextParagraph) FileOutputStream(java.io.FileOutputStream) HSLFSlideShow(org.apache.poi.hslf.usermodel.HSLFSlideShow) HSLFSlide(org.apache.poi.hslf.usermodel.HSLFSlide)

Example 2 with HSLFTextBox

use of org.apache.poi.hslf.usermodel.HSLFTextBox in project poi by apache.

the class CreateHyperlink method main.

public static void main(String[] args) throws IOException {
    HSLFSlideShow ppt = new HSLFSlideShow();
    try {
        HSLFSlide slideA = ppt.createSlide();
        ppt.createSlide();
        HSLFSlide slideC = ppt.createSlide();
        // link to a URL
        HSLFTextBox textBox1 = slideA.createTextBox();
        textBox1.setText("Apache POI");
        textBox1.setAnchor(new Rectangle(100, 100, 200, 50));
        HSLFHyperlink link1 = textBox1.getTextParagraphs().get(0).getTextRuns().get(0).createHyperlink();
        link1.linkToUrl("http://www.apache.org");
        link1.setLabel(textBox1.getText());
        // link to another slide
        HSLFTextBox textBox2 = slideA.createTextBox();
        textBox2.setText("Go to slide #3");
        textBox2.setAnchor(new Rectangle(100, 300, 200, 50));
        HSLFHyperlink link2 = textBox2.getTextParagraphs().get(0).getTextRuns().get(0).createHyperlink();
        link2.linkToSlide(slideC);
        FileOutputStream out = new FileOutputStream("hyperlink.ppt");
        ppt.write(out);
        out.close();
    } finally {
        ppt.close();
    }
}
Also used : HSLFTextBox(org.apache.poi.hslf.usermodel.HSLFTextBox) HSLFHyperlink(org.apache.poi.hslf.usermodel.HSLFHyperlink) FileOutputStream(java.io.FileOutputStream) Rectangle(java.awt.Rectangle) HSLFSlideShow(org.apache.poi.hslf.usermodel.HSLFSlideShow) HSLFSlide(org.apache.poi.hslf.usermodel.HSLFSlide)

Example 3 with HSLFTextBox

use of org.apache.poi.hslf.usermodel.HSLFTextBox 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 4 with HSLFTextBox

use of org.apache.poi.hslf.usermodel.HSLFTextBox 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 5 with HSLFTextBox

use of org.apache.poi.hslf.usermodel.HSLFTextBox 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

HSLFTextBox (org.apache.poi.hslf.usermodel.HSLFTextBox)7 HSLFSlide (org.apache.poi.hslf.usermodel.HSLFSlide)6 HSLFSlideShow (org.apache.poi.hslf.usermodel.HSLFSlideShow)6 HSLFTextRun (org.apache.poi.hslf.usermodel.HSLFTextRun)5 Test (org.junit.Test)4 Rectangle (java.awt.Rectangle)2 Rectangle2D (java.awt.geom.Rectangle2D)2 ByteArrayInputStream (java.io.ByteArrayInputStream)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 FileOutputStream (java.io.FileOutputStream)2 HSLFHyperlink (org.apache.poi.hslf.usermodel.HSLFHyperlink)2 HSLFTextParagraph (org.apache.poi.hslf.usermodel.HSLFTextParagraph)2 TextLayout (java.awt.font.TextLayout)1 HSLFShape (org.apache.poi.hslf.usermodel.HSLFShape)1