KickJava   Java API By Example, From Geeks To Geeks.

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


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.util.List JavaDoc;
34
35 import org.pdfbox.pdmodel.PDDocument;
36 import org.pdfbox.pdmodel.PDPage;
37
38 import org.pdfbox.pdmodel.interactive.action.type.PDAction;
39 import org.pdfbox.pdmodel.interactive.action.type.PDActionURI;
40 import org.pdfbox.pdmodel.interactive.annotation.PDAnnotation;
41 import org.pdfbox.pdmodel.interactive.annotation.PDAnnotationLink;
42
43
44 /**
45  * This is an example of how to replace a URL in a PDF document. This
46  * will only replace the URL that the text refers to and not the text
47  * itself.
48  *
49  * @author <a HREF="mailto:ben@benlitchfield.com">Ben Litchfield</a>
50  * @version $Revision: 1.2 $
51  */

52 public class ReplaceURLs
53 {
54     /**
55      * Constructor.
56      */

57     private ReplaceURLs()
58     {
59         //utility class
60
}
61
62     /**
63      * This will read in a document and replace all of the urls with
64      * http://www.pdfbox.org.
65      * <br />
66      * see usage() for commandline
67      *
68      * @param args Command line arguments.
69      *
70      * @throws Exception If there is an error during the process.
71      */

72     public static void main(String JavaDoc[] args) throws Exception JavaDoc
73     {
74         PDDocument doc = null;
75         try
76         {
77             if( args.length != 2 )
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                     PDPage page = (PDPage)allPages.get( i );
88                     List JavaDoc annotations = page.getAnnotations();
89                     
90                     for( int j=0; j<annotations.size(); j++ )
91                     {
92                         PDAnnotation annot = (PDAnnotation)annotations.get( j );
93                         if( annot instanceof PDAnnotationLink )
94                         {
95                             PDAnnotationLink link = (PDAnnotationLink)annot;
96                             PDAction action = link.getAction();
97                             if( action instanceof PDActionURI )
98                             {
99                                 PDActionURI uri = (PDActionURI)action;
100                                 String JavaDoc oldURI = uri.getURI();
101                                 String JavaDoc newURI = "http://www.pdfbox.org";
102                                 System.out.println( "Page " + (i+1) +": Replacing " + oldURI + " with " + newURI );
103                                 uri.setURI( newURI );
104                             }
105                         }
106                     }
107                 }
108                 doc.save( args[1] );
109             }
110         }
111         finally
112         {
113             if( doc != null )
114             {
115                 doc.close();
116             }
117         }
118     }
119
120     /**
121      * This will print out a message telling how to use this example.
122      */

123     private static void usage()
124     {
125         System.err.println( "usage: " + ReplaceURLs.class.getName() + " <input-file> <output-file>" );
126     }
127 }
Popular Tags