Sanitize PDFs

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
6using System;
7using System.IO;
8using System.Collections;
9
10using pdftron;
11using pdftron.Common;
12using pdftron.Filters;
13using pdftron.SDF;
14using pdftron.PDF;
15
16using NUnit.Framework;
17
18//------------------------------------------------------------------------------
19// PDFNet's Sanitizer is a security-focused feature that permanently removes
20// hidden, sensitive, or potentially unsafe content from a PDF document.
21// While redaction targets visible page content such as text or graphics,
22// sanitization focuses on non-visual elements and embedded structures.
23//
24// PDFNet Sanitizer ensures hidden or inactive content is destroyed,
25// not merely obscured or disabled. This prevents leakage of sensitive
26// data such as authoring details, editing history, private identifiers,
27// and residual form entries, and neutralizes scripts or attachments.
28//
29// Sanitization is recommended prior to external sharing with clients,
30// partners, or regulatory bodies. It helps align with privacy policies
31// and compliance requirements by permanently removing non-visual data.
32//------------------------------------------------------------------------------
33
34namespace MiscellaneousSamples
35{
36
37 [TestFixture]
38 public class PDFSanitizeTest
39 {
40
41 [Test]
42 public static void Sample()
43 {
44 const string input_path = "TestFiles/";
45
46
47 // The following example illustrates how to retrieve the existing
48 // sanitizable content categories within a document.
49 try
50 {
51 using (PDFDoc doc = new PDFDoc(Utils.GetAssetTempFile(input_path + "numbered.pdf")))
52 {
53 doc.InitSecurityHandler();
54
55 SanitizeOptions opts = Sanitizer.GetSanitizableContent(doc);
56 if (opts.GetMetadata())
57 {
58 Console.WriteLine("Document has metadata.");
59 }
60 if (opts.GetMarkups())
61 {
62 Console.WriteLine("Document has markups.");
63 }
64 if (opts.GetHiddenLayers())
65 {
66 Console.WriteLine("Document has hidden layers.");
67 }
68 Console.WriteLine("Done...");
69 }
70 }
71 catch (PDFNetException e)
72 {
73 Console.WriteLine(e.Message);
74 Assert.True(false);
75 }
76
77
78 // The following example illustrates how to sanitize a document with default options,
79 // which will remove all sanitizable content present within a document.
80 try
81 {
82 using (PDFDoc doc = new PDFDoc(Utils.GetAssetTempFile(input_path + "financial.pdf")))
83 {
84 doc.InitSecurityHandler();
85
86 Sanitizer.SanitizeDocument(doc);
87 doc.Save(Utils.CreateExternalFile("financial_sanitized.pdf"), SDFDoc.SaveOptions.e_linearized);
88 Console.WriteLine("Done...");
89 }
90 }
91 catch (PDFNetException e)
92 {
93 Console.WriteLine(e.Message);
94 Assert.True(false);
95 }
96
97
98 // The following example illustrates how to sanitize a document with custom set options,
99 // which will only remove the content categories specified by the options object.
100 try
101 {
102 using (PDFDoc doc = new PDFDoc(Utils.GetAssetTempFile(input_path + "form1.pdf")))
103 {
104 doc.InitSecurityHandler();
105
106 SanitizeOptions opts = new SanitizeOptions();
107 opts.SetMetadata(true);
108 opts.SetFormData(true);
109 opts.SetBookmarks(true);
110
111 Sanitizer.SanitizeDocument(doc, opts);
112 doc.Save(Utils.CreateExternalFile("form1_sanitized.pdf"), SDFDoc.SaveOptions.e_linearized);
113 Console.WriteLine("Done...");
114 }
115 }
116 catch (PDFNetException e)
117 {
118 Console.WriteLine(e.Message);
119 Assert.True(false);
120 }
121
122 }
123 }
124}
125

Did you find this helpful?

Trial setup questions?

Ask experts on Discord

Need other help?

Contact Support

Pricing or product questions?

Contact Sales
Use Apryse SDK's Sanitization to remove hidden, non-visual content within PDF documents and strip sensitive metadata, embedded scripts, and residual data for safe external sharing - sample code in Python, C++, C#, Java, Node.js (JavaScript), PHP, Ruby, Go, VB and Obj-C | Apryse documentation