KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > pdfbox > examples > persistence > WriteDecodedDoc


1 /**
2  * Copyright (c) 2003-2004, 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.persistence;
32
33 import java.io.IOException JavaDoc;
34
35 import java.util.Iterator JavaDoc;
36
37 import org.pdfbox.cos.COSBase;
38 import org.pdfbox.cos.COSObject;
39 import org.pdfbox.cos.COSStream;
40
41 import org.pdfbox.pdmodel.PDDocument;
42
43 import org.pdfbox.exceptions.COSVisitorException;
44
45 import org.pdfbox.exceptions.InvalidPasswordException;
46
47 /**
48  * load document and write with all streams decoded.
49  *
50  * @author Michael Traut
51  * @version $Revision: 1.8 $
52  */

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

59     public WriteDecodedDoc()
60     {
61         super();
62     }
63
64     /**
65      * This will perform the document reading, decoding and writing.
66      *
67      * @param in The filename used for input.
68      * @param out The filename used for output.
69      *
70      * @throws IOException If there is an error parsing the document.
71      * @throws COSVisitorException If there is an error while copying the document.
72      */

73     public void doIt(String JavaDoc in, String JavaDoc out) throws IOException JavaDoc, COSVisitorException
74     {
75         PDDocument doc = null;
76         try
77         {
78             doc = PDDocument.load( in );
79             if( doc.isEncrypted() )
80             {
81                 try
82                 {
83                     doc.decrypt( "" );
84                 }
85                 catch( InvalidPasswordException e )
86                 {
87                     System.err.println( "Error: The document is encrypted." );
88                 }
89                 catch( org.pdfbox.exceptions.CryptographyException e )
90                 {
91                     e.printStackTrace();
92                 }
93             }
94
95             for (Iterator JavaDoc i = doc.getDocument().getObjects().iterator(); i.hasNext();)
96             {
97                 COSBase base = ((COSObject) i.next()).getObject();
98                 if (base instanceof COSStream)
99                 {
100                     // just kill the filters
101
COSStream cosStream = (COSStream)base;
102                     cosStream.getUnfilteredStream();
103                     cosStream.setFilters(null);
104                 }
105             }
106             doc.save( out );
107         }
108         finally
109         {
110             if( doc != null )
111             {
112                 doc.close();
113             }
114         }
115     }
116
117     /**
118      * This will write a PDF document with completely decoded streams.
119      * <br />
120      * see usage() for commandline
121      *
122      * @param args command line arguments
123      */

124     public static void main(String JavaDoc[] args)
125     {
126         WriteDecodedDoc app = new WriteDecodedDoc();
127         try
128         {
129             if( args.length != 2 )
130             {
131                 app.usage();
132             }
133             else
134             {
135                 app.doIt( args[0], args[1]);
136             }
137         }
138         catch (Exception JavaDoc e)
139         {
140             e.printStackTrace();
141         }
142     }
143
144     /**
145      * This will print out a message telling how to use this example.
146      */

147     private void usage()
148     {
149         System.err.println( "usage: " + this.getClass().getName() + " <input-file> <output-file>" );
150     }
151 }
Popular Tags