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 Xamarin SDK.

1//---------------------------------------------------------------------------------------
2// Copyright (c) 2001-2021 by PDFTron Systems 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
16using NUnit.Framework;
17
18namespace MiscellaneousSamples
19{
20 // PDF Redactor is a separately licensable Add-on that offers options to remove
21 // (not just covering or obscuring) content within a region of PDF.
22 // With printed pages, redaction involves blacking-out or cutting-out areas of
23 // the printed page. With electronic documents that use formats such as PDF,
24 // redaction typically involves removing sensitive content within documents for
25 // safe distribution to courts, patent and government institutions, the media,
26 // customers, vendors or any other audience with restricted access to the content.
27 //
28 // The redaction process in PDFNet consists of two steps:
29 //
30 // a) Content identification: A user applies redact annotations that specify the
31 // pieces or regions of content that should be removed. The content for redaction
32 // can be identified either interactively (e.g. using 'pdftron.PDF.PDFViewCtrl'
33 // as shown in PDFView sample) or programmatically (e.g. using 'pdftron.PDF.TextSearch'
34 // or 'pdftron.PDF.TextExtractor'). Up until the next step is performed, the user
35 // can see, move and redefine these annotations.
36 // b) Content removal: Using 'pdftron.PDF.Redactor.Redact()' the user instructs
37 // PDFNet to apply the redact regions, after which the content in the area specified
38 // by the redact annotations is removed. The redaction function includes number of
39 // options to control the style of the redaction overlay (including color, text,
40 // font, border, transparency, etc.).
41 //
42 // PDFTron Redactor makes sure that if a portion of an image, text, or vector graphics
43 // is contained in a redaction region, that portion of the image or path data is
44 // destroyed and is not simply hidden with clipping or image masks. PDFNet API can also
45 // be used to review and remove metadata and other content that can exist in a PDF
46 // document, including XML Forms Architecture (XFA) content and Extensible Metadata
47 // Platform (XMP) content.
48 [TestFixture]
49 public class PDFRedactTest
50 {
51
52 static void Redact(string input, string output, ArrayList rarr, Redactor.Appearance app)
53 {
54 using (PDFDoc doc = new PDFDoc(input))
55 {
56 doc.InitSecurityHandler();
57 Redactor.Redact(doc, rarr, app, false, true);
58 doc.Save(output, SDFDoc.SaveOptions.e_linearized);
59 }
60 }
61
62 /// <summary>
63 /// The following sample illustrates how to redact a PDF document using 'pdftron.PDF.Redactor'.
64 /// </summary>
65 [Test]
66 public static void Sample()
67 {
68
69 const string input_path = "TestFiles/";
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(Utils.GetAssetTempFile(input_path + "newsletter.pdf"), Utils.CreateExternalFile("redacted.pdf"), rarr, app);
87
88 Console.WriteLine("Done...");
89 }
90 catch (PDFNetException e)
91 {
92 Console.WriteLine(e.Message);
93 Assert.True(false);
94 }
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