Search in sources :

Example 31 with HSLFSlideShow

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

the class ApacheconEU08 method main.

public static void main(String[] args) throws IOException {
    SlideShow<?, ?> ppt = new HSLFSlideShow();
    // SlideShow<?,?> ppt = new XMLSlideShow();
    ppt.setPageSize(new Dimension(720, 540));
    slide1(ppt);
    slide2(ppt);
    slide3(ppt);
    slide4(ppt);
    slide5(ppt);
    slide6(ppt);
    slide7(ppt);
    slide8(ppt);
    slide9(ppt);
    slide10(ppt);
    slide11(ppt);
    slide12(ppt);
    String ext = ppt.getClass().getName().contains("HSLF") ? "ppt" : "pptx";
    FileOutputStream out = new FileOutputStream("apachecon_eu_08." + ext);
    ppt.write(out);
    out.close();
    ppt.close();
}
Also used : FileOutputStream(java.io.FileOutputStream) Dimension(java.awt.Dimension) HSLFSlideShow(org.apache.poi.hslf.usermodel.HSLFSlideShow)

Example 32 with HSLFSlideShow

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

the class HSLFExtractor method parse.

protected void parse(DirectoryNode root, XHTMLContentHandler xhtml) throws IOException, SAXException, TikaException {
    HSLFSlideShow ss = new HSLFSlideShow(root);
    List<HSLFSlide> _slides = ss.getSlides();
    xhtml.startElement("div", "class", "slideShow");
    /* Iterate over slides and extract text */
    for (HSLFSlide slide : _slides) {
        xhtml.startElement("div", "class", "slide");
        // Slide header, if present
        HeadersFooters hf = slide.getHeadersFooters();
        if (hf != null && hf.isHeaderVisible() && hf.getHeaderText() != null) {
            xhtml.startElement("p", "class", "slide-header");
            xhtml.characters(hf.getHeaderText());
            xhtml.endElement("p");
        }
        // Slide master, if present
        extractMaster(xhtml, slide.getMasterSheet());
        // Slide text
        {
            xhtml.startElement("div", "class", "slide-content");
            textRunsToText(xhtml, slide.getTextParagraphs());
            xhtml.endElement("div");
        }
        // Table text
        for (HSLFShape shape : slide.getShapes()) {
            if (shape instanceof HSLFTable) {
                extractTableText(xhtml, (HSLFTable) shape);
            }
        }
        // Slide footer, if present
        if (hf != null && hf.isFooterVisible() && hf.getFooterText() != null) {
            xhtml.startElement("p", "class", "slide-footer");
            xhtml.characters(hf.getFooterText());
            xhtml.endElement("p");
        }
        // Comments, if present
        StringBuilder authorStringBuilder = new StringBuilder();
        for (Comment comment : slide.getComments()) {
            authorStringBuilder.setLength(0);
            xhtml.startElement("p", "class", "slide-comment");
            if (comment.getAuthor() != null) {
                authorStringBuilder.append(comment.getAuthor());
            }
            if (comment.getAuthorInitials() != null) {
                if (authorStringBuilder.length() > 0) {
                    authorStringBuilder.append(" ");
                }
                authorStringBuilder.append("(" + comment.getAuthorInitials() + ")");
            }
            if (authorStringBuilder.length() > 0) {
                if (comment.getText() != null) {
                    authorStringBuilder.append(" - ");
                }
                xhtml.startElement("b");
                xhtml.characters(authorStringBuilder.toString());
                xhtml.endElement("b");
            }
            if (comment.getText() != null) {
                xhtml.characters(comment.getText());
            }
            xhtml.endElement("p");
        }
        // Now any embedded resources
        handleSlideEmbeddedResources(slide, xhtml);
        // Find the Notes for this slide and extract inline
        HSLFNotes notes = slide.getNotes();
        if (notes != null) {
            xhtml.startElement("div", "class", "slide-notes");
            textRunsToText(xhtml, notes.getTextParagraphs());
            xhtml.endElement("div");
        }
        // Slide complete
        xhtml.endElement("div");
    }
    // All slides done
    xhtml.endElement("div");
    /* notes */
    xhtml.startElement("div", "class", "slide-notes");
    HashSet<Integer> seenNotes = new HashSet<>();
    HeadersFooters hf = ss.getNotesHeadersFooters();
    for (HSLFSlide slide : _slides) {
        HSLFNotes notes = slide.getNotes();
        if (notes == null) {
            continue;
        }
        Integer id = notes._getSheetNumber();
        if (seenNotes.contains(id)) {
            continue;
        }
        seenNotes.add(id);
        // Repeat the Notes header, if set
        if (hf != null && hf.isHeaderVisible() && hf.getHeaderText() != null) {
            xhtml.startElement("p", "class", "slide-note-header");
            xhtml.characters(hf.getHeaderText());
            xhtml.endElement("p");
        }
        // Notes text
        textRunsToText(xhtml, notes.getTextParagraphs());
        // Repeat the notes footer, if set
        if (hf != null && hf.isFooterVisible() && hf.getFooterText() != null) {
            xhtml.startElement("p", "class", "slide-note-footer");
            xhtml.characters(hf.getFooterText());
            xhtml.endElement("p");
        }
    }
    handleSlideEmbeddedPictures(ss, xhtml);
    xhtml.endElement("div");
}
Also used : HeadersFooters(org.apache.poi.hslf.model.HeadersFooters) HSLFNotes(org.apache.poi.hslf.usermodel.HSLFNotes) Comment(org.apache.poi.hslf.model.Comment) HSLFShape(org.apache.poi.hslf.usermodel.HSLFShape) HSLFTable(org.apache.poi.hslf.usermodel.HSLFTable) HSLFSlideShow(org.apache.poi.hslf.usermodel.HSLFSlideShow) HSLFSlide(org.apache.poi.hslf.usermodel.HSLFSlide) HashSet(java.util.HashSet)

