Author: tilman Date: Wed Apr 20 21:37:49 2016 New Revision: 1740200 URL: http://svn.apache.org/viewvc?rev=1740200&view=rev Log: PDFBOX-3323: add possibility to set info and xmp after merge
Modified: pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/multipdf/PDFMergerUtility.java Modified: pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/multipdf/PDFMergerUtility.java URL: http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/multipdf/PDFMergerUtility.java?rev=1740200&r1=1740199&r2=1740200&view=diff ============================================================================== --- pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/multipdf/PDFMergerUtility.java (original) +++ pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/multipdf/PDFMergerUtility.java Wed Apr 20 21:37:49 2016 @@ -16,6 +16,7 @@ */ package org.apache.pdfbox.multipdf; +import java.io.ByteArrayInputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; @@ -45,6 +46,7 @@ import org.apache.pdfbox.pdmodel.PDDocum import org.apache.pdfbox.pdmodel.PDPage; import org.apache.pdfbox.pdmodel.PDResources; import org.apache.pdfbox.pdmodel.PageMode; +import org.apache.pdfbox.pdmodel.common.PDMetadata; import org.apache.pdfbox.pdmodel.common.PDNumberTreeNode; import org.apache.pdfbox.pdmodel.common.PDStream; import org.apache.pdfbox.pdmodel.documentinterchange.logicalstructure.PDMarkInfo; @@ -71,6 +73,8 @@ public class PDFMergerUtility private String destinationFileName; private OutputStream destinationStream; private boolean ignoreAcroFormErrors = false; + private PDDocumentInformation destinationDocumentInformation = null; + private byte[] destinationXmpMetadata = null; /** * Instantiate a new PDFMergerUtility. @@ -122,6 +126,50 @@ public class PDFMergerUtility } /** + * Get the destination document information that is to be set in {@link #mergeDocuments(org.apache.pdfbox.io.MemoryUsageSetting) + * }. The default is null, which means that it is ignored. + * + * @return The destination document information. + */ + public PDDocumentInformation getDestinationDocumentInformation() + { + return destinationDocumentInformation; + } + + /** + * Set the destination document information that is to be set in {@link #mergeDocuments(org.apache.pdfbox.io.MemoryUsageSetting) + * }. The default is null, which means that it is ignored. + * + * @param info The destination document information. + */ + public void setDestinationDocumentInformation(PDDocumentInformation info) + { + destinationDocumentInformation = info; + } + + /** + * Set the destination XMP metadata that is to be set in {@link #mergeDocuments(org.apache.pdfbox.io.MemoryUsageSetting) + * }. The default is null, which means that it is ignored. + * + * @return The destination XMP metadata. + */ + public byte[] getDestinationXmpMetadata() + { + return destinationXmpMetadata; + } + + /** + * Set the destination XMP metadata that is to be set in {@link #mergeDocuments(org.apache.pdfbox.io.MemoryUsageSetting) + * }. The default is null, which means that it is ignored. + * + * @param xmp The destination XMP metadata. + */ + public void setXMPMetadata(byte[] xmp) + { + destinationXmpMetadata = xmp; + } + + /** * Add a source file to the list of files to merge. * * @param source Full path and file name of source document. @@ -213,6 +261,18 @@ public class PDFMergerUtility tobeclosed.add(source); appendDocument(destination, source); } + + // optionally set meta data + if (destinationDocumentInformation != null) + { + destination.setDocumentInformation(destinationDocumentInformation); + } + if (destinationXmpMetadata != null) + { + PDMetadata meta = new PDMetadata(destination, new ByteArrayInputStream(destinationXmpMetadata)); + destination.getDocumentCatalog().setMetadata(meta); + } + if (destinationStream == null) { destination.save(destinationFileName);