//// This code is part of Document Solutions for PDF .NET demos.// Copyright (c) MESCIUS inc. All rights reserved.//usingSystem.IO;usingSystem.Drawing;usingSystem.Text.RegularExpressions;usingGrapeCity.Documents.Pdf;usingGrapeCity.Documents.Pdf.Annotations;usingGrapeCity.Documents.Pdf.TextMap;usingGrapeCity.Documents.Pdf.AcroForms; namespaceDsPdfWeb.Demos{// This sample shows how to use the text map to find specific content // in a PDF and mark it for redaction. // Check out the ApplyRedact sample to see how the redact annotations// added by this sample can be applied to actually erase the data.// Also check out the samples in DsPdfViewer section to see how// redact annotations (added programmatically as in this sample,// or via the viewer's UI) can be applied selectively or all together. // The PDF used in this sample was created by TimeSheet.publicclassFindAndRedact {publicintCreatePDF(Stream stream) {var doc = newGcPdfDocument();usingvar fs = File.OpenRead(Path.Combine("Resources", "PDFs", "TimeSheet.pdf")); doc.Load(fs); // Note: Acrobat does not allow applying redactions in a digitally signed// document, so first we find and remove any existing signatures:removeSignatureFields(doc.AcroForm.Fields); // Loop through pages, removing anything that looks like a short date:foreach (var page in doc.Pages) {var tmap = page.GetTextMap();foreach (ITextLine tline in tmap) {if (Regex.Match(tline.Text.Trim(), @"\d+[/-]\w+[/-]\d").Success) {var redact = newRedactAnnotation() {Rect = tline.GetCoords().ToRect(),MarkBorderColor = Color.Red,MarkFillColor = Color.Yellow,Page = page };// If we hadn't already set redact.Page = page, we could do this:// page.Annotations.Add(redact); } } }// Done: doc.Save(stream);return doc.Pages.Count; // This code is from the RemoveSignatureFields sample:staticvoidremoveSignatureFields(FieldCollection fields) {for (int i = fields.Count - 1; i >= 0; --i) {removeSignatureFields(fields[i].Children);if (fields[i] isSignatureField) fields.RemoveAt(i); } } } // Viewer options for SupportApiDemo:publicstaticGcPdfViewerSupportApiDemo.Models.PdfViewerOptionsPdfViewerOptions {get => newGcPdfViewerSupportApiDemo.Models.PdfViewerOptions(GcPdfViewerSupportApiDemo.Models.PdfViewerOptions.Options.AnnotationEditorPanel |GcPdfViewerSupportApiDemo.Models.PdfViewerOptions.Options.ActivateAnnotationEditor, viewerTools: newstring[] { "save", "edit-redact-apply", "$split", "text-selection", "pan", "$zoom", "$fullscreen", "print", "rotate", "download", "doc-properties", "about" }, annotationEditorTools: newstring[] { "save", "edit-redact", "edit-redact-apply" }); } }}