Example 33 with HSLFSlideShow

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

the class TestShapes method lineColor.

@Test
public void lineColor() throws IOException {
    HSLFSlideShow ss = new HSLFSlideShow(_slTests.openResourceAsStream("51731.ppt"));
    List<HSLFShape> shape = ss.getSlides().get(0).getShapes();
    assertEquals(4, shape.size());
    HSLFTextShape sh1 = (HSLFTextShape) shape.get(0);
    assertEquals("Hello Apache POI", sh1.getText());
    assertNull(sh1.getLineColor());
    HSLFTextShape sh2 = (HSLFTextShape) shape.get(1);
    assertEquals("Why are you showing this border?", sh2.getText());
    assertNull(sh2.getLineColor());
    HSLFTextShape sh3 = (HSLFTextShape) shape.get(2);
    assertEquals("Text in a black border", sh3.getText());
    assertEquals(Color.black, sh3.getLineColor());
    assertEquals(0.75, sh3.getLineWidth(), 0);
    HSLFTextShape sh4 = (HSLFTextShape) shape.get(3);
    assertEquals("Border width is 5 pt", sh4.getText());
    assertEquals(Color.black, sh4.getLineColor());
    assertEquals(5.0, sh4.getLineWidth(), 0);
    ss.close();
}
Also used : HSLFShape(org.apache.poi.hslf.usermodel.HSLFShape) HSLFTextShape(org.apache.poi.hslf.usermodel.HSLFTextShape) HSLFSlideShow(org.apache.poi.hslf.usermodel.HSLFSlideShow) Test(org.junit.Test)

Example 34 with HSLFSlideShow

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

the class TestShapes method shapeId.

