001: /**
002: * Copyright (c) 2003, 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 org.pdfbox.cos.COSDocument;
035:
036: import org.pdfbox.pdfparser.PDFParser;
037:
038: import org.pdfbox.pdfwriter.COSWriter;
039: import org.pdfbox.exceptions.COSVisitorException;
040:
041: /**
042: * This is an example used to copy a documents contents from a source doc to destination doc
043: * via an in-memory document representation.
044: *
045: * @author Michael Traut
046: * @version $Revision: 1.7 $
047: */
048: public class CopyDoc {
049: /**
050: * Constructor.
051: */
052: public CopyDoc() {
053: super ();
054: }
055:
056: /**
057: * This will perform the document copy.
058: *
059: * @param in The filename used for input.
060: * @param out The filename used for output.
061: *
062: * @throws IOException If there is an error parsing the document.
063: * @throws COSVisitorException If there is an error while copying the document.
064: */
065: public void doIt(String in, String out) throws IOException,
066: COSVisitorException {
067: java.io.InputStream is = null;
068: java.io.OutputStream os = null;
069: COSWriter writer = null;
070: try {
071: is = new java.io.FileInputStream(in);
072: PDFParser parser = new PDFParser(is);
073: parser.parse();
074:
075: COSDocument doc = parser.getDocument();
076:
077: os = new java.io.FileOutputStream(out);
078: writer = new COSWriter(os);
079:
080: writer.write(doc);
081:
082: } finally {
083: if (is != null) {
084: is.close();
085: }
086: if (os != null) {
087: os.close();
088: }
089: if (writer != null) {
090: writer.close();
091: }
092: }
093: }
094:
095: /**
096: * This will copy a PDF document.
097: * <br />
098: * see usage() for commandline
099: *
100: * @param args command line arguments
101: */
102: public static void main(String[] args) {
103: CopyDoc app = new CopyDoc();
104: try {
105: if (args.length != 2) {
106: app.usage();
107: } else {
108: app.doIt(args[0], args[1]);
109: }
110: } catch (Exception e) {
111: e.printStackTrace();
112: }
113: }
114:
115: /**
116: * This will print out a message telling how to use this example.
117: */
118: private void usage() {
119: System.err.println("usage: " + this .getClass().getName()
120: + " <input-file> <output-file>");
121: }
122: }
|