officefileapi-405392-pdf-document-api-annotations-redact-annotations.md
Redaction annotations are tools used to remove sensitive or confidential information from a document. These annotations allow users to mark areas of text, images, or other content for redaction.
PDF Document API allows you to create, delete, and flatten annotations, edit their content, and add related comments and reviews. You can implement the following scenarios:
Create redaction annotations and save the document You can send the result for review. The recipient can approve or comment on the redaction annotations before applying them.Load a document with redaction annotations and inspect redacted areasObtain annotations, review or change the required parameters, and apply or remove redaction annotations if necessary.Create and apply all redaction annotations in your PDF documentLoad a document with redaction annotations or create annotations in code, and apply these annotations. Once applied, the redacted content is removed so that the information cannot be recovered or viewed by unauthorized viewers.
This topic contains detailed information on redaction annotations. Refer to the following help topic for more information on all annotations and their common features: Annotations in PDF Documents.
The PdfPageFacade.Annotations property retrieves all page annotation properties. Use the PdfDocumentFacade.Pages property to access the PdfPageFacade class.
Use the PdfPageFacade.AddRedactAnnotation method to create a redaction annotation. When adding a redaction, you can specify multiple areas on the page (redact information from multiple places simultaneously).
The following code snippet searches for specific words and creates redaction annotations over them:
using DevExpress.Pdf;
PdfDocumentProcessor pdfProcessor = new PdfDocumentProcessor();
pdfProcessor.LoadDocument("..\\..\\Invoice.pdf");
PdfDocumentFacade documentFacade = pdfProcessor.DocumentFacade;
PdfTextSearchParameters searchParameters =
new PdfTextSearchParameters();
searchParameters.CaseSensitive = true;
searchParameters.WholeWords = true;
string[] search = new string[] { "Maria Anders", "030-0074321", "[email protected]" };
foreach (string word in search)
{
PdfTextSearchResults results = pdfProcessor.FindText(word, searchParameters);
// If the text is found, create an annotation
if (results.Status == PdfTextSearchStatus.Found) {
var pageIndex = results.Page.GetPageIndex();
PdfRedactAnnotationFacade redactAnnotation =
documentFacade.Pages[pageIndex].AddRedactAnnotation(results.Rectangles);
redactAnnotation.Author = "Jane Doe";
// Set the redaction annotation appearance
redactAnnotation.FontColor = new PdfRGBColor(0, 0, 0);
redactAnnotation.FillColor = new PdfRGBColor(1, 1, 1);
redactAnnotation.FontName = "Arial";
redactAnnotation.FontSize = 0; // Enables font auto-size
redactAnnotation.OverlayText = "Classified";
redactAnnotation.TextJustification = PdfTextJustification.Centered;
redactAnnotation.RepeatText = false;
}
}
// Save the document with the redaction annotation
// and send it for review
pdfProcessor.SaveDocument("output_to_review.pdf");
Imports DevExpress.Pdf
Private pdfProcessor As New PdfDocumentProcessor()
pdfProcessor.LoadDocument("..\..\Invoice.pdf")
Dim documentFacade As PdfDocumentFacade = pdfProcessor.DocumentFacade
Dim searchParameters As New PdfTextSearchParameters()
searchParameters.CaseSensitive = True
searchParameters.WholeWords = True
Dim search() As String = { "Maria Anders", "030-0074321", "[email protected]" }
For Each word As String In search
Dim results As PdfTextSearchResults = pdfProcessor.FindText(word, searchParameters)
' If the text is found, create an annotation
If results.Status = PdfTextSearchStatus.Found Then
Dim pageIndex = results.Page.GetPageIndex()
Dim redactAnnotation As PdfRedactAnnotationFacade = documentFacade.Pages(pageIndex).AddRedactAnnotation(results.Rectangles)
redactAnnotation.Author = "Jane Doe"
' Set the redaction annotation appearance
redactAnnotation.FontColor = New PdfRGBColor(0, 0, 0)
redactAnnotation.FillColor = New PdfRGBColor(1, 1, 1)
redactAnnotation.FontName = "Arial"
redactAnnotation.FontSize = 0 ' enables font auto-size
redactAnnotation.OverlayText = "Classified"
redactAnnotation.TextJustification = PdfTextJustification.Centered
redactAnnotation.RepeatText = False
End If
Next word
' Save the document with the redaction annotation
' and send it for review
pdfProcessor.SaveDocument("output_to_review.pdf")
The PdfPageFacade.Annotations property returns all page annotation properties. You can filter redaction annotation properties, cast them to the PdfRedactAnnotationFacade class, and use class properties to change annotation parameters.
The following code snippet obtains all redaction annotations, and adds an “approved” review to all annotations from a specific author:
using DevExpress.Pdf;
using (PdfDocumentProcessor processor = new PdfDocumentProcessor())
{
// Load a document
processor.LoadDocument("output_to_review.pdf");
// Access the first page's properties
PdfPageFacade page = processor.DocumentFacade.Pages[0];
// Retrieve all redaction annotations
var redactAnnotations = page.Annotations.Where
(annotation => annotation.Type == PdfAnnotationType.Redaction);
// Change annotation parameters
foreach (PdfRedactAnnotationFacade redaction in redactAnnotations)
{
if(redaction.Author == "Jane Doe")
redaction.AddReview("Nancy Skywalker", PdfAnnotationReviewStatus.Accepted);
}
processor.SaveDocument("output_approved.pdf");
}
Imports DevExpress.Pdf
Using processor As New PdfDocumentProcessor()
' Load a document
processor.LoadDocument("output_to_review.pdf")
' Access the first page's properties
Dim page As PdfPageFacade = processor.DocumentFacade.Pages(0)
' Retrieve all redaction annotations
Dim redactAnnotations = page.Annotations.Where(Function(annotation) annotation.Type = PdfAnnotationType.Redaction)
' Change annotation parameters
For Each redaction As PdfRedactAnnotationFacade In redactAnnotations
If redaction.Author = "Jane Doe" Then
redaction.AddReview("Nancy Skywalker", PdfAnnotationReviewStatus.Accepted)
End If
Next redaction
processor.SaveDocument("output_approved.pdf")
End Using
Call the PdfAnnotationFacade.Remove() method to remove an annotation.
The following code snippet removes all redaction annotations on the first page:
using DevExpress.Pdf;
using (PdfDocumentProcessor processor = new PdfDocumentProcessor()) {
// Load a document
processor.LoadDocument("Invoice.pdf");
// Access the first page's properties
PdfPageFacade page = processor.DocumentFacade.Pages[0];
// Retrieve all rubber stamps
var redactAnnotations = page.Annotations.Where
(annotation => annotation.Type == PdfAnnotationType.Redaction).ToList();
// Remove all rubber stamps
foreach (PdfRedactAnnotationFacade redaction in redactAnnotations)
{
redaction.Remove();
}
processor.SaveDocument("output_deleted.pdf");
}
Imports DevExpress.Pdf
Using processor As New PdfDocumentProcessor()
' Load a document
processor.LoadDocument("Invoice.pdf")
' Access the first page's properties
Dim page As PdfPageFacade = processor.DocumentFacade.Pages(0)
' Retrieve all rubber stamps
Dim redactAnnotations = page.Annotations.Where(Function(annotation) annotation.Type = PdfAnnotationType.Redaction).ToList()
' Remove all rubber stamps
For Each redaction As PdfRedactAnnotationFacade In redactAnnotations
redaction.Remove()
Next redaction
processor.SaveDocument("output_deleted.pdf")
End Using
Note
When you call one of the Flatten… methods for redaction annotations, their overlay parameters (text, fill color, and so on) are not applied and the redacted content is not removed. Use one of the ApplyRedactAnnotations methods to apply these annotations.
You can apply a single redaction annotation, annotations on a specific page, or annotations in an entire document. The following methods are available:
Each method allows you to specify what content type to keep visible in the redaction area. Create the PdfClearContentOptions object, set one of the Clear... properties to false to specify the content type to keep visible, and pass this object as the Apply... method parameter. If the options parameter is not specified, all content is redacted.
Once the redaction annotation is applied, it can no longer be obtained, edited, or removed.
The following code snippet applies redaction annotations made by a specific author:
using DevExpress.Pdf;
using (PdfDocumentProcessor processor = new PdfDocumentProcessor())
{
processor.LoadDocument("output_to_review.pdf");
PdfDocumentFacade documentFacade = processor.DocumentFacade;
foreach (var page in documentFacade.Pages)
{
var redactionAnnotations = page.Annotations.Where(annotation => annotation.Type == PdfAnnotationType.Redaction).ToList();
foreach (PdfRedactAnnotationFacade annotation in redactionAnnotations)
{
if (annotation.Author == "Jane Doe")
annotation.Apply();
}
}
processor.SaveDocument("output_applied.pdf");
}
Using processor As New PdfDocumentProcessor()
Dim documentFacade As PdfDocumentFacade = processor.DocumentFacade
For Each page In documentFacade.Pages
Dim redactionAnnotations = page.Annotations.Where(Function(annotation) TypeOf annotation Is PdfRedactAnnotationFacade)
For Each annotation As PdfRedactAnnotationFacade In redactionAnnotations
' Apply redaction from a specific author
If annotation.Author = "Jane Doe" Then
annotation.Apply()
End If
Next annotation
Next page
End Using