@Test
public void shapeId() throws IOException {
    HSLFSlideShow ss = new HSLFSlideShow();
    HSLFSlide slide = ss.createSlide();
    HSLFShape shape = null;
    //EscherDgg is a document-level record which keeps track of the drawing groups
    EscherDggRecord dgg = ss.getDocumentRecord().getPPDrawingGroup().getEscherDggRecord();
    EscherDgRecord dg = slide.getSheetContainer().getPPDrawing().getEscherDgRecord();
    //total number of shapes in the ppt
    int dggShapesUsed = dgg.getNumShapesSaved();
    //max number of shapeId
    int dggMaxId = dgg.getShapeIdMax();
    //max shapeId in the slide
    int dgMaxId = dg.getLastMSOSPID();
    // number of shapes in the slide
    int dgShapesUsed = dg.getNumShapes();
    //insert 3 shapes and make sure the Ids are properly incremented
    for (int i = 0; i < 3; i++) {
        shape = new HSLFLine();
        assertEquals(0, shape.getShapeId());
        slide.addShape(shape);
        assertTrue(shape.getShapeId() > 0);
        //check that EscherDgRecord is updated
        assertEquals(shape.getShapeId(), dg.getLastMSOSPID());
        assertEquals(dgMaxId + 1, dg.getLastMSOSPID());
        assertEquals(dgShapesUsed + 1, dg.getNumShapes());
        //check that EscherDggRecord is updated
        assertEquals(shape.getShapeId() + 1, dgg.getShapeIdMax());
        assertEquals(dggMaxId + 1, dgg.getShapeIdMax());
        assertEquals(dggShapesUsed + 1, dgg.getNumShapesSaved());
        dggShapesUsed = dgg.getNumShapesSaved();
        dggMaxId = dgg.getShapeIdMax();
        dgMaxId = dg.getLastMSOSPID();
        dgShapesUsed = dg.getNumShapes();
    }
    //For each drawing group PPT allocates clusters with size=1024
    //if the number of shapes is greater that 1024 a new cluster is allocated
    //make sure it is so
    int numClusters = dgg.getNumIdClusters();
    for (int i = 0; i < 1025; i++) {
        shape = new HSLFLine();
        slide.addShape(shape);
    }
    assertEquals(numClusters + 1, dgg.getNumIdClusters());
    ss.close();
}
Also used : HSLFLine(org.apache.poi.hslf.usermodel.HSLFLine) HSLFShape(org.apache.poi.hslf.usermodel.HSLFShape) EscherDggRecord(org.apache.poi.ddf.EscherDggRecord) EscherDgRecord(org.apache.poi.ddf.EscherDgRecord) HSLFSlideShow(org.apache.poi.hslf.usermodel.HSLFSlideShow) HSLFSlide(org.apache.poi.hslf.usermodel.HSLFSlide) Test(org.junit.Test)

Example 35 with HSLFSlideShow

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

the class TestShapes method shapeGroup.

/**
     * Test adding shapes to <code>ShapeGroup</code>
     */
@Test
public void shapeGroup() throws IOException {
    HSLFSlideShow ss = new HSLFSlideShow();
    HSLFSlide slide = ss.createSlide();
    Dimension pgsize = ss.getPageSize();
    HSLFGroupShape group = new HSLFGroupShape();
    group.setAnchor(new Rectangle2D.Double(0, 0, pgsize.getWidth(), pgsize.getHeight()));
    slide.addShape(group);
    HSLFPictureData data = ss.addPicture(_slTests.readFile("clock.jpg"), PictureType.JPEG);
    HSLFPictureShape pict = new HSLFPictureShape(data, group);
    pict.setAnchor(new Rectangle2D.Double(0, 0, 200, 200));
    group.addShape(pict);
    HSLFLine line = new HSLFLine(group);
    line.setAnchor(new Rectangle2D.Double(300, 300, 500, 0));
    group.addShape(line);
    //serialize and read again.
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    ss.write(out);
    out.close();
    ss.close();
    ByteArrayInputStream is = new ByteArrayInputStream(out.toByteArray());
    ss = new HSLFSlideShow(is);
    is.close();
    slide = ss.getSlides().get(0);
    List<HSLFShape> shape = slide.getShapes();
    assertEquals(1, shape.size());
    assertTrue(shape.get(0) instanceof HSLFGroupShape);
    group = (HSLFGroupShape) shape.get(0);
    List<HSLFShape> grshape = group.getShapes();
    assertEquals(2, grshape.size());
    assertTrue(grshape.get(0) instanceof HSLFPictureShape);
    assertTrue(grshape.get(1) instanceof HSLFLine);
    pict = (HSLFPictureShape) grshape.get(0);
    assertEquals(new Rectangle2D.Double(0, 0, 200, 200), pict.getAnchor());
    line = (HSLFLine) grshape.get(1);
    assertEquals(new Rectangle2D.Double(300, 300, 500, 0), line.getAnchor());
    ss.close();
}
Also used : Rectangle2D(java.awt.geom.Rectangle2D) Dimension(java.awt.Dimension) ByteArrayOutputStream(java.io.ByteArrayOutputStream) HSLFSlideShow(org.apache.poi.hslf.usermodel.HSLFSlideShow) HSLFGroupShape(org.apache.poi.hslf.usermodel.HSLFGroupShape) HSLFLine(org.apache.poi.hslf.usermodel.HSLFLine) HSLFPictureShape(org.apache.poi.hslf.usermodel.HSLFPictureShape) HSLFShape(org.apache.poi.hslf.usermodel.HSLFShape) ByteArrayInputStream(java.io.ByteArrayInputStream) HSLFPictureData(org.apache.poi.hslf.usermodel.HSLFPictureData) 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