1 31 package org.pdfbox.examples.persistence; 32 33 import java.io.IOException ; 34 35 import java.util.Iterator ; 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 53 public class WriteDecodedDoc 54 { 55 56 59 public WriteDecodedDoc() 60 { 61 super(); 62 } 63 64 73 public void doIt(String in, String out) throws IOException , 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 i = doc.getDocument().getObjects().iterator(); i.hasNext();) 96 { 97 COSBase base = ((COSObject) i.next()).getObject(); 98 if (base instanceof COSStream) 99 { 100 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 124 public static void main(String [] 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 e) 139 { 140 e.printStackTrace(); 141 } 142 } 143 144 147 private void usage() 148 { 149 System.err.println( "usage: " + this.getClass().getName() + " <input-file> <output-file>" ); 150 } 151 } | Popular Tags |