Search in sources :

Example 16 with HSLFSlide

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

the class Hyperlinks method main.

public static void main(String[] args) throws Exception {
    for (int i = 0; i < args.length; i++) {
        FileInputStream is = new FileInputStream(args[i]);
        HSLFSlideShow ppt = new HSLFSlideShow(is);
        is.close();
        for (HSLFSlide slide : ppt.getSlides()) {
            System.out.println("\nslide " + slide.getSlideNumber());
            // read hyperlinks from the slide's text runs
            System.out.println("- reading hyperlinks from the text runs");
            for (List<HSLFTextParagraph> paras : slide.getTextParagraphs()) {
                for (HSLFTextParagraph para : paras) {
                    for (HSLFTextRun run : para) {
                        HSLFHyperlink link = run.getHyperlink();
                        if (link != null) {
                            System.out.println(toStr(link, run.getRawText()));
                        }
                    }
                }
            }
            // in PowerPoint you can assign a hyperlink to a shape without text,
            // for example to a Line object. The code below demonstrates how to
            // read such hyperlinks
            System.out.println("- reading hyperlinks from the slide's shapes");
            for (HSLFShape sh : slide.getShapes()) {
                if (sh instanceof HSLFSimpleShape) {
                    HSLFHyperlink link = ((HSLFSimpleShape) sh).getHyperlink();
                    if (link != null) {
                        System.out.println(toStr(link, null));
                    }
                }
            }
        }
        ppt.close();
    }
}
Also used : HSLFTextRun(org.apache.poi.hslf.usermodel.HSLFTextRun) HSLFShape(org.apache.poi.hslf.usermodel.HSLFShape) HSLFTextParagraph(org.apache.poi.hslf.usermodel.HSLFTextParagraph) HSLFHyperlink(org.apache.poi.hslf.usermodel.HSLFHyperlink) HSLFSimpleShape(org.apache.poi.hslf.usermodel.HSLFSimpleShape) HSLFSlideShow(org.apache.poi.hslf.usermodel.HSLFSlideShow) HSLFSlide(org.apache.poi.hslf.usermodel.HSLFSlide) FileInputStream(java.io.FileInputStream)

Example 17 with HSLFSlide

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

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

the class PowerPointExtractor method getText.

public String getText(boolean getSlideText, boolean getNoteText, boolean getCommentText, boolean getMasterText) {
    StringBuffer ret = new StringBuffer();
    if (getSlideText) {
        if (getMasterText) {
            for (HSLFSlideMaster master : _show.getSlideMasters()) {
                for (HSLFShape sh : master.getShapes()) {
                    if (sh instanceof HSLFTextShape) {
                        HSLFTextShape hsh = (HSLFTextShape) sh;
                        final String text = hsh.getText();
                        if (text == null || "".equals(text) || "*".equals(text)) {
                            continue;
                        }
                        if (HSLFMasterSheet.isPlaceholder(sh)) {
                            // check for metro shape of complex placeholder
                            boolean isMetro = new HSLFMetroShape<HSLFShape>(sh).hasMetroBlob();
                            if (!isMetro) {
                                // don't bother about boiler plate text on master sheets
                                LOG.log(POILogger.INFO, "Ignoring boiler plate (placeholder) text on slide master:", text);
                                continue;
                            }
                        }
                        ret.append(text);
                        if (!text.endsWith("\n")) {
                            ret.append("\n");
                        }
                    }
                }
            }
        }
        for (HSLFSlide slide : _slides) {
            String headerText = "";
            String footerText = "";
            HeadersFooters hf = slide.getHeadersFooters();
            if (hf != null) {
                if (hf.isHeaderVisible()) {
                    headerText = safeLine(hf.getHeaderText());
                }
                if (hf.isFooterVisible()) {
                    footerText = safeLine(hf.getFooterText());
                }
            }
            // Slide header, if set
            ret.append(headerText);
            // Slide text
            textRunsToText(ret, slide.getTextParagraphs());
            // Table text
            for (HSLFShape shape : slide.getShapes()) {
                if (shape instanceof HSLFTable) {
                    extractTableText(ret, (HSLFTable) shape);
                }
            }
            // Slide footer, if set
            ret.append(footerText);
            // Comments, if requested and present
            if (getCommentText) {
                for (Comment comment : slide.getComments()) {
                    ret.append(comment.getAuthor() + " - " + comment.getText() + "\n");
                }
            }
        }
        if (getNoteText) {
            ret.append('\n');
        }
    }
    if (getNoteText) {
        // Not currently using _notes, as that can have the notes of
        // master sheets in. Grab Slide list, then work from there,
        // but ensure no duplicates
        Set<Integer> seenNotes = new HashSet<Integer>();
        String headerText = "";
        String footerText = "";
        HeadersFooters hf = _show.getNotesHeadersFooters();
        if (hf != null) {
            if (hf.isHeaderVisible()) {
                headerText = safeLine(hf.getHeaderText());
            }
            if (hf.isFooterVisible()) {
                footerText = safeLine(hf.getFooterText());
            }
        }
        for (HSLFSlide slide : _slides) {
            HSLFNotes notes = slide.getNotes();
            if (notes == null) {
                continue;
            }
            Integer id = Integer.valueOf(notes._getSheetNumber());
            if (seenNotes.contains(id)) {
                continue;
            }
            seenNotes.add(id);
            // Repeat the Notes header, if set
            ret.append(headerText);
            // Notes text
            textRunsToText(ret, notes.getTextParagraphs());
            // Repeat the notes footer, if set
            ret.append(footerText);
        }
    }
    return ret.toString();
}
Also used : Comment(org.apache.poi.hslf.model.Comment) HSLFSlideMaster(org.apache.poi.hslf.usermodel.HSLFSlideMaster) HeadersFooters(org.apache.poi.hslf.model.HeadersFooters) HSLFNotes(org.apache.poi.hslf.usermodel.HSLFNotes) HSLFShape(org.apache.poi.hslf.usermodel.HSLFShape) HSLFTable(org.apache.poi.hslf.usermodel.HSLFTable) HSLFTextShape(org.apache.poi.hslf.usermodel.HSLFTextShape) HSLFSlide(org.apache.poi.hslf.usermodel.HSLFSlide) HashSet(java.util.HashSet)

