Skip to content

Instantly share code, notes, and snippets.

@JoelGeraci-Datalogics
Last active June 10, 2016 18:52
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/3570b1dc9d4cdf2e2e6c01e49a2be8b3 to your computer and use it in GitHub Desktop.
Save JoelGeraci-Datalogics/3570b1dc9d4cdf2e2e6c01e49a2be8b3 to your computer and use it in GitHub Desktop.
This sample will populate a few fields on a Certified Document and the save it without breaking the certification.
/*
* Copyright Datalogics, Inc. 2015
*/
package pdfjt.cookbook.forms;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
import java.util.HashMap;
import pdfjt.util.SampleFileServices;
import com.adobe.fontengine.font.Font;
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.PDFSaveIncrementalOptions;
import com.adobe.pdfjt.pdf.graphics.font.PDFFont;
import com.adobe.pdfjt.pdf.interactive.forms.PDFFieldList;
import com.adobe.pdfjt.services.ap.AppearanceService;
import com.adobe.pdfjt.services.ap.spi.APContext;
import com.adobe.pdfjt.services.ap.spi.APResources;
/**
* This sample will populate a few fields on a Certified Document and the save it without breaking the certification.
*/
public class FillCertifiedForm {
private static final String inputPDFURL = "http://dev.datalogics.com/cookbook/forms/BlankInputForm_Certified.pdf";
private static final String outputDir = "cookbook/Forms/output/";
static public void main(String[] args) throws Exception {
/*
* Get the PDF File
*/
URLConnection connection = new URL(inputPDFURL).openConnection();
connection.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.95 Safari/537.11");
connection.connect();
InputStream fis = connection.getInputStream();
ByteReader byteReader = new InputStreamByteReader(fis);
PDFDocument pdfDocument = PDFDocument.newInstance(byteReader, PDFOpenOptions.newInstance());
/*
* Set the field values
*/
PDFFieldList fieldList = pdfDocument.getInteractiveForm().getChildren();
fieldList.getFieldNamed("FirstName").setStringValue("Joel");
fieldList.getFieldNamed("LastName").setStringValue("Geraci");
fieldList.getFieldNamed("Address").setStringValue("101 N. Wacker Drive #1800");
fieldList.getFieldNamed("City").setStringValue("Chicago");
fieldList.getFieldNamed("Region").setStringValue("IL");
fieldList.getFieldNamed("PostalCode").setStringValue("60606");
fieldList.getFieldNamed("Country").setStringValue("United States");
/*
* Get the resources necessary to generate the field appearances
* after we add the data.
*/
APResources apResources = new APResources(pdfDocument.getCosDocument().getOptions().getFontSet(), pdfDocument.getCosDocument()
.getOptions().getDocLocale(), new HashMap<Font, PDFFont>());
APContext apContext = new APContext(apResources, true, null);
AppearanceService.generateAppearances(pdfDocument, apContext, null);
/*
* Write out the file
*/
SampleFileServices.createDir(outputDir);
String outputFileName = "FilledAndCerfified.pdf";
ByteWriter outputFile = SampleFileServices.getRAFByteWriter(outputDir + outputFileName);
pdfDocument.save(outputFile, PDFSaveIncrementalOptions.newInstance());
System.out.println("Created: " + outputFileName);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment