Search in sources :

Example 1 with HSLFHyperlink

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

the class TestHyperlink method testTextRunHyperlinks.

@Test
public void testTextRunHyperlinks() throws Exception {
    HSLFSlideShow ppt = new HSLFSlideShow(_slTests.openResourceAsStream("WithLinks.ppt"));
    HSLFSlide slide = ppt.getSlides().get(0);
    List<HSLFTextParagraph> para = slide.getTextParagraphs().get(1);
    String rawText = toExternalString(getRawText(para), para.get(0).getRunType());
    String expected = "This page has two links:\n" + "http://jakarta.apache.org/poi/\n" + "\n" + "http://slashdot.org/\n" + "\n" + "In addition, its notes has one link";
    assertEquals(expected, rawText);
    List<HSLFHyperlink> links = findHyperlinks(para);
    assertEquals(2, links.size());
    assertEquals("http://jakarta.apache.org/poi/", links.get(0).getLabel());
    assertEquals("http://jakarta.apache.org/poi/", links.get(0).getAddress());
    assertEquals("http://jakarta.apache.org/poi/", rawText.substring(links.get(0).getStartIndex(), links.get(0).getEndIndex() - 1));
    assertEquals("http://slashdot.org/", links.get(1).getLabel());
    assertEquals("http://slashdot.org/", links.get(1).getAddress());
    assertEquals("http://slashdot.org/", rawText.substring(links.get(1).getStartIndex(), links.get(1).getEndIndex() - 1));
    slide = ppt.getSlides().get(1);
    para = slide.getTextParagraphs().get(1);
    rawText = toExternalString(getRawText(para), para.get(0).getRunType());
    expected = "I have the one link:\n" + "Jakarta HSSF";
    assertEquals(expected, rawText);
    links.clear();
    links = findHyperlinks(para);
    assertNotNull(links);
    assertEquals(1, links.size());
    assertEquals("Open Jakarta POI HSSF module test  ", links.get(0).getLabel());
    assertEquals("http://jakarta.apache.org/poi/hssf/", links.get(0).getAddress());
    assertEquals("Jakarta HSSF", rawText.substring(links.get(0).getStartIndex(), links.get(0).getEndIndex() - 1));
    ppt.close();
}
Also used : HSLFTextParagraph(org.apache.poi.hslf.usermodel.HSLFTextParagraph) HSLFHyperlink(org.apache.poi.hslf.usermodel.HSLFHyperlink) HSLFTextParagraph.toExternalString(org.apache.poi.hslf.usermodel.HSLFTextParagraph.toExternalString) HSLFSlideShow(org.apache.poi.hslf.usermodel.HSLFSlideShow) HSLFSlide(org.apache.poi.hslf.usermodel.HSLFSlide) Test(org.junit.Test)

Example 2 with HSLFHyperlink

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

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

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

the class TestHyperlink method bug47291.

