Skip to content

Instantly share code, notes, and snippets.

@JoelGeraci-Datalogics
Last active June 10, 2016 22:06
Show Gist options
  • Save JoelGeraci-Datalogics/fc473780d6ed0ca515fb to your computer and use it in GitHub Desktop.
Save JoelGeraci-Datalogics/fc473780d6ed0ca515fb to your computer and use it in GitHub Desktop.
This sample will scrub the document for PDF features that can make the rendered appearance of a PDF document vary. It will then generate and add the LegalAttestation object to the document catalog.
/*
* Copyright Datalogics, Inc. 2015
*/
package pdfjt.cookbook.document;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
import java.util.Map;
import pdfjt.util.SampleFileServices;
import com.adobe.internal.io.ByteReader;
import com.adobe.internal.io.ByteWriter;
import com.adobe.internal.io.InputStreamByteReader;
import com.adobe.pdfjt.pdf.digsig.PDFLegalAttestation;
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.services.digsig.LegalScrubber;
import com.adobe.pdfjt.services.digsig.impl.LegalWarning;
/**
* This sample will scrub the document for PDF features that can make the
* rendered appearance of a PDF document vary. It will then generate and add the
* LegalAttestation object to the document catalog.
*/
public class GenerateLegalAttestation {
private static final String inputPDFURL = "http://dev.datalogics.com/cookbook/document/LegalScrubber.pdf";
private static final String outputDir = "cookbook/Document/output/";
static public void main(String[] args) throws Exception {
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());
/*
* Create a new LegalScrubber and generate the warnings Map.
*/
LegalScrubber legalScrubber = LegalScrubber.newInstance(pdfDocument);
@SuppressWarnings("unchecked")
Map<LegalWarning, Integer> legalWarnings = legalScrubber.generateWarnings();
/*
* Check for specific warnings.
* The first three conditions are present in the sample input document.
* The fourth is not. It is left as a developer exercise to supply
* warnings for all other LegalWarning constants.
*/
if (legalWarnings.containsKey(LegalWarning.k_OptionalContent)) {
System.out.println("This document contains layers that can be silently displayed or hidden on the fly.");
}
if (legalWarnings.containsKey(LegalWarning.k_NonEmbeddedFonts)) {
System.out.println("This document uses fonts that are not embedded. Displayed text may be appear different from what was intended.");
}
if (legalWarnings.containsKey(LegalWarning.k_Annotations)) {
System.out.println("The document contains comments whose visual appearances may change based on external variables.");
}
if (legalWarnings.containsKey(LegalWarning.k_JavaScriptActions)) {
System.out.println("The document contains hidden actions that may not be known by the end user. Actions include JavaScript actions (document open, save, etc.), playing multimedia, executing a menu item, etc.");
}
/*
* Generate and add the LegalAttestation to the document catalog. See
* section 12.8.5 Legal Content Attestations of the PDF Specification.
*/
PDFLegalAttestation legalAttestation = legalScrubber.generateLegalAttestation();
pdfDocument.requireCatalog().setLegalAttestation(legalAttestation);
String outputFileName = "OutputWithLegalAttestation.pdf";
ByteWriter outputFile = SampleFileServices.getRAFByteWriter(outputDir + outputFileName);
SampleFileServices.createDir(outputDir);
pdfDocument.save(outputFile, PDFSaveFullOptions.newInstance());
System.out.println("Created: " + outputFileName);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment