Sanitize PDFs - Java 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
6import java.lang.*;
7import java.awt.*;
8
9import com.pdftron.pdf.*;
10import com.pdftron.sdf.SDFDoc;
11
12//------------------------------------------------------------------------------
13// PDFNet's Sanitizer is a security-focused feature that permanently removes
14// hidden, sensitive, or potentially unsafe content from a PDF document.
15// While redaction targets visible page content such as text or graphics,
16// sanitization focuses on non-visual elements and embedded structures.
17//
18// PDFNet Sanitizer ensures hidden or inactive content is destroyed,
19// not merely obscured or disabled. This prevents leakage of sensitive
20// data such as authoring details, editing history, private identifiers,
21// and residual form entries, and neutralizes scripts or attachments.
22//
23// Sanitization is recommended prior to external sharing with clients,
24// partners, or regulatory bodies. It helps align with privacy policies
25// and compliance requirements by permanently removing non-visual data.
26//------------------------------------------------------------------------------
27public class PDFSanitizeTest {
28
29 public static void main(String[] args) {
30 // Relative paths to folders containing test files.
31 String input_path = "../../TestFiles/";
32 String output_path = "../../TestFiles/Output/";
33
34 PDFNet.initialize(PDFTronLicense.Key());
35
36
37 // The following example illustrates how to retrieve the existing
38 // sanitizable content categories within a document.
39 try (PDFDoc doc = new PDFDoc(input_path + "numbered.pdf")) {
40 doc.initSecurityHandler();
41
42 SanitizeOptions opts = Sanitizer.getSanitizableContent(doc);
43 if (opts.getMetadata())
44 {
45 System.out.println("Document has metadata.");
46 }
47 if (opts.getMarkups())
48 {
49 System.out.println("Document has markups.");
50 }
51 if (opts.getHiddenLayers())
52 {
53 System.out.println("Document has hidden layers.");
54 }
55 System.out.println("Done...");
56 } catch (Exception e) {
57 e.printStackTrace();
58 }
59
60
61 // The following example illustrates how to sanitize a document with default options,
62 // which will remove all sanitizable content present within a document.
63 try (PDFDoc doc = new PDFDoc(input_path + "financial.pdf")) {
64 doc.initSecurityHandler();
65
66 Sanitizer.sanitizeDocument(doc);
67 doc.save(output_path + "financial_sanitized.pdf", SDFDoc.SaveMode.LINEARIZED, null);
68 System.out.println("Done...");
69 } catch (Exception e) {
70 e.printStackTrace();
71 }
72
73
74 // The following example illustrates how to sanitize a document with custom set options,
75 // which will only remove the content categories specified by the options object.
76 try (PDFDoc doc = new PDFDoc(input_path + "form1.pdf")) {
77 doc.initSecurityHandler();
78
79 SanitizeOptions options = new SanitizeOptions();
80 options.setMetadata(true);
81 options.setFormData(true);
82 options.setBookmarks(true);
83
84 Sanitizer.sanitizeDocument(doc, options);
85 doc.save(output_path + "form1_sanitized.pdf", SDFDoc.SaveMode.LINEARIZED, null);
86 System.out.println("Done...");
87 } catch (Exception e) {
88 e.printStackTrace();
89 }
90
91 PDFNet.terminate();
92 }
93}

Did you find this helpful?

Trial setup questions?

Ask experts on Discord

Need other help?

Contact Support

Pricing or product questions?

Contact Sales