Skip to content

Instantly share code, notes, and snippets.

@JoelGeraci-Datalogics
Last active November 18, 2016 17:45
Show Gist options
  • Save JoelGeraci-Datalogics/67f1468118080f92a6d94097247b285f to your computer and use it in GitHub Desktop.
Save JoelGeraci-Datalogics/67f1468118080f92a6d94097247b285f to your computer and use it in GitHub Desktop.
Filla a rich text field in a PDF file and then creates it's appearances.
package pdfjt.cookbook.forms;
/*
* Copyright 2016 Datalogics, Inc.
*/
import com.adobe.fontengine.font.Font;
import com.adobe.internal.io.ByteReader;
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.document.PDFText;
import com.adobe.pdfjt.pdf.graphics.font.PDFFont;
import com.adobe.pdfjt.pdf.interactive.forms.PDFFieldNode;
import com.adobe.pdfjt.pdf.interactive.forms.PDFInteractiveForm;
import com.adobe.pdfjt.services.ap.AppearanceService;
import com.adobe.pdfjt.services.ap.TextFormatterImpl;
import com.adobe.pdfjt.services.ap.extension.APExtensionOptions;
import com.adobe.pdfjt.services.ap.spi.APContext;
import com.adobe.pdfjt.services.ap.spi.APResources;
import com.adobe.pdfjt.services.rcg.impl.RichTextHandler;
import com.datalogics.pdf.document.DocumentHelper;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Scanner;
/**
*/
public class FillRichTextField {
private static final String inputPDF_URL = "http://dev.datalogics.com/cookbook/forms/RichText.pdf";
private static final String inputXML_URL = "http://dev.datalogics.com/cookbook/forms/RichText.xml";
private static final String userAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.95 Safari/537.11";
public static void main(final String... args) throws Exception {
// First get the PDF file
URLConnection connection = new URL(inputPDF_URL).openConnection();
connection.setRequestProperty("User-Agent", userAgent);
connection.connect();
InputStream fis = connection.getInputStream();
ByteReader byteReader = new InputStreamByteReader(fis);
PDFDocument pdfDocument = PDFDocument.newInstance(byteReader, PDFOpenOptions.newInstance());
// Then get the XML that represents the Rich Text
connection = new URL(inputXML_URL).openConnection();
connection.setRequestProperty("User-Agent", userAgent);
connection.connect();
fis = connection.getInputStream();
Scanner scanner = new Scanner(fis, StandardCharsets.UTF_8.name());
String xmlString = scanner.useDelimiter("\\A").next();
scanner.close();
// Now we can fill the /RV key of the field "richText1" with the XML string
PDFInteractiveForm pdfInteractiveForm = pdfDocument.requireCatalog().getInteractiveForm();
PDFFieldNode richTextField = pdfInteractiveForm.getChildren().getFieldFullyNamed("richText1");
richTextField.setRichText(PDFText.createString(pdfDocument, xmlString));
// Next we have to set the /V key with just the text portion of the XML otherwise Acrobat won't be able to edit the field.
String rawValue = new RichTextHandler().getRawContent(xmlString);
richTextField.setStringValue(rawValue);
// Finally we can generate the appearance.
APResources apResources = new APResources(
pdfDocument.getCosDocument().getOptions().getFontSet(),
pdfDocument.getCosDocument().getOptions().getDocLocale(),
new HashMap<Font, PDFFont>());
APExtensionOptions apExtensionOptions = APExtensionOptions.newInstance();
APContext apContext = new APContext(apResources, true, apExtensionOptions);
AppearanceService.generateAppearances(pdfDocument, apContext, new TextFormatterImpl(pdfDocument));
// Save and close the file.
DocumentHelper.saveAndClose(pdfDocument, "RichText_Output.pdf", PDFSaveFullOptions.newInstance());
System.out.println("Done!");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment