Redact PDFs - PHP 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<?php
2//---------------------------------------------------------------------------------------
3// Copyright (c) 2001-2023 by Apryse Software Inc. All Rights Reserved.
4// Consult LICENSE.txt regarding license information.
5//---------------------------------------------------------------------------------------
6if(file_exists("../../../PDFNetC/Lib/PDFNetPHP.php"))
7include("../../../PDFNetC/Lib/PDFNetPHP.php");
8include("../../LicenseKey/PHP/LicenseKey.php");
9
10// PDF Redactor is a separately licensable Add-on that offers options to remove
11// (not just covering or obscuring) content within a region of PDF.
12// With printed pages, redaction involves blacking-out or cutting-out areas of
13// the printed page. With electronic documents that use formats such as PDF,
14// redaction typically involves removing sensitive content within documents for
15// safe distribution to courts, patent and government institutions, the media,
16// customers, vendors or any other audience with restricted access to the content.
17//
18// The redaction process in PDFNet consists of two steps:
19//
20// a) Content identification: A user applies redact annotations that specify the
21// pieces or regions of content that should be removed. The content for redaction
22// can be identified either interactively (e.g. using 'pdftron.PDF.PDFViewCtrl'
23// as shown in PDFView sample) or programmatically (e.g. using 'pdftron.PDF.TextSearch'
24// or 'pdftron.PDF.TextExtractor'). Up until the next step is performed, the user
25// can see, move and redefine these annotations.
26// b) Content removal: Using 'pdftron.PDF.Redactor.Redact()' the user instructs
27// PDFNet to apply the redact regions, after which the content in the area specified
28// by the redact annotations is removed. The redaction function includes number of
29// options to control the style of the redaction overlay (including color, text,
30// font, border, transparency, etc.).
31//
32// PDFTron Redactor makes sure that if a portion of an image, text, or vector graphics
33// is contained in a redaction region, that portion of the image or path data is
34// destroyed and is not simply hidden with clipping or image masks. PDFNet API can also
35// be used to review and remove metadata and other content that can exist in a PDF
36// document, including XML Forms Architecture (XFA) content and Extensible Metadata
37// Platform (XMP) content.
38
39function Redact($input, $output, $vec, $app) {
40 $doc = new PDFDoc($input);
41 if ($doc->InitSecurityHandler()) {
42 Redactor::Redact($doc, $vec, $app, false, true);
43 $doc->Save($output, SDFDoc::e_linearized);
44 }
45
46}
47
48 # Relative path to the folder containing the test files.
49 $input_path = getcwd()."/../../TestFiles/";
50 $output_path = $input_path."Output/";
51
52 PDFNet::Initialize($LicenseKey);
53 PDFNet::GetSystemFontList(); // Wait for fonts to be loaded if they haven't already. This is done because PHP can run into errors when shutting down if font loading is still in progress.
54
55 $vec = new VectorRedaction();
56 $vec->push(new Redaction(1, new Rect(100.0, 100.0, 550.0, 600.0), false, "Top Secret"));
57 $vec->push(new Redaction(2, new Rect(30.0, 30.0, 450.0, 450.0), true, "Negative Redaction"));
58 $vec->push(new Redaction(2, new Rect(0.0, 0.0, 100.0, 100.0), false, "Positive"));
59 $vec->push(new Redaction(2, new Rect(100.0, 100.0, 200.0, 200.0), false, "Positive"));
60 $vec->push(new Redaction(2, new Rect(300.0, 300.0, 400.0, 400.0), false, ""));
61 $vec->push(new Redaction(2, new Rect(500.0, 500.0, 600.0, 600.0), false, ""));
62 $vec->push(new Redaction(3, new Rect(0.0, 0.0, 700.0, 20.0), false, ""));
63
64 $app = new Appearance();
65 $app->RedactionOverlay = true;
66 $app->Border = false;
67 $app->ShowRedactedContentRegions = true;
68 Redact($input_path."newsletter.pdf", $output_path."redacted.pdf", $vec, $app);
69 PDFNet::Terminate();
70 echo "Done...\n";
71?>

Did you find this helpful?

Trial setup questions?

Ask experts on Discord

Need other help?

Contact Support

Pricing or product questions?

Contact Sales