Example 19 with HSLFSlide

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

the class TestDocumentEncryption method cryptoAPIDecryption.

@Test
public void cryptoAPIDecryption() throws Exception {
    // taken from a msdn blog:
    // http://blogs.msdn.com/b/openspecification/archive/2009/05/08/dominic-salemno.aspx
    Biff8EncryptionKey.setCurrentUserPassword("crypto");
    NPOIFSFileSystem fs = new NPOIFSFileSystem(slTests.getFile("cryptoapi-proc2356.ppt"));
    HSLFSlideShowImpl hss = new HSLFSlideShowImpl(fs);
    HSLFSlideShow ss = new HSLFSlideShow(hss);
    HSLFSlide slide = ss.getSlides().get(0);
    String rawText = HSLFTextParagraph.getRawText(slide.getTextParagraphs().get(0));
    assertEquals("Dominic Salemno", rawText);
    String[][] picCmp = { { "0", "nKsDTKqxTCR8LFkVVWlP9GSTvZ0=" }, { "95163", "SuNOR+9V1UVYZIoeD65l3VTaLoc=" }, { "100864", "Ql3IGrr4bNq07ZTp5iPg7b+pva8=" }, { "714114", "8pdst9NjBGSfWezSZE8+aVhIRe0=" }, { "723752", "go6xqW7lvkCtlOO5tYLiMfb4oxw=" }, { "770128", "gZUM8YqRNL5kGNfyyYvEEernvCc=" }, { "957958", "CNU2iiqUFAnk3TDXsXV1ihH9eRM=" } };
    MessageDigest md = CryptoFunctions.getMessageDigest(HashAlgorithm.sha1);
    List<HSLFPictureData> pd = hss.getPictureData();
    int i = 0;
    for (HSLFPictureData p : pd) {
        byte[] hash = md.digest(p.getData());
        assertEquals(Integer.parseInt(picCmp[i][0]), p.getOffset());
        assertEquals(picCmp[i][1], Base64.encodeBase64String(hash));
        i++;
    }
    DocumentEncryptionAtom dea = hss.getDocumentEncryptionAtom();
    POIFSFileSystem fs2 = ((CryptoAPIDecryptor) dea.getEncryptionInfo().getDecryptor()).getSummaryEntries(fs.getRoot(), "EncryptedSummary");
    PropertySet ps = PropertySetFactory.create(fs2.getRoot(), SummaryInformation.DEFAULT_STREAM_NAME);
    assertTrue(ps.isSummaryInformation());
    assertEquals("RC4 CryptoAPI Encryption", ps.getProperties()[1].getValue());
    ps = PropertySetFactory.create(fs2.getRoot(), DocumentSummaryInformation.DEFAULT_STREAM_NAME);
    assertTrue(ps.isDocumentSummaryInformation());
    assertEquals("On-screen Show (4:3)", ps.getProperties()[1].getValue());
    ss.close();
    fs.close();
    fs2.close();
}
Also used : HSLFSlideShow(org.apache.poi.hslf.usermodel.HSLFSlideShow) HSLFSlideShowImpl(org.apache.poi.hslf.usermodel.HSLFSlideShowImpl) NPOIFSFileSystem(org.apache.poi.poifs.filesystem.NPOIFSFileSystem) POIFSFileSystem(org.apache.poi.poifs.filesystem.POIFSFileSystem) NPOIFSFileSystem(org.apache.poi.poifs.filesystem.NPOIFSFileSystem) PropertySet(org.apache.poi.hpsf.PropertySet) MessageDigest(java.security.MessageDigest) HSLFPictureData(org.apache.poi.hslf.usermodel.HSLFPictureData) HSLFSlide(org.apache.poi.hslf.usermodel.HSLFSlide) CryptoAPIDecryptor(org.apache.poi.poifs.crypt.cryptoapi.CryptoAPIDecryptor) Test(org.junit.Test)

Example 20 with HSLFSlide

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

the class TestSlideAtom method testSSSlideInfoAtom.

@Test
public void testSSSlideInfoAtom() throws IOException {
    HSLFSlideShow ss1 = new HSLFSlideShow();
    HSLFSlide slide1 = ss1.createSlide(), slide2 = ss1.createSlide();
    slide2.setHidden(true);
    HSLFSlideShow ss2 = HSLFTestDataSamples.writeOutAndReadBack(ss1);
    slide1 = ss2.getSlides().get(0);
    slide2 = ss2.getSlides().get(1);
    assertFalse(slide1.getHidden());
    assertTrue(slide2.getHidden());
    ss2.close();
    ss1.close();
}
Also used : HSLFSlideShow(org.apache.poi.hslf.usermodel.HSLFSlideShow) 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