01: /* ====================================================================
02: Licensed to the Apache Software Foundation (ASF) under one or more
03: contributor license agreements. See the NOTICE file distributed with
04: this work for additional information regarding copyright ownership.
05: The ASF licenses this file to You under the Apache License, Version 2.0
06: (the "License"); you may not use this file except in compliance with
07: the License. You may obtain a copy of the License at
08:
09: http://www.apache.org/licenses/LICENSE-2.0
10:
11: Unless required by applicable law or agreed to in writing, software
12: distributed under the License is distributed on an "AS IS" BASIS,
13: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14: See the License for the specific language governing permissions and
15: limitations under the License.
16: ==================================================================== */
17: package org.apache.poi.hslf.examples;
18:
19: import org.apache.poi.hslf.usermodel.SlideShow;
20: import org.apache.poi.hslf.model.Slide;
21: import org.apache.poi.hslf.model.TextRun;
22: import org.apache.poi.hslf.model.Hyperlink;
23: import org.apache.poi.hslf.model.Shape;
24:
25: import java.io.FileInputStream;
26:
27: /**
28: * Demonstrates how to read hyperlinks from a presentation
29: *
30: * @author Yegor Kozlov
31: */
32: public class Hyperlinks {
33:
34: public static void main(String[] args) throws Exception {
35: for (int i = 0; i < args.length; i++) {
36: FileInputStream is = new FileInputStream(args[i]);
37: SlideShow ppt = new SlideShow(is);
38: is.close();
39:
40: Slide[] slide = ppt.getSlides();
41: for (int j = 0; j < slide.length; j++) {
42: System.out
43: .println("slide " + slide[j].getSlideNumber());
44:
45: //read hyperlinks from the slide's text runs
46: System.out
47: .println("reading hyperlinks from the text runs");
48: TextRun[] txt = slide[j].getTextRuns();
49: for (int k = 0; k < txt.length; k++) {
50: String text = txt[k].getText();
51: Hyperlink[] links = txt[k].getHyperlinks();
52: if (links != null)
53: for (int l = 0; l < links.length; l++) {
54: Hyperlink link = links[l];
55: String title = link.getTitle();
56: String address = link.getAddress();
57: System.out.println(" " + title);
58: System.out.println(" " + address);
59: String substring = text.substring(link
60: .getStartIndex(), link
61: .getEndIndex() - 1);//in ppt end index is inclusive
62: System.out.println(" " + substring);
63: }
64: }
65:
66: //in PowerPoint you can assign a hyperlink to a shape without text,
67: //for example to a Line object. The code below demonstrates how to
68: //read such hyperlinks
69: System.out
70: .println(" reading hyperlinks from the slide's shapes");
71: Shape[] sh = slide[j].getShapes();
72: for (int k = 0; k < sh.length; k++) {
73: Hyperlink link = sh[k].getHyperlink();
74: if (link != null) {
75: String title = link.getTitle();
76: String address = link.getAddress();
77: System.out.println(" " + title);
78: System.out.println(" " + address);
79: }
80: }
81: }
82:
83: }
84:
85: }
86: }
|