PDFRedact

Sample C# code for using Apryse SDK to remove potentially sensitive content within PDF documents. Using 'pdftron.PDF.Redactor' makes sure that if a portion of an image, text, or vector graphics is contained in a redaction region, that portion is destroyed and is not simply hidden with clipping or image masks. Learn more about our Server SDK.

1//---------------------------------------------------------------------------------------
2// Copyright (c) 2001-2024 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
16namespace PDFNetSamples
17{
18 // PDF Redactor is a separately licensable Add-on that offers options to remove
19 // (not just covering or obscuring) content within a region of PDF.
20 // With printed pages, redaction involves blacking-out or cutting-out areas of
21 // the printed page. With electronic documents that use formats such as PDF,
22 // redaction typically involves removing sensitive content within documents for
23 // safe distribution to courts, patent and government institutions, the media,
24 // customers, vendors or any other audience with restricted access to the content.
25 //
26 // The redaction process in PDFNet consists of two steps:
27 //
28 // a) Content identification: A user applies redact annotations that specify the
29 // pieces or regions of content that should be removed. The content for redaction
30 // can be identified either interactively (e.g. using 'pdftron.PDF.PDFViewCtrl'
31 // as shown in PDFView sample) or programmatically (e.g. using 'pdftron.PDF.TextSearch'
32 // or 'pdftron.PDF.TextExtractor'). Up until the next step is performed, the user
33 // can see, move and redefine these annotations.
34 // b) Content removal: Using 'pdftron.PDF.Redactor.Redact()' the user instructs
35 // PDFNet to apply the redact regions, after which the content in the area specified
36 // by the redact annotations is removed. The redaction function includes number of
37 // options to control the style of the redaction overlay (including color, text,
38 // font, border, transparency, etc.).
39 //
40 // PDFTron Redactor makes sure that if a portion of an image, text, or vector graphics
41 // is contained in a redaction region, that portion of the image or path data is
42 // destroyed and is not simply hidden with clipping or image masks. PDFNet API can also
43 // be used to review and remove metadata and other content that can exist in a PDF
44 // document, including XML Forms Architecture (XFA) content and Extensible Metadata
45 // Platform (XMP) content.
46 class Class1
47 {
48 private static pdftron.PDFNetLoader pdfNetLoader = pdftron.PDFNetLoader.Instance();
49 static Class1() {}
50
51 static void Redact(string input, string output, ArrayList rarr, Redactor.Appearance app)
52 {
53 using (PDFDoc doc = new PDFDoc(input))
54 {
55 doc.InitSecurityHandler();
56 Redactor.Redact(doc, rarr, app, false, true);
57 doc.Save(output, SDFDoc.SaveOptions.e_linearized);
58 }
59 }
60
61 /// <summary>
62 /// The following sample illustrates how to redact a PDF document using 'pdftron.PDF.Redactor'.
63 /// </summary>
64 static void Main(string[] args)
65 {
66 PDFNet.Initialize(PDFTronLicense.Key);
67
68 string input_path = "../../../../TestFiles/";
69 string output_path = "../../../../TestFiles/Output/";
70 try
71 {
72 ArrayList rarr = new ArrayList();
73 rarr.Add(new Redactor.Redaction(1, new Rect(100, 100, 550, 600), false, "Top Secret"));
74 rarr.Add(new Redactor.Redaction(2, new Rect(30, 30, 450, 450), true, "Negative Redaction"));
75 rarr.Add(new Redactor.Redaction(2, new Rect(0, 0, 100, 100), false, "Positive"));
76 rarr.Add(new Redactor.Redaction(2, new Rect(100, 100, 200, 200), false, "Positive"));
77 rarr.Add(new Redactor.Redaction(2, new Rect(300, 300, 400, 400), false, ""));
78 rarr.Add(new Redactor.Redaction(2, new Rect(500, 500, 600, 600), false, ""));
79 rarr.Add(new Redactor.Redaction(3, new Rect(0, 0, 700, 20), false, ""));
80
81 Redactor.Appearance app = new Redactor.Appearance();
82 app.RedactionOverlay = true;
83 app.Border = false;
84 app.ShowRedactedContentRegions = true;
85
86 Redact(input_path + "newsletter.pdf", output_path + "redacted.pdf", rarr, app);
87
88 Console.WriteLine("Done...");
89 }
90 catch (PDFNetException e)
91 {
92 Console.WriteLine(e.Message);
93 }
94 PDFNet.Terminate();
95 }
96 }
97}

Did you find this helpful?

Trial setup questions?

Ask experts on Discord

Need other help?

Contact Support

Pricing or product questions?

Contact Sales