Author: tilman Date: Thu Mar 13 17:51:32 2014 New Revision: 1577261 URL: http://svn.apache.org/r1577261 Log: PDFBOX-1964: support non-sequential parser
Modified: pdfbox/branches/1.8/pdfbox/src/main/java/org/apache/pdfbox/util/PDFMergerUtility.java Modified: pdfbox/branches/1.8/pdfbox/src/main/java/org/apache/pdfbox/util/PDFMergerUtility.java URL: http://svn.apache.org/viewvc/pdfbox/branches/1.8/pdfbox/src/main/java/org/apache/pdfbox/util/PDFMergerUtility.java?rev=1577261&r1=1577260&r2=1577261&view=diff ============================================================================== --- pdfbox/branches/1.8/pdfbox/src/main/java/org/apache/pdfbox/util/PDFMergerUtility.java (original) +++ pdfbox/branches/1.8/pdfbox/src/main/java/org/apache/pdfbox/util/PDFMergerUtility.java Thu Mar 13 17:51:32 2014 @@ -35,6 +35,7 @@ import org.apache.pdfbox.cos.COSNumber; import org.apache.pdfbox.cos.COSStream; import org.apache.pdfbox.cos.COSString; import org.apache.pdfbox.exceptions.COSVisitorException; +import org.apache.pdfbox.io.RandomAccess; import org.apache.pdfbox.pdmodel.PDDocument; import org.apache.pdfbox.pdmodel.PDDocumentCatalog; import org.apache.pdfbox.pdmodel.PDDocumentInformation; @@ -53,10 +54,11 @@ import org.apache.pdfbox.pdmodel.interac import org.apache.pdfbox.pdmodel.interactive.form.PDFieldFactory; /** - * This class will take a list of pdf documents and merge them, saving the result in a new document. - * + * This class will take a list of pdf documents and merge them, saving the + * result in a new document. + * * @author <a href="mailto:b...@benlitchfield.com">Ben Litchfield</a> - * + * */ public class PDFMergerUtility { @@ -77,7 +79,7 @@ public class PDFMergerUtility /** * Get the name of the destination file. - * + * * @return Returns the destination. */ public String getDestinationFileName() @@ -87,7 +89,7 @@ public class PDFMergerUtility /** * Set the name of the destination file. - * + * * @param destination The destination to set. */ public void setDestinationFileName(String destination) @@ -97,7 +99,7 @@ public class PDFMergerUtility /** * Get the destination OutputStream. - * + * * @return Returns the destination OutputStream. */ public OutputStream getDestinationStream() @@ -107,7 +109,7 @@ public class PDFMergerUtility /** * Set the destination OutputStream. - * + * * @param destStream The destination to set. */ public void setDestinationStream(OutputStream destStream) @@ -117,7 +119,7 @@ public class PDFMergerUtility /** * Add a source file to the list of files to merge. - * + * * @param source Full path and file name of source document. */ public void addSource(String source) @@ -134,7 +136,7 @@ public class PDFMergerUtility /** * Add a source file to the list of files to merge. - * + * * @param source File representing source document */ public void addSource(File source) @@ -151,7 +153,7 @@ public class PDFMergerUtility /** * Add a source to the list of documents to merge. - * + * * @param source InputStream representing source document */ public void addSource(InputStream source) @@ -161,8 +163,9 @@ public class PDFMergerUtility /** * Add a list of sources to the list of documents to merge. - * - * @param sourcesList List of InputStream objects representing source documents + * + * @param sourcesList List of InputStream objects representing source + * documents */ public void addSources(List<InputStream> sourcesList) { @@ -170,13 +173,37 @@ public class PDFMergerUtility } /** - * Merge the list of source documents, saving the result in the destination file. - * + * Merge the list of source documents, saving the result in the destination + * file. + * * @throws IOException If there is an error saving the document. - * @throws COSVisitorException If an error occurs while saving the destination file. + * @throws COSVisitorException If an error occurs while saving the + * destination file. */ public void mergeDocuments() throws IOException, COSVisitorException { + mergeDocuments(false, null); + } + + /** + * Merge the list of source documents with the non sequential parser, saving + * the result in the destination file. + * + * @param scratchFile location to store temp PDFBox data for this output + * document + * @throws IOException If there is an error saving the document. + * @throws COSVisitorException If an error occurs while saving the + * destination file. + */ + public void mergeDocumentsNonSeq(RandomAccess scratchFile) + throws IOException, COSVisitorException + { + mergeDocuments(true, scratchFile); + } + + private void mergeDocuments(boolean isNonSeq, RandomAccess scratchFile) + throws IOException, COSVisitorException + { PDDocument destination = null; InputStream sourceFile; PDDocument source; @@ -188,12 +215,27 @@ public class PDFMergerUtility { Iterator<InputStream> sit = sources.iterator(); sourceFile = sit.next(); - destination = PDDocument.load(sourceFile); + if (isNonSeq) + { + destination = PDDocument.loadNonSeq(sourceFile, scratchFile); + } + else + { + destination = PDDocument.load(sourceFile); + } while (sit.hasNext()) { sourceFile = sit.next(); - source = PDDocument.load(sourceFile); + if (isNonSeq) + { + source = PDDocument.loadNonSeq(sourceFile, null); + } + else + { + source = PDDocument.load(sourceFile); + } + tobeclosed.add(source); appendDocument(destination, source); } @@ -222,13 +264,15 @@ public class PDFMergerUtility /** * append all pages from source to destination. - * + * * @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. + * + * @throws IOException If there is an error accessing data from either + * document. */ - public void appendDocument(PDDocument destination, PDDocument source) throws IOException + public void appendDocument(PDDocument destination, PDDocument source) + throws IOException { if (destination.isEncrypted()) { @@ -517,8 +561,9 @@ public class PDFMergerUtility private int nextFieldNum = 1; /** - * Merge the contents of the source form into the destination form for the destination file. - * + * Merge the contents of the source form into the destination form for the + * destination file. + * * @param cloner the object cloner for the destination document * @param destAcroForm the destination form * @param srcAcroForm the source form @@ -555,7 +600,7 @@ public class PDFMergerUtility /** * Indicates if acroform errors are ignored or not. - * + * * @return true if acroform errors are ignored */ public boolean isIgnoreAcroFormErrors() @@ -565,8 +610,9 @@ public class PDFMergerUtility /** * Set to true to ignore acroform errors. - * - * @param ignoreAcroFormErrorsValue true if acroform errors should be ignored + * + * @param ignoreAcroFormErrorsValue true if acroform errors should be + * ignored */ public void setIgnoreAcroFormErrors(boolean ignoreAcroFormErrorsValue) { @@ -575,7 +621,7 @@ public class PDFMergerUtility /** * Update the Pg and Obj references to the new (merged) page. - * + * * @param parentTreeEntry * @param objMapping mapping between old and new references */ @@ -626,7 +672,7 @@ public class PDFMergerUtility /** * Update the P reference to the new parent dictionary. - * + * * @param kArray the kids array * @param newParent the new parent */ @@ -648,7 +694,7 @@ public class PDFMergerUtility /** * Update the StructParents and StructParent values in a PDPage. - * + * * @param page the new page * @param structParentOffset the offset which should be applied */