Sanitize PDFs - Obj-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#import <OBJC/PDFNetOBJC.h>
7#import <Foundation/Foundation.h>
8
9//------------------------------------------------------------------------------
10// PDFNet's Sanitizer is a security-focused feature that permanently removes
11// hidden, sensitive, or potentially unsafe content from a PDF document.
12// While redaction targets visible page content such as text or graphics,
13// sanitization focuses on non-visual elements and embedded structures.
14//
15// PDFNet Sanitizer ensures hidden or inactive content is destroyed,
16// not merely obscured or disabled. This prevents leakage of sensitive
17// data such as authoring details, editing history, private identifiers,
18// and residual form entries, and neutralizes scripts or attachments.
19//
20// Sanitization is recommended prior to external sharing with clients,
21// partners, or regulatory bodies. It helps align with privacy policies
22// and compliance requirements by permanently removing non-visual data.
23//------------------------------------------------------------------------------
24
25int main(int argc, char *argv[])
26{
27 @autoreleasepool {
28
29 int ret = 0;
30
31 [PTPDFNet Initialize: 0];
32
33
34 // The following example illustrates how to retrieve the existing
35 // sanitizable content categories within a document.
36 @try
37 {
38 PTPDFDoc *doc = [[PTPDFDoc alloc] initWithFilepath: @"../../TestFiles/numbered.pdf"];
39 [doc InitSecurityHandler];
40 PTSanitizeOptions *opts = [PTSanitizer GetSanitizableContent: doc];
41
42 if ([opts GetMetadata]) {
43 NSLog(@"Document has metadata.");
44 }
45 if ([opts GetMarkups]) {
46 NSLog(@"Document has markups.");
47 }
48 if ([opts GetHiddenLayers]) {
49 NSLog(@"Document has hidden layers.");
50 }
51 NSLog(@"Done...");
52 }
53 @catch(NSException *e)
54 {
55 NSLog(@"%@", e.reason);
56 ret = 1;
57 }
58
59
60 // The following example illustrates how to sanitize a document with default options,
61 // which will remove all sanitizable content present within a document.
62 @try
63 {
64 PTPDFDoc *doc = [[PTPDFDoc alloc] initWithFilepath: @"../../TestFiles/financial.pdf"];
65 [doc InitSecurityHandler];
66
67 [PTSanitizer SanitizeDocument: doc options: nil];
68 [doc SaveToFile: @"../../TestFiles/Output/financial_sanitized.pdf" flags: e_ptlinearized ];
69 NSLog(@"Done...");
70 }
71 @catch(NSException *e)
72 {
73 NSLog(@"%@", e.reason);
74 ret = 1;
75 }
76
77
78 // The following example illustrates how to sanitize a document with custom set options,
79 // which will only remove the content categories specified by the options object.
80 @try
81 {
82 PTSanitizeOptions *opts = [[PTSanitizeOptions alloc] init];
83 [opts SetMetadata: YES];
84 [opts SetFormData: YES];
85 [opts SetBookmarks: YES];
86
87 PTPDFDoc *doc = [[PTPDFDoc alloc] initWithFilepath: @"../../TestFiles/form1.pdf"];
88 [doc InitSecurityHandler];
89
90 [PTSanitizer SanitizeDocument: doc options: opts];
91 [doc SaveToFile: @"../../TestFiles/Output/form1_sanitized.pdf" flags: e_ptlinearized ];
92 NSLog(@"Done...");
93 }
94 @catch(NSException *e)
95 {
96 NSLog(@"%@", e.reason);
97 ret = 1;
98 }
99
100 [PTPDFNet Terminate: 0];
101 return ret;
102 }
103}
104
105

Did you find this helpful?

Trial setup questions?

Ask experts on Discord

Need other help?

Contact Support

Pricing or product questions?

Contact Sales