001: /**
002: * Copyright (c) 2003-2004, www.pdfbox.org
003: * All rights reserved.
004: *
005: * Redistribution and use in source and binary forms, with or without
006: * modification, are permitted provided that the following conditions are met:
007: *
008: * 1. Redistributions of source code must retain the above copyright notice,
009: * this list of conditions and the following disclaimer.
010: * 2. Redistributions in binary form must reproduce the above copyright notice,
011: * this list of conditions and the following disclaimer in the documentation
012: * and/or other materials provided with the distribution.
013: * 3. Neither the name of pdfbox; nor the names of its
014: * contributors may be used to endorse or promote products derived from this
015: * software without specific prior written permission.
016: *
017: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
018: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
019: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
020: * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
021: * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
022: * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
023: * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
024: * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
025: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
026: * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
027: *
028: * http://www.pdfbox.org
029: *
030: */package org.pdfbox.examples.persistence;
031:
032: import java.io.IOException;
033:
034: import java.util.Iterator;
035:
036: import org.pdfbox.cos.COSBase;
037: import org.pdfbox.cos.COSObject;
038: import org.pdfbox.cos.COSStream;
039:
040: import org.pdfbox.pdmodel.PDDocument;
041:
042: import org.pdfbox.exceptions.COSVisitorException;
043:
044: import org.pdfbox.exceptions.InvalidPasswordException;
045:
046: /**
047: * load document and write with all streams decoded.
048: *
049: * @author Michael Traut
050: * @version $Revision: 1.8 $
051: */
052: public class WriteDecodedDoc {
053:
054: /**
055: * Constructor.
056: */
057: public WriteDecodedDoc() {
058: super ();
059: }
060:
061: /**
062: * This will perform the document reading, decoding and writing.
063: *
064: * @param in The filename used for input.
065: * @param out The filename used for output.
066: *
067: * @throws IOException If there is an error parsing the document.
068: * @throws COSVisitorException If there is an error while copying the document.
069: */
070: public void doIt(String in, String out) throws IOException,
071: COSVisitorException {
072: PDDocument doc = null;
073: try {
074: doc = PDDocument.load(in);
075: if (doc.isEncrypted()) {
076: try {
077: doc.decrypt("");
078: } catch (InvalidPasswordException e) {
079: System.err
080: .println("Error: The document is encrypted.");
081: } catch (org.pdfbox.exceptions.CryptographyException e) {
082: e.printStackTrace();
083: }
084: }
085:
086: for (Iterator i = doc.getDocument().getObjects().iterator(); i
087: .hasNext();) {
088: COSBase base = ((COSObject) i.next()).getObject();
089: if (base instanceof COSStream) {
090: // just kill the filters
091: COSStream cosStream = (COSStream) base;
092: cosStream.getUnfilteredStream();
093: cosStream.setFilters(null);
094: }
095: }
096: doc.save(out);
097: } finally {
098: if (doc != null) {
099: doc.close();
100: }
101: }
102: }
103:
104: /**
105: * This will write a PDF document with completely decoded streams.
106: * <br />
107: * see usage() for commandline
108: *
109: * @param args command line arguments
110: */
111: public static void main(String[] args) {
112: WriteDecodedDoc app = new WriteDecodedDoc();
113: try {
114: if (args.length != 2) {
115: app.usage();
116: } else {
117: app.doIt(args[0], args[1]);
118: }
119: } catch (Exception e) {
120: e.printStackTrace();
121: }
122: }
123:
124: /**
125: * This will print out a message telling how to use this example.
126: */
127: private void usage() {
128: System.err.println("usage: " + this .getClass().getName()
129: + " <input-file> <output-file>");
130: }
131: }
|