@Test
public void bug47291() throws IOException {
    HSLFSlideShow ppt1 = new HSLFSlideShow();
    HSLFSlide slide1 = ppt1.createSlide();
    HSLFTextRun r1 = slide1.createTextBox().setText("page1");
    HSLFHyperlink hl1 = r1.createHyperlink();
    hl1.linkToEmail("dev@poi.apache.org");
    HSLFTextRun r2 = ppt1.createSlide().createTextBox().setText("page2");
    HSLFHyperlink hl2 = r2.createHyperlink();
    hl2.linkToLastSlide();
    HSLFSlide sl1 = ppt1.createSlide();
    HSLFTextBox tb1 = sl1.createTextBox();
    tb1.setAnchor(new Rectangle2D.Double(100, 100, 100, 100));
    tb1.appendText("text1 ", false);
    HSLFTextRun r3 = tb1.appendText("link", false);
    tb1.appendText(" text2", false);
    HSLFHyperlink hl3 = r3.createHyperlink();
    hl3.linkToSlide(slide1);
    HSLFTextRun r4 = ppt1.createSlide().createTextBox().setText("page4");
    HSLFHyperlink hl4 = r4.createHyperlink();
    hl4.linkToUrl("http://poi.apache.org");
    HSLFTextBox tb5 = ppt1.createSlide().createTextBox();
    tb5.setText("page5");
    HSLFHyperlink hl5 = tb5.createHyperlink();
    hl5.linkToFirstSlide();
    HSLFSlideShow ppt2 = HSLFTestDataSamples.writeOutAndReadBack(ppt1);
    ppt1.close();
    List<HSLFSlide> slides = ppt2.getSlides();
    tb1 = (HSLFTextBox) slides.get(0).getShapes().get(0);
    hl1 = tb1.getTextParagraphs().get(0).getTextRuns().get(0).getHyperlink();
    assertNotNull(hl1);
    assertEquals("dev@poi.apache.org", hl1.getLabel());
    assertEquals(HyperlinkType.EMAIL, hl1.getTypeEnum());
    HSLFTextBox tb2 = (HSLFTextBox) slides.get(1).getShapes().get(0);
    hl2 = tb2.getTextParagraphs().get(0).getTextRuns().get(0).getHyperlink();
    assertNotNull(hl2);
    assertEquals(InteractiveInfoAtom.LINK_LastSlide, hl2.getInfo().getInteractiveInfoAtom().getHyperlinkType());
    assertEquals(HyperlinkType.DOCUMENT, hl2.getTypeEnum());
    HSLFTextBox tb3 = (HSLFTextBox) slides.get(2).getShapes().get(0);
    hl3 = tb3.getTextParagraphs().get(0).getTextRuns().get(1).getHyperlink();
    assertNotNull(hl3);
    assertEquals(ppt2.getSlides().get(0)._getSheetNumber(), Integer.parseInt(hl3.getAddress().split(",")[0]));
    assertEquals(HyperlinkType.DOCUMENT, hl3.getTypeEnum());
    HSLFTextBox tb4 = (HSLFTextBox) slides.get(3).getShapes().get(0);
    hl4 = tb4.getTextParagraphs().get(0).getTextRuns().get(0).getHyperlink();
    assertNotNull(hl4);
    assertEquals("http://poi.apache.org", hl4.getLabel());
    assertEquals(HyperlinkType.URL, hl4.getTypeEnum());
    tb5 = (HSLFTextBox) slides.get(4).getShapes().get(0);
    hl5 = tb5.getHyperlink();
    assertNotNull(hl5);
    assertEquals(InteractiveInfoAtom.LINK_FirstSlide, hl5.getInfo().getInteractiveInfoAtom().getHyperlinkType());
    assertEquals(HyperlinkType.DOCUMENT, hl5.getTypeEnum());
    ppt2.close();
}
Also used : HSLFTextRun(org.apache.poi.hslf.usermodel.HSLFTextRun) HSLFTextBox(org.apache.poi.hslf.usermodel.HSLFTextBox) HSLFHyperlink(org.apache.poi.hslf.usermodel.HSLFHyperlink) Rectangle2D(java.awt.geom.Rectangle2D) HSLFSlideShow(org.apache.poi.hslf.usermodel.HSLFSlideShow) HSLFSlide(org.apache.poi.hslf.usermodel.HSLFSlide) Test(org.junit.Test)

Aggregations

HSLFHyperlink (org.apache.poi.hslf.usermodel.HSLFHyperlink)4 HSLFSlide (org.apache.poi.hslf.usermodel.HSLFSlide)4 HSLFSlideShow (org.apache.poi.hslf.usermodel.HSLFSlideShow)4 HSLFTextBox (org.apache.poi.hslf.usermodel.HSLFTextBox)2 HSLFTextParagraph (org.apache.poi.hslf.usermodel.HSLFTextParagraph)2 HSLFTextRun (org.apache.poi.hslf.usermodel.HSLFTextRun)2 Test (org.junit.Test)2 Rectangle (java.awt.Rectangle)1 Rectangle2D (java.awt.geom.Rectangle2D)1 FileInputStream (java.io.FileInputStream)1 FileOutputStream (java.io.FileOutputStream)1 HSLFShape (org.apache.poi.hslf.usermodel.HSLFShape)1 HSLFSimpleShape (org.apache.poi.hslf.usermodel.HSLFSimpleShape)1 HSLFTextParagraph.toExternalString (org.apache.poi.hslf.usermodel.HSLFTextParagraph.toExternalString)1