This module uses the DevExpress PDF Document API to redact PDF documents. You can process the sample file or supply your own document. To do the latter, select Upload a File from the file selection drop-down menu.
Specify the text to redact, the overlay text, and choose to apply the redaction on saving or keep it as an annotation. Click Redact Document to update the document and download the result.
using DevExpress.Pdf;
Stream AddRedaction(Stream documentStream, string searchText, string redactionText, bool applyRedaction) {
using var processor = new PdfDocumentProcessor();
processor.LoadDocument(documentStream);
PdfTextSearchResults search;
do {
search = processor.FindText(
searchText,
new PdfTextSearchParameters() {
CaseSensitive = false,
WholeWords = false
});
if(search.Status == PdfTextSearchStatus.Found) {
var annotation = processor.DocumentFacade.Pages[search.Page.GetPageIndex()].AddRedactAnnotation(search.Rectangles);
annotation.Author = "DX Demo";
annotation.FillColor = new PdfRGBColor(0, 0, 0);
annotation.FontColor = new PdfRGBColor(1, 1, 1);
if(!string.IsNullOrEmpty(redactionText))
annotation.OverlayText = redactionText;
annotation.CreationDate = DateTime.UtcNow;
annotation.TextJustification = PdfTextJustification.LeftJustified;
annotation.RepeatText = true;
annotation.FontSize = 0;
}
} while(search.Status == PdfTextSearchStatus.Found);
if(applyRedaction)
processor.DocumentFacade.ApplyRedactAnnotations();
var outputStream = new MemoryStream();
processor.SaveDocument(outputStream);
outputStream.Position = 0;
return outputStream;
}