PDFRedact

Sample Java code for using Apryse SDK to remove potentially sensitive content within PDF documents. Using 'pdftron.PDF.Redactor' makes sure that if a portion of an image, text, or vector graphics is contained in a redaction region, that portion is destroyed and is not simply hidden with clipping or image masks. Learn more about our Android SDK.

1//---------------------------------------------------------------------------------------
2// Copyright (c) 2001-2019 by PDFTron Systems Inc. All Rights Reserved.
3// Consult legal.txt regarding legal and license information.
4//---------------------------------------------------------------------------------------
5
6package com.pdftron.android.pdfnetsdksamples.samples;
7
8import com.pdftron.android.pdfnetsdksamples.OutputListener;
9import com.pdftron.android.pdfnetsdksamples.PDFNetSample;
10import com.pdftron.android.pdfnetsdksamples.R;
11import com.pdftron.android.pdfnetsdksamples.util.Utils;
12import com.pdftron.pdf.PDFDoc;
13import com.pdftron.pdf.Rect;
14import com.pdftron.pdf.Redactor;
15import com.pdftron.sdf.SDFDoc;
16
17import java.util.ArrayList;
18
19/**
20 * PDF Redactor is a separately licensable Add-on that offers options to remove
21 * (not just covering or obscuring) content within a region of PDF.
22 * With printed pages, redaction involves blacking-out or cutting-out areas of
23 * the printed page. With electronic documents that use formats such as PDF,
24 * redaction typically involves removing sensitive content within documents for
25 * safe distribution to courts, patent and government institutions, the media,
26 * customers, vendors or any other audience with restricted access to the content.
27 * <p>
28 * The redaction process in PDFNet consists of two steps:
29 * <p>
30 * a) Content identification: A user applies redact annotations that specify the
31 * pieces or regions of content that should be removed. The content for redaction
32 * can be identified either interactively (e.g. using 'com.pdftron.pdf.PDFViewCtrl'
33 * as shown in PDFView sample) or programmatically (e.g. using 'com.pdftron.pdf.TextSearch'
34 * or 'com.pdftron.pdf.TextExtractor'). Up until the next step is performed, the user
35 * can see, move and redefine these annotations.
36 * b) Content removal: Using 'com.pdftron.pdf.Redactor.Redact()' the user instructs
37 * PDFNet to apply the redact regions, after which the content in the area specified
38 * by the redact annotations is removed. The redaction function includes number of
39 * options to control the style of the redaction overlay (including color, text,
40 * font, border, transparency, etc.).
41 * <p>
42 * PDFTron Redactor makes sure that if a portion of an image, text, or vector graphics
43 * is contained in a redaction region, that portion of the image or path data is
44 * destroyed and is not simply hidden with clipping or image masks. PDFNet API can also
45 * be used to review and remove metadata and other content that can exist in a PDF
46 * document, including XML Forms Architecture (XFA) content and Extensible Metadata
47 * Platform (XMP) content.
48 */
49
50public class PDFRedactTest extends PDFNetSample {
51
52 private static OutputListener mOutputListener;
53
54 private static ArrayList<String> mFileList = new ArrayList<>();
55
56 public PDFRedactTest() {
57 setTitle(R.string.sample_pdfredact_title);
58 setDescription(R.string.sample_pdfredact_description);
59
60 // The standard library does not include the Redaction feature.
61 // If using the full library, please comment out the following
62 // call.
63 // DisableRun();
64 }
65
66 @Override
67 public void run(OutputListener outputListener) {
68 super.run(outputListener);
69 mOutputListener = outputListener;
70 mFileList.clear();
71 printHeader(outputListener);
72 // Relative paths to folders containing test files.
73
74 try {
75 Redactor.Redaction[] vec = new Redactor.Redaction[7];
76 vec[0] = new Redactor.Redaction(1, new Rect(100, 100, 550, 600), false, "Top Secret");
77 vec[1] = new Redactor.Redaction(2, new Rect(30, 30, 450, 450), true, "Negative Redaction");
78 vec[2] = new Redactor.Redaction(2, new Rect(0, 0, 100, 100), false, "Positive");
79 vec[3] = new Redactor.Redaction(2, new Rect(100, 100, 200, 200), false, "Positive");
80 vec[4] = new Redactor.Redaction(2, new Rect(300, 300, 400, 400), false, "");
81 vec[5] = new Redactor.Redaction(2, new Rect(500, 500, 600, 600), false, "");
82 vec[6] = new Redactor.Redaction(3, new Rect(0, 0, 700, 20), false, "");
83
84 Redactor.Appearance app = new Redactor.Appearance();
85 app.redactionOverlay = true;
86 app.border = false;
87 app.showRedactedContentRegions = true;
88
89 redact(Utils.getAssetTempFile(INPUT_PATH + "newsletter.pdf").getAbsolutePath(), Utils.createExternalFile("redacted.pdf", mFileList).getAbsolutePath(), vec, app);
90
91 mOutputListener.println("Done...");
92 } catch (Exception e) {
93 mOutputListener.printError(e.getStackTrace());
94 }
95
96 for (String file : mFileList) {
97 addToFileList(file);
98 }
99 printFooter(outputListener);
100 }
101
102 public static void redact(String input, String output, Redactor.Redaction[] vec, Redactor.Appearance app) {
103 try (PDFDoc doc = new PDFDoc(input)) {
104 if (doc.initSecurityHandler()) {
105 Redactor.redact(doc, vec, app, false, true);
106 doc.save(output, SDFDoc.SaveMode.REMOVE_UNUSED, null);
107 }
108 } catch (Exception e) {
109 mOutputListener.printError(e.getStackTrace());
110 }
111 }
112
113}

Did you find this helpful?

Trial setup questions?

Ask experts on Discord

Need other help?

Contact Support

Pricing or product questions?

Contact Sales