Redact PDFs - Java Sample Code

Requirements
View Demo

Sample 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. Sample code provided in Python, C++, C#, Java, Node.js (JavaScript), PHP, Ruby and VB.

Implementation steps

To redact files with Apryse Server SDK:

Step 1: Follow get started with Server SDK in your preferred language or framework
Step 2: Add the sample code provided in this guide

To use this feature in production, your license key will need the Redaction Package. Trial keys already include all packages.

Learn more about our Server SDK.

1//---------------------------------------------------------------------------------------
2// Copyright (c) 2001-2024 by Apryse Software Inc. All Rights Reserved.
3// Consult legal.txt regarding legal and license information.
4//---------------------------------------------------------------------------------------
5
6import java.lang.*;
7import java.awt.*;
8
9import com.pdftron.pdf.*;
10import com.pdftron.sdf.SDFDoc;
11
12
13// PDF Redactor is a separately licensable Add-on that offers options to remove
14// (not just covering or obscuring) content within a region of PDF.
15// With printed pages, redaction involves blacking-out or cutting-out areas of
16// the printed page. With electronic documents that use formats such as PDF,
17// redaction typically involves removing sensitive content within documents for
18// safe distribution to courts, patent and government institutions, the media,
19// customers, vendors or any other audience with restricted access to the content.
20//
21// The redaction process in PDFNet consists of two steps:
22//
23// a) Content identification: A user applies redact annotations that specify the
24// pieces or regions of content that should be removed. The content for redaction
25// can be identified either interactively (e.g. using 'pdftron.PDF.PDFViewCtrl'
26// as shown in PDFView sample) or programmatically (e.g. using 'pdftron.PDF.TextSearch'
27// or 'pdftron.PDF.TextExtractor'). Up until the next step is performed, the user
28// can see, move and redefine these annotations.
29// b) Content removal: Using 'pdftron.PDF.Redactor.Redact()' the user instructs
30// PDFNet to apply the redact regions, after which the content in the area specified
31// by the redact annotations is removed. The redaction function includes number of
32// options to control the style of the redaction overlay (including color, text,
33// font, border, transparency, etc.).
34//
35// PDFTron Redactor makes sure that if a portion of an image, text, or vector graphics
36// is contained in a redaction region, that portion of the image or path data is
37// destroyed and is not simply hidden with clipping or image masks. PDFNet API can also
38// be used to review and remove metadata and other content that can exist in a PDF
39// document, including XML Forms Architecture (XFA) content and Extensible Metadata
40// Platform (XMP) content.
41public class PDFRedactTest {
42
43 public static void redact(String input, String output, Redactor.Redaction[] vec, Redactor.Appearance app) {
44 try (PDFDoc doc = new PDFDoc(input)) {
45 if (doc.initSecurityHandler()) {
46 Redactor.redact(doc, vec, app, false, true);
47 doc.save(output, SDFDoc.SaveMode.REMOVE_UNUSED, null);
48 }
49 } catch (Exception e) {
50 e.printStackTrace();
51 }
52 }
53
54 public static void main(String[] args) {
55 // Relative paths to folders containing test files.
56 String input_path = "../../TestFiles/";
57 String output_path = "../../TestFiles/Output/";
58
59 PDFNet.initialize(PDFTronLicense.Key());
60
61 try {
62 Redactor.Redaction[] vec = new Redactor.Redaction[7];
63 vec[0] = new Redactor.Redaction(1, new Rect(100, 100, 550, 600), false, "Top Secret");
64 vec[1] = new Redactor.Redaction(2, new Rect(30, 30, 450, 450), true, "Negative Redaction");
65 vec[2] = new Redactor.Redaction(2, new Rect(0, 0, 100, 100), false, "Positive");
66 vec[3] = new Redactor.Redaction(2, new Rect(100, 100, 200, 200), false, "Positive");
67 vec[4] = new Redactor.Redaction(2, new Rect(300, 300, 400, 400), false, "");
68 vec[5] = new Redactor.Redaction(2, new Rect(500, 500, 600, 600), false, "");
69 vec[6] = new Redactor.Redaction(3, new Rect(0, 0, 700, 20), false, "");
70
71 Redactor.Appearance app = new Redactor.Appearance();
72 app.redactionOverlay = true;
73 app.border = false;
74 app.showRedactedContentRegions = true;
75
76 redact(input_path + "newsletter.pdf", output_path + "redacted.pdf", vec, app);
77
78 System.out.println("Done...");
79 } catch (Exception e) {
80 e.printStackTrace();
81 }
82 PDFNet.terminate();
83 }
84}

Did you find this helpful?

Trial setup questions?

Ask experts on Discord

Need other help?

Contact Support

Pricing or product questions?

Contact Sales