Redact PDFs - C++ 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-2024 by Apryse Software Inc. All Rights Reserved.
3// Consult legal.txt regarding legal and license information.
4//---------------------------------------------------------------------------------------
5
6#include <PDF/PDFNet.h>
7#include <PDF/PDFDoc.h>
8#include <PDF/Redactor.h>
9
10#include <iostream>
11#include <vector>
12#include "../../LicenseKey/CPP/LicenseKey.h"
13
14using namespace std;
15
16using namespace pdftron;
17using namespace Common;
18using namespace SDF;
19using namespace PDF;
20
21// PDF Redactor is a separately licensable Add-on that offers options to remove
22// (not just covering or obscuring) content within a region of PDF.
23// With printed pages, redaction involves blacking-out or cutting-out areas of
24// the printed page. With electronic documents that use formats such as PDF,
25// redaction typically involves removing sensitive content within documents for
26// safe distribution to courts, patent and government institutions, the media,
27// customers, vendors or any other audience with restricted access to the content.
28//
29// The redaction process in PDFNet consists of two steps:
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 'pdftron.PDF.PDFViewCtrl'
34// as shown in PDFView sample) or programmatically (e.g. using 'pdftron.PDF.TextSearch'
35// or '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 '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// PDFTron Redactor makes sure that if a portion of an image, text, or vector graphics
44// is contained in a redaction region, that portion of the image or path data is
45// destroyed and is not simply hidden with clipping or image masks. PDFNet API can also
46// be used to review and remove metadata and other content that can exist in a PDF
47// document, including XML Forms Architecture (XFA) content and Extensible Metadata
48// Platform (XMP) content.
49
50static void Redact(const string& input, const string& output, const vector<Redactor::Redaction>& vec, Redactor::Appearance app)
51{
52 PDFDoc doc(input);
53 if (doc.InitSecurityHandler()) {
54 Redactor::Redact(doc, vec, app, false, true);
55 doc.Save(output, SDFDoc::e_linearized, 0);
56 }
57}
58
59int main(int argc, char *argv[])
60{
61 int ret = 0;
62
63 // Relative paths to folders containing test files.
64 string input_path = "../../TestFiles/";
65 string output_path = "../../TestFiles/Output/";
66
67 PDFNet::Initialize(LicenseKey);
68
69 try
70 {
71 vector<Redactor::Redaction> vec;
72 vec.push_back(Redactor::Redaction(1, Rect(100, 100, 550, 600), false, "Top Secret"));
73 vec.push_back(Redactor::Redaction(2, Rect(30, 30, 450, 450), true, "Negative Redaction"));
74 vec.push_back(Redactor::Redaction(2, Rect(0, 0, 100, 100), false, "Positive"));
75 vec.push_back(Redactor::Redaction(2, Rect(100, 100, 200, 200), false, "Positive"));
76 vec.push_back(Redactor::Redaction(2, Rect(300, 300, 400, 400), false, ""));
77 vec.push_back(Redactor::Redaction(2, Rect(500, 500, 600, 600), false, ""));
78 vec.push_back(Redactor::Redaction(3, Rect(0, 0, 700, 20), false, ""));
79
80 Redactor::Appearance app;
81 app.RedactionOverlay = true;
82 app.Border = false;
83 app.ShowRedactedContentRegions = true;
84
85 Redact(input_path + "newsletter.pdf", output_path + "redacted.pdf", vec, app);
86
87 cout << "Done..." << endl;
88 }
89 catch(Common::Exception& e)
90 {
91 cout << e << endl;
92 ret = 1;
93 }
94 catch(...)
95 {
96 cout << "Unknown Exception" << endl;
97 ret = 1;
98 }
99
100 PDFNet::Terminate();
101 return ret;
102}

Did you find this helpful?

Trial setup questions?

Ask experts on Discord

Need other help?

Contact Support

Pricing or product questions?

Contact Sales