Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save JoelGeraci-Datalogics/d2aed3e8be782654dced to your computer and use it in GitHub Desktop.
Save JoelGeraci-Datalogics/d2aed3e8be782654dced to your computer and use it in GitHub Desktop.
Merging PDF Files and Flattening 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.services.formflattener.FormFlattener;
import com.adobe.pdfjt.services.manipulations.PMMOptions;
import com.adobe.pdfjt.services.manipulations.PMMService;
import java.io.File;
import java.io.FileInputStream;
import pdfjt.util.SampleFileServices;
public class MergeFormsFlatteningFields {
private static final String inputDir = "cookbook/MergeForms/input";
private static final String outputDir = "cookbook/MergeForms/output";
private static final String outputFilename = "MergedFormsFlattened.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());
/*
* 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.
*
* Flattening the forms removes the form field widget from
* the page and leaves the field appearance in it's place.
*/
FormFlattener.flattenDocument(null, pdfToAppend, null);
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