Home » pdfbox-1.1.0-src » org.apache.pdfbox.examples.persistence » [javadoc | source]

    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 java.util.Iterator;
   22   
   23   import org.apache.pdfbox.cos.COSBase;
   24   import org.apache.pdfbox.cos.COSObject;
   25   import org.apache.pdfbox.cos.COSStream;
   26   
   27   import org.apache.pdfbox.pdmodel.PDDocument;
   28   
   29   import org.apache.pdfbox.exceptions.COSVisitorException;
   30   
   31   import org.apache.pdfbox.exceptions.InvalidPasswordException;
   32   
   33   /**
   34    * load document and write with all streams decoded.
   35    *
   36    * @author Michael Traut
   37    * @version $Revision: 1.8 $
   38    */
   39   public class WriteDecodedDoc
   40   {
   41   
   42       /**
   43        * Constructor.
   44        */
   45       public WriteDecodedDoc()
   46       {
   47           super();
   48       }
   49   
   50       /**
   51        * This will perform the document reading, decoding and writing.
   52        *
   53        * @param in The filename used for input.
   54        * @param out The filename used for output.
   55        *
   56        * @throws IOException If there is an error parsing the document.
   57        * @throws COSVisitorException If there is an error while copying the document.
   58        */
   59       public void doIt(String in, String out) throws IOException, COSVisitorException
   60       {
   61           PDDocument doc = null;
   62           try
   63           {
   64               doc = PDDocument.load( in );
   65               if( doc.isEncrypted() )
   66               {
   67                   try
   68                   {
   69                       doc.decrypt( "" );
   70                   }
   71                   catch( InvalidPasswordException e )
   72                   {
   73                       System.err.println( "Error: The document is encrypted." );
   74                   }
   75                   catch( org.apache.pdfbox.exceptions.CryptographyException e )
   76                   {
   77                       e.printStackTrace();
   78                   }
   79               }
   80   
   81               for (Iterator i = doc.getDocument().getObjects().iterator(); i.hasNext();)
   82               {
   83                   COSBase base = ((COSObject) i.next()).getObject();
   84                   if (base instanceof COSStream)
   85                   {
   86                       // just kill the filters
   87                       COSStream cosStream = (COSStream)base;
   88                       cosStream.getUnfilteredStream();
   89                       cosStream.setFilters(null);
   90                   }
   91               }
   92               doc.save( out );
   93           }
   94           finally
   95           {
   96               if( doc != null )
   97               {
   98                   doc.close();
   99               }
  100           }
  101       }
  102   
  103       /**
  104        * This will write a PDF document with completely decoded streams.
  105        * <br />
  106        * see usage() for commandline
  107        *
  108        * @param args command line arguments
  109        */
  110       public static void main(String[] args)
  111       {
  112           WriteDecodedDoc app = new WriteDecodedDoc();
  113           try
  114           {
  115               if( args.length != 2 )
  116               {
  117                   app.usage();
  118               }
  119               else
  120               {
  121                   app.doIt( args[0], args[1]);
  122               }
  123           }
  124           catch (Exception e)
  125           {
  126               e.printStackTrace();
  127           }
  128       }
  129   
  130       /**
  131        * This will print out a message telling how to use this example.
  132        */
  133       private void usage()
  134       {
  135           System.err.println( "usage: " + this.getClass().getName() + " <input-file> <output-file>" );
  136       }
  137   }

Home » pdfbox-1.1.0-src » org.apache.pdfbox.examples.persistence » [javadoc | source]