1 /* 2 * Licensed to the Apache Software Foundation (ASF) under one or more 3 * contributor license agreements. See the NOTICE file distributed with 4 * this work for additional information regarding copyright ownership. 5 * The ASF licenses this file to You under the Apache License, Version 2.0 6 * (the "License"); you may not use this file except in compliance with 7 * the License. You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 package org.apache.pdfbox.examples.persistence; 18 19 import java.io.IOException; 20 21 import org.apache.pdfbox.cos.COSDocument; 22 23 24 25 import org.apache.pdfbox.pdfparser.PDFParser; 26 27 import org.apache.pdfbox.pdfwriter.COSWriter; 28 import org.apache.pdfbox.exceptions.COSVisitorException; 29 30 /** 31 * This is an example used to copy a documents contents from a source doc to destination doc 32 * via an in-memory document representation. 33 * 34 * @author Michael Traut 35 * @version $Revision: 1.7 $ 36 */ 37 public class CopyDoc 38 { 39 /** 40 * Constructor. 41 */ 42 public CopyDoc() 43 { 44 super(); 45 } 46 47 /** 48 * This will perform the document copy. 49 * 50 * @param in The filename used for input. 51 * @param out The filename used for output. 52 * 53 * @throws IOException If there is an error parsing the document. 54 * @throws COSVisitorException If there is an error while copying the document. 55 */ 56 public void doIt(String in, String out) throws IOException, COSVisitorException 57 { 58 java.io.InputStream is = null; 59 java.io.OutputStream os = null; 60 COSWriter writer = null; 61 try 62 { 63 is = new java.io.FileInputStream(in); 64 PDFParser parser = new PDFParser(is); 65 parser.parse(); 66 67 COSDocument doc = parser.getDocument(); 68 69 os = new java.io.FileOutputStream(out); 70 writer = new COSWriter(os); 71 72 writer.write(doc); 73 74 } 75 finally 76 { 77 if( is != null ) 78 { 79 is.close(); 80 } 81 if( os != null ) 82 { 83 os.close(); 84 } 85 if( writer != null ) 86 { 87 writer.close(); 88 } 89 } 90 } 91 92 /** 93 * This will copy a PDF document. 94 * <br /> 95 * see usage() for commandline 96 * 97 * @param args command line arguments 98 */ 99 public static void main(String[] args) 100 { 101 CopyDoc app = new CopyDoc(); 102 try 103 { 104 if( args.length != 2 ) 105 { 106 app.usage(); 107 } 108 else 109 { 110 app.doIt( args[0], args[1]); 111 } 112 } 113 catch (Exception e) 114 { 115 e.printStackTrace(); 116 } 117 } 118 119 /** 120 * This will print out a message telling how to use this example. 121 */ 122 private void usage() 123 { 124 System.err.println( "usage: " + this.getClass().getName() + " <input-file> <output-file>" ); 125 } 126 }