For the complete documentation index, see llms.txt. This page is also available as Markdown.
Sanitization

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.

//---------------------------------------------------------------------------------------
// Copyright (c) 2001-2026 by Apryse Software Inc. All Rights Reserved.
// Consult legal.txt regarding legal and license information.
//---------------------------------------------------------------------------------------

using System;
using System.IO;
using System.Collections;

using pdftron;
using pdftron.Common;
using pdftron.Filters;
using pdftron.SDF;
using pdftron.PDF;

using NUnit.Framework;

//------------------------------------------------------------------------------
// PDFNet's Sanitizer is a security-focused feature that permanently removes
// hidden, sensitive, or potentially unsafe content from a PDF document.
// While redaction targets visible page content such as text or graphics,
// sanitization focuses on non-visual elements and embedded structures.
//
// PDFNet Sanitizer ensures hidden or inactive content is destroyed,
// not merely obscured or disabled. This prevents leakage of sensitive
// data such as authoring details, editing history, private identifiers,
// and residual form entries, and neutralizes scripts or attachments.
//
// Sanitization is recommended prior to external sharing with clients,
// partners, or regulatory bodies. It helps align with privacy policies
// and compliance requirements by permanently removing non-visual data.
//------------------------------------------------------------------------------

namespace MiscellaneousSamples
{

	[TestFixture]
	public class PDFSanitizeTest
	{

		[Test]
		public static void Sample()
		{
			const string input_path =  "TestFiles/";


            // The following example illustrates how to retrieve the existing
            // sanitizable content categories within a document.
			try
			{
         	    using (PDFDoc doc = new PDFDoc(Utils.GetAssetTempFile(input_path + "numbered.pdf")))
    		    {
		            doc.InitSecurityHandler();

			        SanitizeOptions opts = Sanitizer.GetSanitizableContent(doc);
			        if (opts.GetMetadata())
    			    {
	    			    Console.WriteLine("Document has metadata.");
	    		    }
			        if (opts.GetMarkups())
			        {
				        Console.WriteLine("Document has markups.");
			        }
			        if (opts.GetHiddenLayers())
			        {
				        Console.WriteLine("Document has hidden layers.");
			        }
                    Console.WriteLine("Done...");
			    }
			}
			catch (PDFNetException e)
			{
				Console.WriteLine(e.Message);
				Assert.True(false);
			}


    	    // The following example illustrates how to sanitize a document with default options,
    	    // which will remove all sanitizable content present within a document.
    		try
    		{
         	    using (PDFDoc doc = new PDFDoc(Utils.GetAssetTempFile(input_path + "financial.pdf")))
        	    {
    		        doc.InitSecurityHandler();

			        Sanitizer.SanitizeDocument(doc);
    				doc.Save(Utils.CreateExternalFile("financial_sanitized.pdf"), SDFDoc.SaveOptions.e_linearized);
                    Console.WriteLine("Done...");
    			}
    		}
    		catch (PDFNetException e)
    		{
    			Console.WriteLine(e.Message);
    			Assert.True(false);
    		}


	        // The following example illustrates how to sanitize a document with custom set options,
	        // which will only remove the content categories specified by the options object.
    		try
    		{
         	    using (PDFDoc doc = new PDFDoc(Utils.GetAssetTempFile(input_path + "form1.pdf")))
        	    {
    		        doc.InitSecurityHandler();

    		        SanitizeOptions opts = new SanitizeOptions();
		            opts.SetMetadata(true);
        		    opts.SetFormData(true);
        		    opts.SetBookmarks(true);

			        Sanitizer.SanitizeDocument(doc, opts);
    				doc.Save(Utils.CreateExternalFile("form1_sanitized.pdf"), SDFDoc.SaveOptions.e_linearized);
                    Console.WriteLine("Done...");
    			}
    		}
    		catch (PDFNetException e)
    		{
    			Console.WriteLine(e.Message);
    			Assert.True(false);
    		}

		}
	}
}