KickJava   Java API By Example, From Geeks To Geeks.

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


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.io.IOException JavaDoc;
34 import java.io.OutputStream JavaDoc;
35 import java.util.List JavaDoc;
36
37 import org.pdfbox.cos.COSArray;
38 import org.pdfbox.cos.COSString;
39 import org.pdfbox.exceptions.COSVisitorException;
40
41 import org.pdfbox.pdfparser.PDFStreamParser;
42 import org.pdfbox.pdfwriter.ContentStreamWriter;
43 import org.pdfbox.pdmodel.PDDocument;
44 import org.pdfbox.pdmodel.PDPage;
45
46 import org.pdfbox.pdmodel.common.PDStream;
47
48 import org.pdfbox.util.PDFOperator;
49
50
51 /**
52  * This is an example that will replace a string in a PDF with a new one.
53  *
54  * The example is taken from the pdf file format specification.
55  *
56  * @author <a HREF="mailto:ben@benlitchfield.com">Ben Litchfield</a>
57  * @version $Revision: 1.3 $
58  */

59 public class ReplaceString
60 {
61     /**
62      * Constructor.
63      */

64     public ReplaceString()
65     {
66         super();
67     }
68
69     /**
70      * Locate a string in a PDF and replace it with a new string.
71      *
72      * @param inputFile The PDF to open.
73      * @param outputFile The PDF to write to.
74      * @param strToFind The string to find in the PDF document.
75      * @param message The message to write in the file.
76      *
77      * @throws IOException If there is an error writing the data.
78      * @throws COSVisitorException If there is an error writing the PDF.
79      */

80     public void doIt( String JavaDoc inputFile, String JavaDoc outputFile, String JavaDoc strToFind, String JavaDoc message)
81         throws IOException JavaDoc, COSVisitorException
82     {
83         // the document
84
PDDocument doc = null;
85         try
86         {
87             doc = PDDocument.load( inputFile );
88             List JavaDoc pages = doc.getDocumentCatalog().getAllPages();
89             for( int i=0; i<pages.size(); i++ )
90             {
91                 PDPage page = (PDPage)pages.get( i );
92                 PDStream contents = page.getContents();
93                 PDFStreamParser parser = new PDFStreamParser(contents.getStream() );
94                 parser.parse();
95                 List JavaDoc tokens = parser.getTokens();
96                 for( int j=0; j<tokens.size(); j++ )
97                 {
98                     Object JavaDoc next = tokens.get( j );
99                     if( next instanceof PDFOperator )
100                     {
101                         PDFOperator op = (PDFOperator)next;
102                         //Tj and TJ are the two operators that display
103
//strings in a PDF
104
if( op.getOperation().equals( "Tj" ) )
105                         {
106                             //Tj takes one operator and that is the string
107
//to display so lets update that operator
108
COSString previous = (COSString)tokens.get( j-1 );
109                             String JavaDoc string = previous.getString();
110                             string = string.replaceFirst( strToFind, message );
111                             previous.reset();
112                             previous.append( string.getBytes() );
113                         }
114                         else if( op.getOperation().equals( "TJ" ) )
115                         {
116                             COSArray previous = (COSArray)tokens.get( j-1 );
117                             for( int k=0; k<previous.size(); k++ )
118                             {
119                                 Object JavaDoc arrElement = previous.getObject( k );
120                                 if( arrElement instanceof COSString )
121                                 {
122                                     COSString cosString = (COSString)arrElement;
123                                     String JavaDoc string = cosString.getString();
124                                     string = string.replaceFirst( strToFind, message );
125                                     cosString.reset();
126                                     cosString.append( string.getBytes() );
127                                 }
128                             }
129                         }
130                     }
131                 }
132                 //now that the tokens are updated we will replace the
133
//page content stream.
134
PDStream updatedStream = new PDStream(doc);
135                 OutputStream JavaDoc out = updatedStream.createOutputStream();
136                 ContentStreamWriter tokenWriter = new ContentStreamWriter(out);
137                 tokenWriter.writeTokens( tokens );
138                 page.setContents( updatedStream );
139             }
140             doc.save( outputFile );
141         }
142         finally
143         {
144             if( doc != null )
145             {
146                 doc.close();
147             }
148         }
149     }
150
151     /**
152      * This will open a PDF and replace a string if it finds it.
153      * <br />
154      * see usage() for commandline
155      *
156      * @param args Command line arguments.
157      */

158     public static void main(String JavaDoc[] args)
159     {
160         ReplaceString app = new ReplaceString();
161         try
162         {
163             if( args.length != 4 )
164             {
165                 app.usage();
166             }
167             else
168             {
169                 app.doIt( args[0], args[1], args[2], args[3] );
170             }
171         }
172         catch (Exception JavaDoc e)
173         {
174             e.printStackTrace();
175         }
176     }
177
178     /**
179      * This will print out a message telling how to use this example.
180      */

181     private void usage()
182     {
183         System.err.println( "usage: " + this.getClass().getName() +
184             " <input-file> <output-file> <search-string> <Message>" );
185     }
186 }
Popular Tags