KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > pdfbox > examples > pdmodel > PrintURLs


1 /**
2  * Copyright (c) 2005, www.pdfbox.org
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
8  * 1. Redistributions of source code must retain the above copyright notice,
9  * this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright notice,
11  * this list of conditions and the following disclaimer in the documentation
12  * and/or other materials provided with the distribution.
13  * 3. Neither the name of pdfbox; nor the names of its
14  * contributors may be used to endorse or promote products derived from this
15  * software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20  * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
21  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
24  * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  *
28  * http://www.pdfbox.org
29  *
30  */

31 package org.pdfbox.examples.pdmodel;
32
33 import java.awt.geom.Rectangle2D JavaDoc;
34 import java.util.List JavaDoc;
35
36 import org.pdfbox.pdmodel.PDDocument;
37 import org.pdfbox.pdmodel.PDPage;
38
39 import org.pdfbox.pdmodel.common.PDRectangle;
40 import org.pdfbox.pdmodel.interactive.action.type.PDAction;
41 import org.pdfbox.pdmodel.interactive.action.type.PDActionURI;
42 import org.pdfbox.pdmodel.interactive.annotation.PDAnnotation;
43 import org.pdfbox.pdmodel.interactive.annotation.PDAnnotationLink;
44 import org.pdfbox.util.PDFTextStripperByArea;
45
46
47 /**
48  * This is an example of how to access a URL in a PDF document.
49  *
50  * @author <a HREF="mailto:ben@benlitchfield.com">Ben Litchfield</a>
51  * @version $Revision: 1.3 $
52  */

53 public class PrintURLs
54 {
55     /**
56      * Constructor.
57      */

58     private PrintURLs()
59     {
60         //utility class
61
}
62
63     /**
64      * This will create a hello world PDF document.
65      * <br />
66      * see usage() for commandline
67      *
68      * @param args Command line arguments.
69      *
70      * @throws Exception If there is an error extracting the URLs.
71      */

72     public static void main(String JavaDoc[] args) throws Exception JavaDoc
73     {
74         PDDocument doc = null;
75         try
76         {
77             if( args.length != 1 )
78             {
79                 usage();
80             }
81             else
82             {
83                 doc = PDDocument.load( args[0] );
84                 List JavaDoc allPages = doc.getDocumentCatalog().getAllPages();
85                 for( int i=0; i<allPages.size(); i++ )
86                 {
87                     PDFTextStripperByArea stripper = new PDFTextStripperByArea();
88                     PDPage page = (PDPage)allPages.get( i );
89                     List JavaDoc annotations = page.getAnnotations();
90                     //first setup text extraction regions
91
for( int j=0; j<annotations.size(); j++ )
92                     {
93                         PDAnnotation annot = (PDAnnotation)annotations.get( j );
94                         if( annot instanceof PDAnnotationLink )
95                         {
96                             PDAnnotationLink link = (PDAnnotationLink)annot;
97                             PDRectangle rect = link.getRectangle();
98                             //need to reposition link rectangle to match text space
99
float x = rect.getLowerLeftX();
100                             float y = rect.getUpperRightY();
101                             float width = rect.getWidth();
102                             float height = rect.getHeight();
103                             int rotation = page.findRotation();
104                             if( rotation == 0 )
105                             {
106                                 PDRectangle pageSize = page.findMediaBox();
107                                 y = pageSize.getHeight() - y;
108                             }
109                             else if( rotation == 90 )
110                             {
111                                 //do nothing
112
}
113                             
114                             Rectangle2D.Float JavaDoc awtRect = new Rectangle2D.Float JavaDoc( x,y,width,height );
115                             stripper.addRegion( "" + j, awtRect );
116                         }
117                     }
118                     
119                     stripper.extractRegions( page );
120                     
121                     for( int j=0; j<annotations.size(); j++ )
122                     {
123                         PDAnnotation annot = (PDAnnotation)annotations.get( j );
124                         if( annot instanceof PDAnnotationLink )
125                         {
126                             PDAnnotationLink link = (PDAnnotationLink)annot;
127                             PDAction action = link.getAction();
128                             String JavaDoc urlText = stripper.getTextForRegion( "" + j );
129                             if( action instanceof PDActionURI )
130                             {
131                                 PDActionURI uri = (PDActionURI)action;
132                                 System.out.println( "Page " + (i+1) +":'" + urlText + "'=" + uri.getURI() );
133                             }
134                         }
135                     }
136                 }
137             }
138         }
139         finally
140         {
141             if( doc != null )
142             {
143                 doc.close();
144             }
145         }
146     }
147
148     /**
149      * This will print out a message telling how to use this example.
150      */

151     private static void usage()
152     {
153         System.err.println( "usage: " + PrintURLs.class.getName() + " <input-file>" );
154     }
155 }
Popular Tags