Search in sources :

Example 1 with HSLFTextParagraph

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

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

the class TestSlideMaster method testIndentation.

/**
     * Varify we can read attrubutes for different identtation levels.
     * (typical for the "bullted body" placeholder)
     */
@Test
public void testIndentation() throws IOException {
    HSLFSlideShow ppt = new HSLFSlideShow(_slTests.openResourceAsStream("slide_master.ppt"));
    HSLFSlide slide = ppt.getSlides().get(0);
    for (List<HSLFTextParagraph> tparas : slide.getTextParagraphs()) {
        HSLFTextParagraph tpara = tparas.get(0);
        if (tpara.getRunType() == TextHeaderAtom.TITLE_TYPE) {
            HSLFTextRun rt = tpara.getTextRuns().get(0);
            assertEquals(40, rt.getFontSize(), 0);
            assertEquals(true, rt.isUnderlined());
            assertEquals("Arial", rt.getFontFamily());
        } else if (tpara.getRunType() == TextHeaderAtom.BODY_TYPE) {
            int[] indents = { 32, 28, 24 };
            for (HSLFTextRun rt : tpara.getTextRuns()) {
                int indent = tpara.getIndentLevel();
                assertEquals(indents[indent], rt.getFontSize(), 0);
            }
        }
    }
    ppt.close();
}
Also used : HSLFTextRun(org.apache.poi.hslf.usermodel.HSLFTextRun) HSLFTextParagraph(org.apache.poi.hslf.usermodel.HSLFTextParagraph) HSLFSlideShow(org.apache.poi.hslf.usermodel.HSLFSlideShow) HSLFSlide(org.apache.poi.hslf.usermodel.HSLFSlide) Test(org.junit.Test)

Example 3 with HSLFTextParagraph

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

the class TestSlideMaster method testMasterAttributes.

/**
     * If a style attribute is not set ensure it is read from the master
     */
@Test
public void testMasterAttributes() throws Exception {
    HSLFSlideShow ppt = new HSLFSlideShow(_slTests.openResourceAsStream("slide_master.ppt"));
    List<HSLFSlide> slide = ppt.getSlides();
    assertEquals(3, slide.size());
    for (List<HSLFTextParagraph> tparas : slide.get(0).getTextParagraphs()) {
        HSLFTextParagraph tpara = tparas.get(0);
        if (tpara.getRunType() == TextHeaderAtom.TITLE_TYPE) {
            HSLFTextRun rt = tpara.getTextRuns().get(0);
            assertEquals(40, rt.getFontSize(), 0);
            assertEquals(true, rt.isUnderlined());
            assertEquals("Arial", rt.getFontFamily());
        } else if (tpara.getRunType() == TextHeaderAtom.BODY_TYPE) {
            HSLFTextRun rt = tpara.getTextRuns().get(0);
            assertEquals(0, tpara.getIndentLevel());
            assertEquals(32, rt.getFontSize(), 0);
            assertEquals("Arial", rt.getFontFamily());
            tpara = tparas.get(1);
            rt = tpara.getTextRuns().get(0);
            assertEquals(1, tpara.getIndentLevel());
            assertEquals(28, rt.getFontSize(), 0);
            assertEquals("Arial", rt.getFontFamily());
        }
    }
    for (List<HSLFTextParagraph> tparas : slide.get(1).getTextParagraphs()) {
        HSLFTextParagraph tpara = tparas.get(0);
        if (tpara.getRunType() == TextHeaderAtom.TITLE_TYPE) {
            HSLFTextRun rt = tpara.getTextRuns().get(0);
            assertEquals(48, rt.getFontSize(), 0);
            assertEquals(true, rt.isItalic());
            assertEquals("Georgia", rt.getFontFamily());
        } else if (tpara.getRunType() == TextHeaderAtom.BODY_TYPE) {
            HSLFTextRun rt;
            rt = tpara.getTextRuns().get(0);
            assertEquals(0, tpara.getIndentLevel());
            assertEquals(32, rt.getFontSize(), 0);
            assertEquals("Courier New", rt.getFontFamily());
        }
    }
    ppt.close();
}
Also used : HSLFTextRun(org.apache.poi.hslf.usermodel.HSLFTextRun) HSLFTextParagraph(org.apache.poi.hslf.usermodel.HSLFTextParagraph) HSLFSlideShow(org.apache.poi.hslf.usermodel.HSLFSlideShow) HSLFSlide(org.apache.poi.hslf.usermodel.HSLFSlide) Test(org.junit.Test)

