Sanitize PDFs - C++ Sample Code

This is sample code for using Apryse SDK to remove hidden, non-visual content within PDF documents. Using pdftron.PDF.Sanitizer ensures that if metadata, form data, bookmarks, hidden layers, markup annotations, JavaScript, or file attachments are present in a document, that content is permanently destroyed and is not simply disabled or obscured. Sample code is provided in Python, C++, C#, Java, Node.js (JavaScript), PHP, Ruby, and VB.

Implementation steps

To sanitize 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.

Learn more about Apryse Server SDK.

1//---------------------------------------------------------------------------------------
2// Copyright (c) 2001-2026 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/Sanitizer.h>
9#include <PDF/SanitizeOptions.h>
10#include <iostream>
11
12#include "../../LicenseKey/CPP/LicenseKey.h"
13
14
15using namespace std;
16
17using namespace pdftron;
18using namespace Common;
19using namespace SDF;
20using namespace PDF;
21
22
23//------------------------------------------------------------------------------
24// PDFNet's Sanitizer is a security-focused feature that permanently removes
25// hidden, sensitive, or potentially unsafe content from a PDF document.
26// While redaction targets visible page content such as text or graphics,
27// sanitization focuses on non-visual elements and embedded structures.
28//
29// PDFNet Sanitizer ensures hidden or inactive content is destroyed,
30// not merely obscured or disabled. This prevents leakage of sensitive
31// data such as authoring details, editing history, private identifiers,
32// and residual form entries, and neutralizes scripts or attachments.
33//
34// Sanitization is recommended prior to external sharing with clients,
35// partners, or regulatory bodies. It helps align with privacy policies
36// and compliance requirements by permanently removing non-visual data.
37//------------------------------------------------------------------------------
38
39int main(int argc, char *argv[])
40{
41 int ret = 0;
42 PDFNet::Initialize(LicenseKey);
43
44 // Relative paths to folders containing test files.
45 string input_path = "../../TestFiles/";
46 string output_path = "../../TestFiles/Output/";
47
48 // The following example illustrates how to retrieve the existing
49 // sanitizable content categories within a document.
50 try
51 {
52 PDFDoc doc(input_path + "numbered.pdf");
53 doc.InitSecurityHandler();
54
55 SanitizeOptions opts = Sanitizer::GetSanitizableContent(doc);
56 if (opts.GetMetadata())
57 {
58 cout << "Document has metadata." << endl;
59 }
60 if (opts.GetMarkups())
61 {
62 cout << "Document has markups." << endl;
63 }
64 if (opts.GetHiddenLayers())
65 {
66 cout << "Document has hidden layers." << endl;
67 }
68 cout << "Done..." << endl;
69 }
70 catch(Common::Exception& e)
71 {
72 cout << e << endl;
73 ret = 1;
74 }
75 catch(...)
76 {
77 cout << "Unknown Exception" << endl;
78 ret = 1;
79 }
80
81
82 // The following example illustrates how to sanitize a document with default options,
83 // which will remove all sanitizable content present within a document.
84 try
85 {
86 PDFDoc doc(input_path + "financial.pdf");
87 doc.InitSecurityHandler();
88
89 Sanitizer::SanitizeDocument(doc, 0);
90 doc.Save(output_path + "financial_sanitized.pdf", SDFDoc::e_linearized, 0);
91 cout << "Done..." << endl;
92 }
93 catch(Common::Exception& e)
94 {
95 cout << e << endl;
96 ret = 1;
97 }
98 catch(...)
99 {
100 cout << "Unknown Exception" << endl;
101 ret = 1;
102 }
103
104
105 // The following example illustrates how to sanitize a document with custom set options,
106 // which will only remove the content categories specified by the options object.
107 try
108 {
109 SanitizeOptions options;
110 options.SetMetadata(true);
111 options.SetFormData(true);
112 options.SetBookmarks(true);
113
114 PDFDoc doc(input_path + "form1.pdf");
115 doc.InitSecurityHandler();
116
117 Sanitizer::SanitizeDocument(doc, &options);
118 doc.Save(output_path + "form1_sanitized.pdf", SDFDoc::e_linearized, 0);
119 cout << "Done..." << endl;
120 }
121 catch(Common::Exception& e)
122 {
123 cout << e << endl;
124 ret = 1;
125 }
126 catch(...)
127 {
128 cout << "Unknown Exception" << endl;
129 ret = 1;
130 }
131
132 PDFNet::Terminate();
133 return ret;
134}
135

Did you find this helpful?

Trial setup questions?

Ask experts on Discord

Need other help?

Contact Support

Pricing or product questions?

Contact Sales