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}
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
16import java.util.*
17
18/**
19 * PDF Redactor is a separately licensable Add-on that offers options to remove
20 * (not just covering or obscuring) content within a region of PDF.
21 * With printed pages, redaction involves blacking-out or cutting-out areas of
22 * the printed page. With electronic documents that use formats such as PDF,
23 * redaction typically involves removing sensitive content within documents for
24 * safe distribution to courts, patent and government institutions, the media,
25 * customers, vendors or any other audience with restricted access to the content.
26 *
27 *
28 * The redaction process in PDFNet consists of two steps:
29 *
30 *
31 * a) Content identification: A user applies redact annotations that specify the
32 * pieces or regions of content that should be removed. The content for redaction
33 * can be identified either interactively (e.g. using 'com.pdftron.pdf.PDFViewCtrl'
34 * as shown in PDFView sample) or programmatically (e.g. using 'com.pdftron.pdf.TextSearch'
35 * or 'com.pdftron.pdf.TextExtractor'). Up until the next step is performed, the user
36 * can see, move and redefine these annotations.
37 * b) Content removal: Using 'com.pdftron.pdf.Redactor.Redact()' the user instructs
38 * PDFNet to apply the redact regions, after which the content in the area specified
39 * by the redact annotations is removed. The redaction function includes number of
40 * options to control the style of the redaction overlay (including color, text,
41 * font, border, transparency, etc.).
42 *
43 *
44 * PDFTron Redactor makes sure that if a portion of an image, text, or vector graphics
45 * is contained in a redaction region, that portion of the image or path data is
46 * destroyed and is not simply hidden with clipping or image masks. PDFNet API can also
47 * be used to review and remove metadata and other content that can exist in a PDF
48 * document, including XML Forms Architecture (XFA) content and Extensible Metadata
49 * Platform (XMP) content.
50 */
51
52class PDFRedactTest : PDFNetSample() {
53 init {
54 setTitle(R.string.sample_pdfredact_title)
55 setDescription(R.string.sample_pdfredact_description)
56
57 // The standard library does not include the Redaction feature.
58 // If using the full library, please comment out the following
59 // call.
60 // DisableRun();
61 }
62
63 override fun run(outputListener: OutputListener?) {
64 super.run(outputListener)
65 mOutputListener = outputListener
66 mFileList.clear()
67 printHeader(outputListener!!)
68 // Relative paths to folders containing test files.
69
70 try {
71 val vec = arrayOfNulls<Redactor.Redaction>(7)
72 vec[0] = Redactor.Redaction(1, Rect(100.0, 100.0, 550.0, 600.0), false, "Top Secret")
73 vec[1] = Redactor.Redaction(2, Rect(30.0, 30.0, 450.0, 450.0), true, "Negative Redaction")
74 vec[2] = Redactor.Redaction(2, Rect(0.0, 0.0, 100.0, 100.0), false, "Positive")
75 vec[3] = Redactor.Redaction(2, Rect(100.0, 100.0, 200.0, 200.0), false, "Positive")
76 vec[4] = Redactor.Redaction(2, Rect(300.0, 300.0, 400.0, 400.0), false, "")
77 vec[5] = Redactor.Redaction(2, Rect(500.0, 500.0, 600.0, 600.0), false, "")
78 vec[6] = Redactor.Redaction(3, Rect(0.0, 0.0, 700.0, 20.0), false, "")
79
80 val app = Redactor.Appearance()
81 app.redactionOverlay = true
82 app.border = false
83 app.showRedactedContentRegions = true
84
85 redact(Utils.getAssetTempFile(PDFNetSample.INPUT_PATH + "newsletter.pdf")!!.absolutePath, Utils.createExternalFile("redacted.pdf", mFileList).absolutePath, vec, app)
86
87 mOutputListener!!.println("Done...")
88 } catch (e: Exception) {
89 mOutputListener!!.printError(e.stackTrace)
90 }
91
92 for (file in mFileList) {
93 addToFileList(file)
94 }
95 printFooter(outputListener)
96 }
97
98 companion object {
99
100 private var mOutputListener: OutputListener? = null
101
102 private val mFileList = ArrayList<String>()
103
104 fun redact(input: String, output: String, vec: Array<Redactor.Redaction?>, app: Redactor.Appearance) {
105 try {
106 PDFDoc(input).use { doc ->
107 if (doc.initSecurityHandler()) {
108 Redactor.redact(doc, vec, app, false, true)
109 doc.save(output, SDFDoc.SaveMode.REMOVE_UNUSED, null)
110 }
111 }
112 } catch (e: Exception) {
113 mOutputListener!!.printError(e.stackTrace)
114 }
115
116 }
117 }
118
119}
Did you find this helpful?
Trial setup questions?
Ask experts on DiscordNeed other help?
Contact SupportPricing or product questions?
Contact Sales