Created
December 7, 2016 01:47
-
-
Save JoelGeraci-Datalogics/18422968d682c190ec01d88a700cec40 to your computer and use it in GitHub Desktop.
This Gist shows how to exclude specific annotation types from being flattened by the FormFlattener.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package pdfjt.cookbook.document; | |
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.graphics.font.PDFFont; | |
import com.adobe.pdfjt.pdf.interactive.annotation.PDFAnnotationEnum; | |
import com.adobe.pdfjt.services.ap.spi.APContext; | |
import com.adobe.pdfjt.services.ap.spi.APResources; | |
import com.adobe.pdfjt.services.formflattener.FormFlattener; | |
import com.datalogics.pdf.document.DocumentHelper; | |
import java.io.InputStream; | |
import java.net.URL; | |
import java.net.URLConnection; | |
import java.util.EnumSet; | |
import java.util.HashMap; | |
/** | |
* This Gist shows how to exclude specific annotation types from being flattened by the FormFlattener. | |
*/ | |
public class FlattenEverythingExceptLinks { | |
private static final String inputPDF_URL = "http://practicalpdf.com/downloads/selectiveFlattening.pdf"; | |
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()); | |
// Create the resources object we'll need for flattening | |
APResources apResources = new APResources(pdfDocument.getCosDocument().getOptions().getFontSet(), | |
pdfDocument.getCosDocument().getOptions().getDocLocale(), new HashMap<Font, PDFFont>()); | |
// Create the context we need for limiting the flattener | |
APContext apContext = new APContext(apResources, true, null); | |
// Add all of the annotation types to the set we are going to flatten | |
EnumSet<PDFAnnotationEnum> annotationsToProcess = EnumSet.allOf(PDFAnnotationEnum.class); | |
// Remove just the link annotation type | |
annotationsToProcess.remove(PDFAnnotationEnum.Link); | |
// now set this set of annotation types as the ones to flatten. | |
apContext.setAnnotationsToBeProcessed(annotationsToProcess); | |
FormFlattener.flattenDocument(apContext, pdfDocument, null); | |
// Save and close the file. | |
DocumentHelper.saveAndClose(pdfDocument, "selectiveFlattening_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