From 6025b6016517c6d898d8957d1d7e03ba71431912 Mon Sep 17 00:00:00 2001 From: tknall Date: Fri, 1 Dec 2006 12:20:24 +0000 Subject: Initial import of release 2.2. git-svn-id: https://joinup.ec.europa.eu/svn/pdf-as/trunk@4 7b5415b0-85f9-ee4d-85bd-d5d0c3b42d1c --- .../examples/persistence/AppendAndFillDoc.java | 282 ++++++++++++++++ .../org/pdfbox/examples/persistence/AppendDoc.java | 357 +++++++++++++++++++++ .../org/pdfbox/examples/persistence/CopyDoc.java | 140 ++++++++ .../org/pdfbox/examples/persistence/FieldsDoc.java | 177 ++++++++++ .../examples/persistence/WriteDecodedDoc.java | 151 +++++++++ .../org/pdfbox/examples/persistence/package.html | 9 + 6 files changed, 1116 insertions(+) create mode 100644 src/main/java/org/pdfbox/examples/persistence/AppendAndFillDoc.java create mode 100644 src/main/java/org/pdfbox/examples/persistence/AppendDoc.java create mode 100644 src/main/java/org/pdfbox/examples/persistence/CopyDoc.java create mode 100644 src/main/java/org/pdfbox/examples/persistence/FieldsDoc.java create mode 100644 src/main/java/org/pdfbox/examples/persistence/WriteDecodedDoc.java create mode 100644 src/main/java/org/pdfbox/examples/persistence/package.html (limited to 'src/main/java/org/pdfbox/examples/persistence') diff --git a/src/main/java/org/pdfbox/examples/persistence/AppendAndFillDoc.java b/src/main/java/org/pdfbox/examples/persistence/AppendAndFillDoc.java new file mode 100644 index 0000000..01b8cf6 --- /dev/null +++ b/src/main/java/org/pdfbox/examples/persistence/AppendAndFillDoc.java @@ -0,0 +1,282 @@ +/** + * Copyright (c) 2003, www.pdfbox.org + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * 3. Neither the name of pdfbox; nor the names of its + * contributors may be used to endorse or promote products derived from this + * software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + * http://www.pdfbox.org + * + */ +package org.pdfbox.examples.persistence; + +import java.io.IOException; +import java.io.OutputStream; +import java.io.FileOutputStream; +import java.io.InputStream; +import java.io.FileInputStream; + +import java.util.Iterator; + +import org.pdfbox.pdfparser.PDFParser; + +import org.pdfbox.pdfwriter.COSWriter; + +import org.pdfbox.cos.COSDocument; +import org.pdfbox.cos.COSString; +import org.pdfbox.cos.COSBase; +import org.pdfbox.cos.COSDictionary; +import org.pdfbox.cos.COSObject; +import org.pdfbox.cos.COSName; +import org.pdfbox.cos.COSArray; +import org.pdfbox.cos.COSNumber; +import org.pdfbox.cos.COSInteger; + +import org.pdfbox.exceptions.COSVisitorException; + +/** + * This concatenates two documents with fields and fills the fields in the two templates using different + * values. + * + * @author Michael Traut + * @version $Revision: 1.6 $ + */ +public class AppendAndFillDoc +{ + /** + * Constructor. + */ + public AppendAndFillDoc() + { + super(); + } + + /** + * Append all pages from source to destination. + * + * todo: this method will go to the pdfmodel package one day + * + * @param destination the document to receive the pages + * @param source the document originating the new pages + * + */ + public void appendDocument(COSDocument destination, COSDocument source) + { + COSDictionary pages2 = getPages(source); + COSArray kids = (COSArray)pages2.getItem(COSName.getPDFName("Kids")); + for (Iterator i = kids.iterator(); i.hasNext();) + { + COSDictionary page = (COSDictionary)((COSObject)i.next()).getObject(); + appendPage(destination, page); + } + } + /** + * append a page dict to destination. + * + * todo: this method will go to the pdfmodel package one day + * + * @param destination the document to receive the page + * @param page the page to append to the document + * + */ + public void appendPage(COSDocument destination, COSDictionary page) + { + // get the parent object (pages dictionary) + COSDictionary pages = getPages(destination); + // and set in the page object + page.setItem(COSName.PARENT, pages); + // add new page to kids entry in the pages dictionary + COSArray kids = (COSArray) pages.getItem(COSName.getPDFName("Kids")); + kids.add(page); + // and increase count + COSNumber count = (COSNumber) pages.getItem(COSName.COUNT); + pages.setItem(COSName.COUNT, new COSInteger(count.intValue() + 1)); + } + /** + * concat two pdf documents and fill fields in both templates + * this is a bit tricky as one has to rename the fields if we use the same template two times. + * here we don't user a clever algorithm to create dynamic fieldnames - this is left to the user.. + * + * @param in1 The first template file + * @param in2 The second template file + * @param out The created fiel with all pages from document one and document two + * @param name1 The name of the PDF field (FDF field) in the first template + * @param value1 The value to be used for the field in the first template + * @param name2 The name of the PDF field (FDF field) in the second template + * @param value2 The value to be used for the field in the second template + * + * @throws IOException If there is an error writing the data. + * @throws COSVisitorException If there is an error generating the PDF document. + */ + public void doIt( String in1, + String in2, + String out, + String name1, + String value1, + String name2, + String value2) throws IOException, COSVisitorException + { + COSDocument doc1 = null; + COSDocument doc2 = null; + InputStream is1 = null; + InputStream is2 = null; + PDFParser parser1 = null; + PDFParser parser2 = null; + + OutputStream os = null; + COSWriter writer = null; + try + { + is1 = new FileInputStream(in1); + parser1 = new PDFParser(is1); + parser1.parse(); + doc1 = parser1.getDocument(); + + is2 = new FileInputStream(in2); + parser2 = new PDFParser(is2); + parser2.parse(); + doc2 = parser2.getDocument(); + + setField(doc1, "doc1", new COSString(name1), new COSString(value1)); + setField(doc2, "doc2", new COSString(name2), new COSString(value2)); + + appendDocument(doc1, doc2); + + os = new FileOutputStream(out); + writer = new COSWriter(os); + writer.write(doc1); + } + finally + { + is1.close(); + doc1.close(); + + is2.close(); + doc2.close(); + + os.close(); + writer.close(); + } + } + + /** + * Lookup the pages dictionary in a document. + * + * todo: this method will go to the pdfmodel package one day + * + * @param doc the document where the pages dict is searched + * + * @return The Pages dictionary. + */ + public COSDictionary getPages(COSDocument doc) + { + // todo should access via catalog instead! + for (Iterator i = doc.getObjects().iterator(); i.hasNext();) + { + COSObject obj = (COSObject) i.next(); + COSBase base = obj.getObject(); + if (base instanceof COSDictionary) + { + COSDictionary dict = (COSDictionary) base; + COSBase type = dict.getItem(COSName.TYPE); + if (type != null && type.equals(COSName.getPDFName("Pages"))) + { + return dict; + } + } + } + return null; + } + /** + * This will concat two pdf documents and fill fields in both. + *
+ * see usage() for commandline + * + * @param args command line arguments + */ + public static void main(String[] args) + { + AppendAndFillDoc app = new AppendAndFillDoc(); + try + { + if( args.length != 7 ) + { + app.usage(); + } + else + { + app.doIt( args[0], args[1], args[2], args[3], args[4], args[5], args[6]); + } + } + catch (Exception e) + { + e.printStackTrace(); + } + } + /** + * lookup and fill the field. + * + * todo: this method will go to the pdfmodel package one day + * + * @param doc the document where the field resides + * @param prefix a prefix to use to make the field name unique in the new document + * @param name the name of the PDF Annotation field + * @param value The desired value to be used for the field + * + */ + public void setField(COSDocument doc, String prefix, COSString name, COSString value) + { + for (Iterator i = doc.getObjects().iterator(); i.hasNext();) + { + COSObject obj = (COSObject) i.next(); + COSBase base = obj.getObject(); + if (base instanceof COSDictionary) + { + COSDictionary dict = (COSDictionary) base; + COSBase type = dict.getItem(COSName.TYPE); + if (type != null && type.equals(COSName.getPDFName("Annot"))) + { + COSBase subtype = dict.getItem(COSName.getPDFName("Subtype")); + if (subtype != null && subtype.equals(COSName.getPDFName("Widget"))) + { + // we found the field + COSBase fname = dict.getItem(COSName.getPDFName("T")); + if (fname != null && fname.equals(name)) + { + dict.setItem(COSName.getPDFName("V"), value); + dict.setItem(COSName.getPDFName("T"), new COSString(prefix + name.getString())); + } + } + } + } + } + } + /** + * This will print out a message telling how to use this example. + */ + private void usage() + { + System.err.println( "usage: " + this.getClass().getName() + + " " ); + } +} \ No newline at end of file diff --git a/src/main/java/org/pdfbox/examples/persistence/AppendDoc.java b/src/main/java/org/pdfbox/examples/persistence/AppendDoc.java new file mode 100644 index 0000000..68e254c --- /dev/null +++ b/src/main/java/org/pdfbox/examples/persistence/AppendDoc.java @@ -0,0 +1,357 @@ +/** + * Copyright (c) 2003-2004, www.pdfbox.org + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * 3. Neither the name of pdfbox; nor the names of its + * contributors may be used to endorse or promote products derived from this + * software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + * http://www.pdfbox.org + * + */ +package org.pdfbox.examples.persistence; + +import java.io.IOException; + +import java.util.HashMap; +import java.util.Iterator; +import java.util.List; +import java.util.Map; + +import org.pdfbox.cos.COSBase; +import org.pdfbox.cos.COSDictionary; +import org.pdfbox.cos.COSName; +import org.pdfbox.cos.COSArray; +import org.pdfbox.cos.COSNumber; +import org.pdfbox.cos.COSInteger; +import org.pdfbox.cos.COSObject; +import org.pdfbox.cos.COSStream; + +import org.pdfbox.pdmodel.PDDocument; +import org.pdfbox.pdmodel.PDDocumentInformation; +import org.pdfbox.pdmodel.PDPage; + +import org.pdfbox.pdmodel.common.PDStream; + +import org.pdfbox.exceptions.COSVisitorException; + +/** + * This example concatenates two documents and writes the result. + * + * @author Michael Traut + * @version $Revision: 1.17 $ + */ +public class AppendDoc +{ + /** + * Constructor. + */ + public AppendDoc() + { + super(); + } + /** + * append all pages from source to destination. + * + * todo: this method will go to the pdfmodel package one day + * + * @param destination the document to receive the pages + * @param source the document originating the new pages + * + * @throws IOException If there is an error accessing data from either document. + */ + public void appendDocument(PDDocument destination, PDDocument source) throws IOException + { + if( destination.isEncrypted() ) + { + throw new IOException( "Error: destination PDF is encrypted, can't append encrypted PDF documents." ); + } + if( source.isEncrypted() ) + { + throw new IOException( "Error: destination PDF is encrypted, can't append encrypted PDF documents." ); + } + PDDocumentInformation destInfo = destination.getDocumentInformation(); + PDDocumentInformation srcInfo = source.getDocumentInformation(); + destInfo.getDictionary().mergeInto( srcInfo.getDictionary() ); + + COSDictionary destTrailer = destination.getDocument().getTrailer(); + COSDictionary destRoot = (COSDictionary)destTrailer.getDictionaryObject( COSName.ROOT ); + + COSDictionary srcTrailer = source.getDocument().getTrailer(); + COSDictionary srcRoot = (COSDictionary)srcTrailer.getDictionaryObject( COSName.ROOT ); + + COSName openAction = COSName.getPDFName( "OpenAction" ); + if( destRoot.getDictionaryObject( openAction ) == null ) + { + COSBase open = srcRoot.getDictionaryObject( openAction ); + if( open != null ) + { + destRoot.setItem( openAction, copyStreamsIntoDocument( destination, open ) ); + } + } + + COSName acroForm = COSName.getPDFName( "AcroForm" ); + COSDictionary destAcroForm = (COSDictionary)destRoot.getDictionaryObject( acroForm ); + COSDictionary srcAcroForm = (COSDictionary)srcRoot.getDictionaryObject( acroForm ); + if( srcAcroForm != null ) + { + if( destAcroForm == null ) + { + destRoot.setItem( acroForm, copyStreamsIntoDocument( destination, srcAcroForm ) ); + } + else + { + //****************need to do proper merge*************** + //destAcroForm.addAll( (COSArray)copyStreamsIntoDocument( destination, srcAcroForm ) ); + } + } + + COSName threads = COSName.getPDFName( "Threads" ); + COSArray destThreads = (COSArray)destRoot.getDictionaryObject( threads ); + COSArray srcThreads = (COSArray)srcRoot.getDictionaryObject( threads ); + if( srcThreads != null ) + { + if( destThreads == null ) + { + destRoot.setItem( threads, copyStreamsIntoDocument( destination, srcThreads ) ); + } + else + { + destThreads.addAll( (COSArray)copyStreamsIntoDocument( destination, srcThreads ) ); + } + } + + COSName names = COSName.getPDFName( "Names" ); + COSDictionary destNames = (COSDictionary)destRoot.getDictionaryObject( names ); + COSDictionary srcNames = (COSDictionary)srcRoot.getDictionaryObject( names ); + if( srcNames != null ) + { + if( destNames == null ) + { + destRoot.setItem( names, copyStreamsIntoDocument( destination, srcNames ) ); + } + else + { + //warning, potential for collision here!! + destNames.mergeInto( (COSDictionary)copyStreamsIntoDocument( destination, srcNames ) ); + } + } + + COSName outlines = COSName.getPDFName( "Outlines" ); + COSDictionary destOutlines = (COSDictionary)destRoot.getDictionaryObject( outlines ); + COSDictionary srcOutlines = (COSDictionary)srcRoot.getDictionaryObject( outlines ); + if( srcOutlines != null && destOutlines == null ) + { + destRoot.setItem( outlines, copyStreamsIntoDocument( destination, srcOutlines ) ); + } + + COSName pagemode = COSName.getPDFName( "PageMode" ); + COSBase srcPageMode = srcRoot.getDictionaryObject( pagemode ); + if( srcOutlines != null && destOutlines == null ) + { + destRoot.setItem( pagemode, copyStreamsIntoDocument( destination, srcPageMode ) ); + } + + COSName pageLabels = COSName.getPDFName( "PageLabels" ); + COSDictionary destLabels = (COSDictionary)destRoot.getDictionaryObject( pageLabels ); + COSDictionary srcLabels = (COSDictionary)srcRoot.getDictionaryObject( pageLabels ); + if( srcLabels != null ) + { + int destPageCount = destination.getNumberOfPages(); + COSArray destNums = null; + if( destLabels == null ) + { + destLabels = new COSDictionary(); + destNums = new COSArray(); + destLabels.setItem( COSName.getPDFName( "Nums" ), destNums ); + destRoot.setItem( pageLabels, destLabels ); + } + else + { + destNums = (COSArray)destLabels.getDictionaryObject( COSName.getPDFName( "Nums" ) ); + } + COSArray srcNums = (COSArray)srcLabels.getDictionaryObject( COSName.getPDFName( "Nums" ) ); + for( int i=0; i + * see usage() for commandline + * + * @param args command line arguments + */ + public static void main(String[] args) + { + AppendDoc app = new AppendDoc(); + try + { + if( args.length != 3 ) + { + app.usage(); + } + else + { + app.doIt( args[0], args[1], args[2]); + } + } + catch( Exception e ) + { + e.printStackTrace(); + } + } + /** + * This will print out a message telling how to use this example. + */ + private void usage() + { + System.err.println( "usage: " + this.getClass().getName() + " " ); + } +} \ No newline at end of file diff --git a/src/main/java/org/pdfbox/examples/persistence/CopyDoc.java b/src/main/java/org/pdfbox/examples/persistence/CopyDoc.java new file mode 100644 index 0000000..a3b7e3f --- /dev/null +++ b/src/main/java/org/pdfbox/examples/persistence/CopyDoc.java @@ -0,0 +1,140 @@ +/** + * Copyright (c) 2003, www.pdfbox.org + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * 3. Neither the name of pdfbox; nor the names of its + * contributors may be used to endorse or promote products derived from this + * software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + * http://www.pdfbox.org + * + */ +package org.pdfbox.examples.persistence; + +import java.io.IOException; + +import org.pdfbox.cos.COSDocument; + + + +import org.pdfbox.pdfparser.PDFParser; + +import org.pdfbox.pdfwriter.COSWriter; +import org.pdfbox.exceptions.COSVisitorException; + +/** + * This is an example used to copy a documents contents from a source doc to destination doc + * via an in-memory document representation. + * + * @author Michael Traut + * @version $Revision: 1.7 $ + */ +public class CopyDoc +{ + /** + * Constructor. + */ + public CopyDoc() + { + super(); + } + + /** + * This will perform the document copy. + * + * @param in The filename used for input. + * @param out The filename used for output. + * + * @throws IOException If there is an error parsing the document. + * @throws COSVisitorException If there is an error while copying the document. + */ + public void doIt(String in, String out) throws IOException, COSVisitorException + { + java.io.InputStream is = null; + java.io.OutputStream os = null; + COSWriter writer = null; + try + { + is = new java.io.FileInputStream(in); + PDFParser parser = new PDFParser(is); + parser.parse(); + + COSDocument doc = parser.getDocument(); + + os = new java.io.FileOutputStream(out); + writer = new COSWriter(os); + + writer.write(doc); + + } + finally + { + if( is != null ) + { + is.close(); + } + if( os != null ) + { + os.close(); + } + if( writer != null ) + { + writer.close(); + } + } + } + + /** + * This will copy a PDF document. + *
+ * see usage() for commandline + * + * @param args command line arguments + */ + public static void main(String[] args) + { + CopyDoc app = new CopyDoc(); + try + { + if( args.length != 2 ) + { + app.usage(); + } + else + { + app.doIt( args[0], args[1]); + } + } + catch (Exception e) + { + e.printStackTrace(); + } + } + + /** + * This will print out a message telling how to use this example. + */ + private void usage() + { + System.err.println( "usage: " + this.getClass().getName() + " " ); + } +} \ No newline at end of file diff --git a/src/main/java/org/pdfbox/examples/persistence/FieldsDoc.java b/src/main/java/org/pdfbox/examples/persistence/FieldsDoc.java new file mode 100644 index 0000000..e506fe1 --- /dev/null +++ b/src/main/java/org/pdfbox/examples/persistence/FieldsDoc.java @@ -0,0 +1,177 @@ +/** + * Copyright (c) 2003, www.pdfbox.org + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * 3. Neither the name of pdfbox; nor the names of its + * contributors may be used to endorse or promote products derived from this + * software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + * http://www.pdfbox.org + * + */ +package org.pdfbox.examples.persistence; + +import java.io.IOException; +import java.io.OutputStream; +import java.io.FileOutputStream; + +import java.util.Iterator; + +import org.pdfbox.pdfparser.PDFParser; + +import org.pdfbox.pdfwriter.COSWriter; + +import org.pdfbox.cos.COSDocument; +import org.pdfbox.cos.COSString; +import org.pdfbox.cos.COSBase; +import org.pdfbox.cos.COSDictionary; +import org.pdfbox.cos.COSObject; +import org.pdfbox.cos.COSName; + +import org.pdfbox.exceptions.COSVisitorException; + +/** + * This example fills a field in a pdf document and writes it to new destination. + * + * @author Michael Traut + * @version $Revision: 1.5 $ + */ +public class FieldsDoc +{ + /** + * Constructor. + */ + public FieldsDoc() + { + super(); + } + /** + * fill a field in the pdf. + * + * @param in The template file + * @param out The file to write the PDF to. + * @param name The name of the PDF field (FDF field) + * @param value The value to be used for the field + * + * @throws IOException If there is an error writing the data. + * @throws COSVisitorException If there is an error generating the PDF document. + */ + public void doIt(String in, String out, String name, String value) throws IOException, COSVisitorException + { + java.io.InputStream is = null; + COSDocument doc = null; + OutputStream os = null; + COSWriter writer = null; + try + { + is = new java.io.FileInputStream(in); + PDFParser parser = new PDFParser(is); + parser.parse(); + + doc = parser.getDocument(); + + setField(doc, new COSString(name), new COSString(value)); + + os = new FileOutputStream(out); + writer = new COSWriter(os); + + writer.write(doc); + + } + finally + { + is.close(); + doc.close(); + os.close(); + writer.close(); + } + } + /** + * This will fill a field in a PDF document. + *
+ * see usage() for commandline + * + * @param args command line arguments + */ + public static void main(String[] args) + { + FieldsDoc app = new FieldsDoc(); + try + { + if( args.length != 4 ) + { + app.usage(); + } + else + { + app.doIt( args[0], args[1], args[2], args[3]); + } + } + catch (Exception e) + { + e.printStackTrace(); + } + } + /** + * lookup and fill the field. + * + * todo: this method will go to the pdfmodel package one day + * + * @param doc the document where the field resides + * @param name the name of the PDF Annotation field + * @param value The desired value to be used for the field + * + */ + public void setField(COSDocument doc, COSString name, COSString value) + { + for (Iterator i = doc.getObjects().iterator(); i.hasNext();) + { + COSObject obj = (COSObject) i.next(); + COSBase base = obj.getObject(); + if (base instanceof COSDictionary) + { + COSDictionary dict = (COSDictionary) base; + COSBase type = dict.getItem(COSName.TYPE); + if (type != null && type.equals(COSName.getPDFName("Annot"))) + { + COSBase subtype = dict.getItem(COSName.getPDFName("Subtype")); + if (subtype != null && subtype.equals(COSName.getPDFName("Widget"))) + { + // we found the field + COSBase fname = dict.getItem(COSName.getPDFName("T")); + if (fname != null && fname.equals(name)) + { + dict.setItem(COSName.getPDFName("V"), value); + } + } + } + } + } + } + /** + * This will print out a message telling how to use this example. + */ + private void usage() + { + System.err.println( "usage: " + this.getClass().getName() + " " ); + } +} \ No newline at end of file diff --git a/src/main/java/org/pdfbox/examples/persistence/WriteDecodedDoc.java b/src/main/java/org/pdfbox/examples/persistence/WriteDecodedDoc.java new file mode 100644 index 0000000..8fcab7f --- /dev/null +++ b/src/main/java/org/pdfbox/examples/persistence/WriteDecodedDoc.java @@ -0,0 +1,151 @@ +/** + * Copyright (c) 2003-2004, www.pdfbox.org + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * 3. Neither the name of pdfbox; nor the names of its + * contributors may be used to endorse or promote products derived from this + * software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + * http://www.pdfbox.org + * + */ +package org.pdfbox.examples.persistence; + +import java.io.IOException; + +import java.util.Iterator; + +import org.pdfbox.cos.COSBase; +import org.pdfbox.cos.COSObject; +import org.pdfbox.cos.COSStream; + +import org.pdfbox.pdmodel.PDDocument; + +import org.pdfbox.exceptions.COSVisitorException; + +import org.pdfbox.exceptions.InvalidPasswordException; + +/** + * load document and write with all streams decoded. + * + * @author Michael Traut + * @version $Revision: 1.8 $ + */ +public class WriteDecodedDoc +{ + + /** + * Constructor. + */ + public WriteDecodedDoc() + { + super(); + } + + /** + * This will perform the document reading, decoding and writing. + * + * @param in The filename used for input. + * @param out The filename used for output. + * + * @throws IOException If there is an error parsing the document. + * @throws COSVisitorException If there is an error while copying the document. + */ + public void doIt(String in, String out) throws IOException, COSVisitorException + { + PDDocument doc = null; + try + { + doc = PDDocument.load( in ); + if( doc.isEncrypted() ) + { + try + { + doc.decrypt( "" ); + } + catch( InvalidPasswordException e ) + { + System.err.println( "Error: The document is encrypted." ); + } + catch( org.pdfbox.exceptions.CryptographyException e ) + { + e.printStackTrace(); + } + } + + for (Iterator i = doc.getDocument().getObjects().iterator(); i.hasNext();) + { + COSBase base = ((COSObject) i.next()).getObject(); + if (base instanceof COSStream) + { + // just kill the filters + COSStream cosStream = (COSStream)base; + cosStream.getUnfilteredStream(); + cosStream.setFilters(null); + } + } + doc.save( out ); + } + finally + { + if( doc != null ) + { + doc.close(); + } + } + } + + /** + * This will write a PDF document with completely decoded streams. + *
+ * see usage() for commandline + * + * @param args command line arguments + */ + public static void main(String[] args) + { + WriteDecodedDoc app = new WriteDecodedDoc(); + try + { + if( args.length != 2 ) + { + app.usage(); + } + else + { + app.doIt( args[0], args[1]); + } + } + catch (Exception e) + { + e.printStackTrace(); + } + } + + /** + * This will print out a message telling how to use this example. + */ + private void usage() + { + System.err.println( "usage: " + this.getClass().getName() + " " ); + } +} \ No newline at end of file diff --git a/src/main/java/org/pdfbox/examples/persistence/package.html b/src/main/java/org/pdfbox/examples/persistence/package.html new file mode 100644 index 0000000..9c0d57f --- /dev/null +++ b/src/main/java/org/pdfbox/examples/persistence/package.html @@ -0,0 +1,9 @@ + + + + + + +These examples will show how to use the persistence features of the PDFBox. + + -- cgit v1.2.3