Redact PDFs - Python Sample Code

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.

Learn more about our Server SDK.

1#---------------------------------------------------------------------------------------
2# Copyright (c) 2001-2023 by Apryse Software Inc. All Rights Reserved.
3# Consult LICENSE.txt regarding license information.
4#---------------------------------------------------------------------------------------
5
6import site
7site.addsitedir("../../../PDFNetC/Lib")
8import sys
9from PDFNetPython import *
10
11sys.path.append("../../LicenseKey/PYTHON")
12from LicenseKey import *
13
14
15# PDF Redactor is a separately licensable Add-on that offers options to remove
16# (not just covering or obscuring) content within a region of PDF.
17# With printed pages, redaction involves blacking-out or cutting-out areas of
18# the printed page. With electronic documents that use formats such as PDF,
19# redaction typically involves removing sensitive content within documents for
20# safe distribution to courts, patent and government institutions, the media,
21# customers, vendors or any other audience with restricted access to the content.
22#
23# The redaction process in PDFNet consists of two steps:
24#
25# a) Content identification: A user applies redact annotations that specify the
26# pieces or regions of content that should be removed. The content for redaction
27# can be identified either interactively (e.g. using 'pdftron.PDF.PDFViewCtrl'
28# as shown in PDFView sample) or programmatically (e.g. using 'pdftron.PDF.TextSearch'
29# or 'pdftron.PDF.TextExtractor'). Up until the next step is performed, the user
30# can see, move and redefine these annotations.
31# b) Content removal: Using 'pdftron.PDF.Redactor.Redact()' the user instructs
32# PDFNet to apply the redact regions, after which the content in the area specified
33# by the redact annotations is removed. The redaction function includes number of
34# options to control the style of the redaction overlay (including color, text,
35# font, border, transparency, etc.).
36#
37# PDFTron Redactor makes sure that if a portion of an image, text, or vector graphics
38# is contained in a redaction region, that portion of the image or path data is
39# destroyed and is not simply hidden with clipping or image masks. PDFNet API can also
40# be used to review and remove metadata and other content that can exist in a PDF
41# document, including XML Forms Architecture (XFA) content and Extensible Metadata
42# Platform (XMP) content.
43
44def Redact(input, output, vec, app):
45 doc = PDFDoc(input)
46 if doc.InitSecurityHandler():
47 Redactor.Redact(doc, vec, app, False, True)
48 doc.Save(output, SDFDoc.e_linearized)
49
50
51def main():
52 # Relative path to the folder containing the test files.
53 input_path = "../../TestFiles/"
54 output_path = "../../TestFiles/Output/"
55
56 PDFNet.Initialize(LicenseKey)
57
58 vec = VectorRedaction()
59 vec.append(Redaction(1, Rect(100, 100, 550, 600), False, "Top Secret"))
60 vec.append(Redaction(2, Rect(30, 30, 450, 450), True, "Negative Redaction"))
61 vec.append(Redaction(2, Rect(0, 0, 100, 100), False, "Positive"))
62 vec.append(Redaction(2, Rect(100, 100, 200, 200), False, "Positive"))
63 vec.append(Redaction(2, Rect(300, 300, 400, 400), False, ""))
64 vec.append(Redaction(2, Rect(500, 500, 600, 600), False, ""))
65 vec.append(Redaction(3, Rect(0, 0, 700, 20), False, ""))
66
67 app = Appearance()
68 app.RedactionOverlay = True
69 app.Border = False
70 app.ShowRedactedContentRegions = True
71 Redact(input_path + "newsletter.pdf", output_path + "redacted.pdf", vec, app)
72
73 PDFNet.Terminate()
74 print("Done...")
75
76if __name__ == '__main__':
77 main()

Did you find this helpful?

Trial setup questions?

Ask experts on Discord

Need other help?

Contact Support

Pricing or product questions?

Contact Sales