Example 4 with HSLFTextParagraph

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

the class TestTextRunReWrite method testWritesOutTheSameNonRich.

@Test
public void testWritesOutTheSameNonRich() throws IOException {
    // Ensure the text lengths are as we'd expect to start with
    assertEquals(1, ss.getSlides().size());
    assertEquals(2, ss.getSlides().get(0).getTextParagraphs().size());
    // Grab the first text run on the first sheet
    List<HSLFTextParagraph> tr1 = ss.getSlides().get(0).getTextParagraphs().get(0);
    List<HSLFTextParagraph> tr2 = ss.getSlides().get(0).getTextParagraphs().get(1);
    assertEquals(30, HSLFTextParagraph.getRawText(tr1).length());
    assertEquals(179, HSLFTextParagraph.getRawText(tr2).length());
    assertEquals(1, tr1.size());
    assertEquals(30, HSLFTextParagraph.getText(tr1).length());
    assertEquals(30, HSLFTextParagraph.getRawText(tr1).length());
    assertEquals(31, tr1.get(0).getTextRuns().get(0).getCharacterStyle().getCharactersCovered());
    assertEquals(31, tr1.get(0).getParagraphStyle().getCharactersCovered());
    // Set the text to be as it is now
    HSLFTextParagraph.setText(tr1, HSLFTextParagraph.getRawText(tr1));
    tr1 = ss.getSlides().get(0).getTextParagraphs().get(0);
    // Check the text lengths are still right
    assertEquals(30, HSLFTextParagraph.getRawText(tr1).length());
    assertEquals(179, HSLFTextParagraph.getRawText(tr2).length());
    assertEquals(1, tr1.size());
    assertEquals(30, HSLFTextParagraph.getText(tr1).length());
    assertEquals(30, HSLFTextParagraph.getRawText(tr1).length());
    assertEquals(31, tr1.get(0).getTextRuns().get(0).getCharacterStyle().getCharactersCovered());
    assertEquals(31, tr1.get(0).getParagraphStyle().getCharactersCovered());
    // Write the slideshow out to a byte array
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    ss.write(baos);
    // Build an input stream of it
    ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
    // Use POIFS to query that lot
    POIFSFileSystem npfs = new POIFSFileSystem(bais);
    // Check that the "PowerPoint Document" sections have the same size
    DirectoryNode oDir = ss.getSlideShowImpl().getDirectory();
    DocumentEntry oProps = (DocumentEntry) oDir.getEntry(HSLFSlideShow.POWERPOINT_DOCUMENT);
    DocumentEntry nProps = (DocumentEntry) npfs.getRoot().getEntry(HSLFSlideShow.POWERPOINT_DOCUMENT);
    assertEquals(oProps.getSize(), nProps.getSize());
    // Check that they contain the same data
    byte[] _oData = new byte[oProps.getSize()];
    byte[] _nData = new byte[nProps.getSize()];
    oDir.createDocumentInputStream(HSLFSlideShow.POWERPOINT_DOCUMENT).read(_oData);
    npfs.createDocumentInputStream(HSLFSlideShow.POWERPOINT_DOCUMENT).read(_nData);
    assertArrayEquals(_oData, _nData);
    npfs.close();
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) POIFSFileSystem(org.apache.poi.poifs.filesystem.POIFSFileSystem) HSLFTextParagraph(org.apache.poi.hslf.usermodel.HSLFTextParagraph) DocumentEntry(org.apache.poi.poifs.filesystem.DocumentEntry) DirectoryNode(org.apache.poi.poifs.filesystem.DirectoryNode) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Test(org.junit.Test)

Example 5 with HSLFTextParagraph

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

the class TestTextRunReWrite method testWritesOutTheSameRich.

