1 31 package org.pdfbox.examples.persistence; 32 33 import java.io.IOException ; 34 35 import org.pdfbox.cos.COSDocument; 36 37 38 39 import org.pdfbox.pdfparser.PDFParser; 40 41 import org.pdfbox.pdfwriter.COSWriter; 42 import org.pdfbox.exceptions.COSVisitorException; 43 44 51 public class CopyDoc 52 { 53 56 public CopyDoc() 57 { 58 super(); 59 } 60 61 70 public void doIt(String in, String out) throws IOException , COSVisitorException 71 { 72 java.io.InputStream is = null; 73 java.io.OutputStream os = null; 74 COSWriter writer = null; 75 try 76 { 77 is = new java.io.FileInputStream (in); 78 PDFParser parser = new PDFParser(is); 79 parser.parse(); 80 81 COSDocument doc = parser.getDocument(); 82 83 os = new java.io.FileOutputStream (out); 84 writer = new COSWriter(os); 85 86 writer.write(doc); 87 88 } 89 finally 90 { 91 if( is != null ) 92 { 93 is.close(); 94 } 95 if( os != null ) 96 { 97 os.close(); 98 } 99 if( writer != null ) 100 { 101 writer.close(); 102 } 103 } 104 } 105 106 113 public static void main(String [] args) 114 { 115 CopyDoc app = new CopyDoc(); 116 try 117 { 118 if( args.length != 2 ) 119 { 120 app.usage(); 121 } 122 else 123 { 124 app.doIt( args[0], args[1]); 125 } 126 } 127 catch (Exception e) 128 { 129 e.printStackTrace(); 130 } 131 } 132 133 136 private void usage() 137 { 138 System.err.println( "usage: " + this.getClass().getName() + " <input-file> <output-file>" ); 139 } 140 } | Popular Tags |