Sanitize PDFs - Go 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
6package main
7import (
8 "fmt"
9 "testing"
10 "flag"
11 . "github.com/pdftron/pdftron-go/v2"
12)
13
14var licenseKey string
15var modulePath string
16
17func init() {
18 flag.StringVar(&licenseKey, "license", "", "License key for Apryse SDK")
19 flag.StringVar(&modulePath, "modulePath", "", "Module path for Apryse SDK")
20}
21
22//------------------------------------------------------------------------------
23// PDFNet's Sanitizer is a security-focused feature that permanently removes
24// hidden, sensitive, or potentially unsafe content from a PDF document.
25// While redaction targets visible page content such as text or graphics,
26// sanitization focuses on non-visual elements and embedded structures.
27//
28// PDFNet Sanitizer ensures hidden or inactive content is destroyed,
29// not merely obscured or disabled. This prevents leakage of sensitive
30// data such as authoring details, editing history, private identifiers,
31// and residual form entries, and neutralizes scripts or attachments.
32//
33// Sanitization is recommended prior to external sharing with clients,
34// partners, or regulatory bodies. It helps align with privacy policies
35// and compliance requirements by permanently removing non-visual data.
36//------------------------------------------------------------------------------
37
38func TestPDFSanitize(t *testing.T){
39
40 // Relative path to the folder containing the test files.
41 inputPath := "../TestFiles/"
42 outputPath := "../TestFiles/Output/"
43
44 PDFNetInitialize(licenseKey)
45
46 // The following example illustrates how to retrieve the existing
47 // sanitizable content categories within a document.
48 {
49 doc := NewPDFDoc(inputPath + "numbered.pdf")
50 doc.InitSecurityHandler()
51 opts := SanitizerGetSanitizableContent(doc)
52 if opts.GetMetadata() {
53 fmt.Println("Document has metadata.")
54 }
55 if opts.GetMarkups() {
56 fmt.Println("Document has markups.")
57 }
58 if opts.GetHiddenLayers() {
59 fmt.Println("Document has hidden layers.")
60 }
61 fmt.Println("Done...")
62 }
63
64 // The following example illustrates how to sanitize a document with default options,
65 // which will remove all sanitizable content present within a document.
66 {
67 doc := NewPDFDoc(inputPath + "financial.pdf")
68 doc.InitSecurityHandler()
69 SanitizerSanitizeDocument(doc, NewSanitizeOptions())
70 doc.Save(outputPath+"financial_sanitized.pdf", uint(SDFDocE_linearized))
71 fmt.Println("Done...")
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 {
77 options := NewSanitizeOptions()
78 options.SetMetadata(true)
79 options.SetFormData(true)
80 options.SetBookmarks(true)
81
82 doc := NewPDFDoc(inputPath + "form1.pdf")
83 doc.InitSecurityHandler()
84 SanitizerSanitizeDocument(doc, options)
85 doc.Save(outputPath+"form1_sanitized.pdf", uint(SDFDocE_linearized))
86 fmt.Println("Done...")
87 }
88
89 PDFNetTerminate()
90}
91
92

Did you find this helpful?

Trial setup questions?

Ask experts on Discord

Need other help?

Contact Support

Pricing or product questions?

Contact Sales
PDF Sanitization Library in Go | Apryse documentation