@Test
public void testWritesOutTheSameRich() throws IOException {
    // Grab the first text run on the first sheet
    List<HSLFTextParagraph> tr1 = ss.getSlides().get(0).getTextParagraphs().get(0);
    // Get the first rich text run
    HSLFTextRun rtr1 = tr1.get(0).getTextRuns().get(0);
    // Check that the text sizes are as expected
    assertEquals(1, tr1.get(0).getTextRuns().size());
    assertEquals(30, HSLFTextParagraph.getRawText(tr1).length());
    assertEquals(30, rtr1.getLength());
    assertEquals(30, rtr1.getRawText().length());
    assertEquals(31, rtr1.getCharacterStyle().getCharactersCovered());
    assertEquals(31, tr1.get(0).getParagraphStyle().getCharactersCovered());
    // Set the text to be as it is now
    rtr1.setText(rtr1.getRawText());
    rtr1 = tr1.get(0).getTextRuns().get(0);
    // Check that the text sizes are still as expected
    assertEquals(1, tr1.get(0).getTextRuns().size());
    assertEquals(30, HSLFTextParagraph.getRawText(tr1).length());
    assertEquals(30, tr1.get(0).getTextRuns().get(0).getRawText().length());
    assertEquals(30, rtr1.getLength());
    assertEquals(30, rtr1.getRawText().length());
    assertEquals(31, rtr1.getCharacterStyle().getCharactersCovered());
    assertEquals(31, tr1.get(0).getParagraphStyle().getCharactersCovered());
    // Write the slideshow out to a byte array
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    ss.write(baos);
    // Build an input stream of it
    ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
    // Use POIFS to query that lot
    POIFSFileSystem npfs = new POIFSFileSystem(bais);
    // Check that the "PowerPoint Document" sections have the same size
    DirectoryNode oDir = ss.getSlideShowImpl().getDirectory();
    DocumentEntry oProps = (DocumentEntry) oDir.getEntry(HSLFSlideShow.POWERPOINT_DOCUMENT);
    DocumentEntry nProps = (DocumentEntry) npfs.getRoot().getEntry(HSLFSlideShow.POWERPOINT_DOCUMENT);
    assertEquals(oProps.getSize(), nProps.getSize());
    // Check that they contain the same data
    byte[] _oData = new byte[oProps.getSize()];
    byte[] _nData = new byte[nProps.getSize()];
    oDir.createDocumentInputStream(HSLFSlideShow.POWERPOINT_DOCUMENT).read(_oData);
    npfs.createDocumentInputStream(HSLFSlideShow.POWERPOINT_DOCUMENT).read(_nData);
    assertArrayEquals(_oData, _nData);
    npfs.close();
}
Also used : HSLFTextRun(org.apache.poi.hslf.usermodel.HSLFTextRun) ByteArrayInputStream(java.io.ByteArrayInputStream) POIFSFileSystem(org.apache.poi.poifs.filesystem.POIFSFileSystem) HSLFTextParagraph(org.apache.poi.hslf.usermodel.HSLFTextParagraph) DocumentEntry(org.apache.poi.poifs.filesystem.DocumentEntry) DirectoryNode(org.apache.poi.poifs.filesystem.DirectoryNode) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Test(org.junit.Test)

Aggregations

HSLFTextParagraph (org.apache.poi.hslf.usermodel.HSLFTextParagraph)11 HSLFSlide (org.apache.poi.hslf.usermodel.HSLFSlide)8 HSLFSlideShow (org.apache.poi.hslf.usermodel.HSLFSlideShow)8 HSLFTextRun (org.apache.poi.hslf.usermodel.HSLFTextRun)8 Test (org.junit.Test)7 ByteArrayInputStream (java.io.ByteArrayInputStream)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)3 HSLFHyperlink (org.apache.poi.hslf.usermodel.HSLFHyperlink)2 HSLFShape (org.apache.poi.hslf.usermodel.HSLFShape)2 HSLFTextBox (org.apache.poi.hslf.usermodel.HSLFTextBox)2 DirectoryNode (org.apache.poi.poifs.filesystem.DirectoryNode)2 DocumentEntry (org.apache.poi.poifs.filesystem.DocumentEntry)2 POIFSFileSystem (org.apache.poi.poifs.filesystem.POIFSFileSystem)2 Rectangle2D (java.awt.geom.Rectangle2D)1 FileInputStream (java.io.FileInputStream)1 FileOutputStream (java.io.FileOutputStream)1 ArrayList (java.util.ArrayList)1 Hyperlink (org.apache.poi.common.usermodel.Hyperlink)1 HSLFMasterSheet (org.apache.poi.hslf.usermodel.HSLFMasterSheet)1 HSLFSimpleShape (org.apache.poi.hslf.usermodel.HSLFSimpleShape)1