Skip to content

Instantly share code, notes, and snippets.

@JoelGeraci-Datalogics
Last active August 29, 2015 14:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JoelGeraci-Datalogics/0f0c603a484eb0d18a20 to your computer and use it in GitHub Desktop.
Save JoelGeraci-Datalogics/0f0c603a484eb0d18a20 to your computer and use it in GitHub Desktop.
Merging PDF Files and Retaining Interactive Fields Using the Datalogics PDF Java Toolkit
/*
* Copyright Datalogics, Inc. 2015
*/
package pdfjt.cookbook.combine;
import com.adobe.internal.io.ByteReader;
import com.adobe.internal.io.ByteWriter;
import com.adobe.internal.io.InputStreamByteReader;
import com.adobe.pdfjt.core.types.ASRectangle;
import com.adobe.pdfjt.pdf.document.PDFDocument;
import com.adobe.pdfjt.pdf.document.PDFOpenOptions;
import com.adobe.pdfjt.pdf.document.PDFSaveFullOptions;
import com.adobe.pdfjt.pdf.document.PDFVersion;
import com.adobe.pdfjt.pdf.interactive.forms.PDFField;
import com.adobe.pdfjt.pdf.interactive.forms.PDFInteractiveForm;
import com.adobe.pdfjt.services.manipulations.PMMOptions;
import com.adobe.pdfjt.services.manipulations.PMMService;
import java.io.File;
import java.io.FileInputStream;
import java.util.Iterator;
import pdfjt.util.SampleFileServices;
public class MergeFormsRetainingFields {
private static final String inputDir = "cookbook/MergeForms/input";
private static final String outputDir = "cookbook/MergeForms/output";
private static final String outputFilename = "MergedFormsWithFields.pdf";
/**
* @param args
*/
public static void main(String[] args) throws Exception{
/*
* Start by creating a new PDF document that will be used to merge the
* other documents into. The new document will contain a single blank page
* but we'll remove this just before saving the merged file.
*/
PDFDocument mergedDocument = PDFDocument.newInstance(
new ASRectangle(new double[]{ 0, 0, 612, 792}),
PDFOpenOptions.newInstance());
/*
* Create the new PMMService that will be used to manipulate the pages.
*/
PMMService pmmService = new PMMService(mergedDocument);
try {
/*
* Add the files in the input directory to the new PDF file. This
* process will append the pages from each document to the end of
* the new document creating a continuous series of pages.
*
* Folders will be skipped.
*/
File root = new File(inputDir);
File[] list = root.listFiles();
if (list == null) return;
for (File pdfFile : list) {
if (pdfFile.isDirectory() == false) {
String pdfFileName = pdfFile.getName();
FileInputStream fis = new FileInputStream(pdfFile);
ByteReader byteReader = new InputStreamByteReader(fis);
PDFDocument pdfToAppend = PDFDocument.newInstance(byteReader, PDFOpenOptions.newInstance());
String pdfBaseFileName = pdfFileName.substring(0, pdfFileName.length() - 4);
/*
* Unless the following step is taken, form fields with the
* same name will be merged and assume the value of the
* first field encountered during the append.
*
* To ensure that each field in the combined file has a
* unique name, we'll prepend the file name to the field
* name prior to combining.
*/
// Get the form and then the field iterator
PDFInteractiveForm pdfInteractiveForm = pdfToAppend.getInteractiveForm();
Iterator<PDFField> fieldIterator = pdfInteractiveForm.iterator();
// Iterator over form fields
while(fieldIterator.hasNext()) {
PDFField pdfField = (PDFField)(fieldIterator.next());
//Change the field name
pdfField.setPartialName(pdfBaseFileName+"."+pdfField.getPartialName());
}
// Now append the PDF with the modified field names.
System.out.println("Appending " + pdfFileName + " to the end of "+outputFilename);
pmmService.appendPages(pdfToAppend, null, PMMOptions.newInstanceAll());
byteReader.close();
}
}
/*
* Remove the first page. We don't need it anymore.
*/
mergedDocument.requirePages().removePage(mergedDocument.requirePages().getPage(0));
/*
* Save the file
*/
ByteWriter outputWriter = SampleFileServices.getRAFByteWriter(outputDir + File.separator + outputFilename);
mergedDocument.save(outputWriter, PDFSaveFullOptions.newInstance(PDFVersion.v1_7));
System.out.println("Created "+outputFilename +" with "+mergedDocument.requirePages().getCount()+" pages");
mergedDocument.close();
outputWriter.close();
} finally {
//
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment