Search in sources :

Example 1 with HSLFTextRun

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

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

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

Example 4 with HSLFTextRun

use of org.apache.poi.hslf.usermodel.HSLFTextRun in project tika by apache.

the class HSLFExtractor method textRunsToText.

private void textRunsToText(XHTMLContentHandler xhtml, List<List<HSLFTextParagraph>> paragraphsList) throws SAXException {
    if (paragraphsList == null) {
        return;
    }
    for (List<HSLFTextParagraph> run : paragraphsList) {
        // Leaving in wisdom from TIKA-712 for easy revert.
        // Avoid boiler-plate text on the master slide (0
        // = TextHeaderAtom.TITLE_TYPE, 1 = TextHeaderAtom.BODY_TYPE):
        //if (!isMaster || (run.getRunType() != 0 && run.getRunType() != 1)) {
        boolean isBullet = false;
        for (HSLFTextParagraph htp : run) {
            boolean nextBullet = htp.isBullet();
            // TODO: identify bullet/list type
            if (isBullet != nextBullet) {
                isBullet = nextBullet;
                if (isBullet) {
                    xhtml.startElement("ul");
                } else {
                    xhtml.endElement("ul");
                }
            }
            List<HSLFTextRun> textRuns = htp.getTextRuns();
            String firstLine = removePBreak(textRuns.get(0).getRawText());
            boolean showBullet = (isBullet && (textRuns.size() > 1 || !"".equals(firstLine)));
            String paraTag = showBullet ? "li" : "p";
            xhtml.startElement(paraTag);
            boolean runIsHyperLink = false;
            for (HSLFTextRun htr : textRuns) {
                Hyperlink link = htr.getHyperlink();
                if (link != null) {
                    String address = link.getAddress();
                    if (address != null && !address.startsWith("_ftn")) {
                        xhtml.startElement("a", "href", link.getAddress());
                        runIsHyperLink = true;
                    }
                }
                String line = htr.getRawText();
                if (line != null) {
                    boolean isfirst = true;
                    for (String fragment : line.split("\\u000b")) {
                        if (!isfirst) {
                            xhtml.startElement("br");
                            xhtml.endElement("br");
                        }
                        isfirst = false;
                        xhtml.characters(removePBreak(fragment));
                    }
                    if (line.endsWith("")) {
                        xhtml.startElement("br");
                        xhtml.endElement("br");
                    }
                }
                if (runIsHyperLink) {
                    xhtml.endElement("a");
                }
                runIsHyperLink = false;
            }
            xhtml.endElement(paraTag);
        }
        if (isBullet) {
            xhtml.endElement("ul");
        }
    }
}
Also used : HSLFTextRun(org.apache.poi.hslf.usermodel.HSLFTextRun) HSLFTextParagraph(org.apache.poi.hslf.usermodel.HSLFTextParagraph) Hyperlink(org.apache.poi.common.usermodel.Hyperlink)

Example 5 with HSLFTextRun

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

the class TableDemo method create2ndTable.

static void create2ndTable(HSLFSlide slide) {
    //two rows, one column
    HSLFTable table2 = slide.createTable(2, 1);
    for (int i = 0; i < txt2.length; i++) {
        for (int j = 0; j < txt2[i].length; j++) {
            HSLFTableCell cell = table2.getCell(i, j);
            HSLFTextRun rt = cell.getTextParagraphs().get(0).getTextRuns().get(0);
            rt.setFontSize(10d);
            rt.setFontFamily("Arial");
            if (i == 0) {
                cell.getFill().setForegroundColor(new Color(0, 51, 102));
                rt.setFontColor(Color.white);
                rt.setBold(true);
                rt.setFontSize(14d);
                cell.setHorizontalCentered(true);
            } else {
                rt.getTextParagraph().setBullet(true);
                rt.setFontSize(12d);
                rt.getTextParagraph().setTextAlign(TextAlign.LEFT);
                cell.setHorizontalCentered(false);
            }
            cell.setVerticalAlignment(VerticalAlignment.MIDDLE);
            cell.setText(txt2[i][j]);
        }
    }
    table2.setColumnWidth(0, 300);
    table2.setRowHeight(0, 30);
    table2.setRowHeight(1, 70);
    DrawTableShape dts2 = new DrawTableShape(table2);
    dts2.setOutsideBorders(Color.black, 1.0);
    table2.moveTo(200, 400);
}
Also used : HSLFTextRun(org.apache.poi.hslf.usermodel.HSLFTextRun) HSLFTable(org.apache.poi.hslf.usermodel.HSLFTable) Color(java.awt.Color) HSLFTableCell(org.apache.poi.hslf.usermodel.HSLFTableCell) DrawTableShape(org.apache.poi.sl.draw.DrawTableShape)

Aggregations

HSLFTextRun (org.apache.poi.hslf.usermodel.HSLFTextRun)14 HSLFSlide (org.apache.poi.hslf.usermodel.HSLFSlide)9 HSLFSlideShow (org.apache.poi.hslf.usermodel.HSLFSlideShow)9 HSLFTextParagraph (org.apache.poi.hslf.usermodel.HSLFTextParagraph)8 Test (org.junit.Test)8 HSLFTextBox (org.apache.poi.hslf.usermodel.HSLFTextBox)5 ByteArrayInputStream (java.io.ByteArrayInputStream)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)3 HSLFShape (org.apache.poi.hslf.usermodel.HSLFShape)3 Color (java.awt.Color)2 Rectangle2D (java.awt.geom.Rectangle2D)2 HSLFHyperlink (org.apache.poi.hslf.usermodel.HSLFHyperlink)2 HSLFTable (org.apache.poi.hslf.usermodel.HSLFTable)2 HSLFTableCell (org.apache.poi.hslf.usermodel.HSLFTableCell)2 DrawTableShape (org.apache.poi.sl.draw.DrawTableShape)2 Rectangle (java.awt.Rectangle)1 TextLayout (java.awt.font.TextLayout)1 FileInputStream (java.io.FileInputStream)1 ArrayList (java.util.ArrayList)1 Hyperlink (org.apache.poi.common.usermodel.Hyperlink)1