Skip to content

Instantly share code, notes, and snippets.

@JoelGeraci-Datalogics
Last active August 29, 2015 14:24
Show Gist options
  • Save JoelGeraci-Datalogics/165c8fd8bb1fbdba4965 to your computer and use it in GitHub Desktop.
Save JoelGeraci-Datalogics/165c8fd8bb1fbdba4965 to your computer and use it in GitHub Desktop.
This sample reproduces the Acrobat feature "Replace Pages". It will change the page content but leave the form fields in place.
/*
* Copyright Datalogics, Inc. 2015
*/
package pdfjt.cookbook.document;
import com.adobe.internal.io.ByteReader;
import com.adobe.internal.io.ByteWriter;
import com.adobe.internal.io.InputStreamByteReader;
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.page.PDFPage;
import com.adobe.pdfjt.services.manipulations.PMMOptions;
import com.adobe.pdfjt.services.manipulations.PMMService;
import java.io.InputStream;
import java.net.URL;
import pdfjt.util.SampleFileServices;
/**
* This sample reproduces the Acrobat feature "Replace Pages". It will change
* the page content but leave the form fields in place.
*/
public class ReplacePages {
private static final String inputPDF = "http://dev.datalogics.com/cookbook/document/FruitForm_2014.pdf";
private static final String repacementPDF = "http://dev.datalogics.com/cookbook/document/FruitForm_2015.pdf";
private static final String outputDir = "cookbook/Document/output/";
static public void main(String[] args) throws Exception
{
SampleFileServices.createDir(outputDir);
try
{
//First read in the PDF files
InputStream fis = new URL(inputPDF).openStream();
ByteReader byteReader = new InputStreamByteReader(fis);
PDFDocument pdfDocument = PDFDocument.newInstance(byteReader, PDFOpenOptions.newInstance());
fis = new URL(repacementPDF).openStream();
byteReader = new InputStreamByteReader(fis);
PDFDocument pdfReplacementDocument = PDFDocument.newInstance(byteReader, PDFOpenOptions.newInstance());
/*
* Resources are not easily copied from one PDF to another so we use
* the PMMService class to insert the replcement page into the
* original PDF. We'll delete it after we get what we need from it.
*/
PMMService pageManipulator = new PMMService(pdfDocument);
/*
* Add the replacement page before the first page of the document.
*/
pageManipulator.insertPages(pdfReplacementDocument, null, null, PMMOptions.newInstanceAll());
/*
* Get the pages we need to copy the annotations to and from.
*/
PDFPage originalPageOne = pdfDocument.requirePages().getPage(1);
PDFPage replacementPageOne = pdfDocument.requirePages().getPage(0);
/*
* Create the Annots dictionary for the new page and then populate
* it with the annotations from the original, then delete it.
*/
replacementPageOne.procureAnnotationList().addAnnotations(originalPageOne.getAnnotationList());
/*
* Remove the annotations from the orignial page or else the
* annotations from both pages will get removed when the original
* page is deleted.
*/
originalPageOne.removeAnnotationList();
pageManipulator.deletePages(originalPageOne, 1);
ByteWriter outputFile = SampleFileServices.getRAFByteWriter(outputDir + "FruitForm_2015_New.pdf");
pdfDocument.save(outputFile, PDFSaveFullOptions.newInstance());
} finally {
//
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment