Redact PDFs - Go Sample Code

Sample 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. Sample code provided in Python, C++, C#, Java, Node.js (JavaScript), PHP, Ruby and VB.

Learn more about our Server SDK.

1//---------------------------------------------------------------------------------------
2// Copyright (c) 2001-2021 by PDFTron Systems Inc. All Rights Reserved.
3// Consult LICENSE.txt regarding license information.
4//---------------------------------------------------------------------------------------
5
6package main
7import (
8 "fmt"
9 . "pdftron"
10)
11
12import "pdftron/Samples/LicenseKey/GO"
13
14// PDF Redactor is a separately licensable Add-on that offers options to remove
15// (not just covering or obscuring) content within a region of PDF.
16// With printed pages, redaction involves blacking-out or cutting-out areas of
17// the printed page. With electronic documents that use formats such as PDF,
18// redaction typically involves removing sensitive content within documents for
19// safe distribution to courts, patent and government institutions, the media,
20// customers, vendors or any other audience with restricted access to the content.
21//
22// The redaction process in PDFNet consists of two steps:
23//
24// a) Content identification: A user applies redact annotations that specify the
25// pieces or regions of content that should be removed. The content for redaction
26// can be identified either interactively (e.g. using 'pdftron.PDF.PDFViewCtrl'
27// as shown in PDFView sample) or programmatically (e.g. using 'pdftron.PDF.TextSearch'
28// or 'pdftron.PDF.TextExtractor'). Up until the next step is performed, the user
29// can see, move and redefine these annotations.
30// b) Content removal: Using 'pdftron.PDF.Redactor.Redact()' the user instructs
31// PDFNet to apply the redact regions, after which the content in the area specified
32// by the redact annotations is removed. The redaction function includes number of
33// options to control the style of the redaction overlay (including color, text,
34// font, border, transparency, etc.).
35//
36// PDFTron Redactor makes sure that if a portion of an image, text, or vector graphics
37// is contained in a redaction region, that portion of the image or path data is
38// destroyed and is not simply hidden with clipping or image masks. PDFNet API can also
39// be used to review and remove metadata and other content that can exist in a PDF
40// document, including XML Forms Architecture (XFA) content and Extensible Metadata
41// Platform (XMP) content.
42
43func Redact(input string, output string, vec VectorRedaction, app Appearance){
44 doc := NewPDFDoc(input)
45 if doc.InitSecurityHandler(){
46 RedactorRedact(doc, vec, app, false, true)
47 doc.Save(output, uint(SDFDocE_linearized))
48 }
49}
50
51func main(){
52
53 // Relative path to the folder containing the test files.
54 inputPath := "../../TestFiles/"
55 outputPath := "../../TestFiles/Output/"
56
57 PDFNetInitialize(PDFTronLicense.Key)
58
59 vec := NewVectorRedaction()
60 vec.Add(NewRedaction(1, NewRect(100.0, 100.0, 550.0, 600.0), false, "Top Secret"))
61 vec.Add(NewRedaction(2, NewRect(30.0, 30.0, 450.0, 450.0), true, "Negative Redaction"))
62 vec.Add(NewRedaction(2, NewRect(0.0, 0.0, 100.0, 100.0), false, "Positive"))
63 vec.Add(NewRedaction(2, NewRect(100.0, 100.0, 200.0, 200.0), false, "Positive"))
64 vec.Add(NewRedaction(2, NewRect(300.0, 300.0, 400.0, 400.0), false, ""))
65 vec.Add(NewRedaction(2, NewRect(500.0, 500.0, 600.0, 600.0), false, ""))
66 vec.Add(NewRedaction(3, NewRect(0.0, 0.0, 700.0, 20.0), false, ""))
67
68 app := NewAppearance()
69 app.SetRedactionOverlay(true)
70 app.SetBorder(false)
71 app.SetShowRedactedContentRegions(true)
72 Redact(inputPath + "newsletter.pdf", outputPath + "redacted.pdf", vec, app)
73
74 PDFNetTerminate()
75 fmt.Println("Done...")
76}

Did you find this helpful?

Trial setup questions?

Ask experts on Discord

Need other help?

Contact Support

Pricing or product questions?

Contact Sales