@@ -1007,11 +1007,11 @@ PdfLoadedDocument loadedDocument = new PdfLoadedDocument("Input.pdf");
10071007PdfRedaction redaction = new PdfRedaction(new RectangleF(150, 150, 60, 24), Color.Transparent);
10081008//Only the text within the redaction bounds should be redacted.
10091009redaction.TextOnly = true;
1010- foreach (PdfLoadedPage loadedPage in document .Pages)
1010+ foreach (PdfLoadedPage loadedPage in loadedDocument .Pages)
10111011{
10121012 loadedPage.AddRedaction(redaction);
10131013}
1014- document .Redact();
1014+ loadedDocument .Redact();
10151015//Save and close the PDF document
10161016loadedDocument.Save("Output.pdf");
10171017loadedDocument.Close(true);
@@ -1029,11 +1029,11 @@ PdfLoadedDocument loadedDocument = new PdfLoadedDocument("input.pdf");
10291029PdfRedaction redaction = new PdfRedaction(new RectangleF(150, 150, 60, 24), Color.Transparent);
10301030//Only the text within the redaction bounds should be redacted.
10311031redaction.TextOnly = true;
1032- foreach (PdfLoadedPage loadedPage in document .Pages)
1032+ foreach (PdfLoadedPage loadedPage in loadedDocument .Pages)
10331033{
10341034 loadedPage.AddRedaction(redaction);
10351035}
1036- document .Redact();
1036+ loadedDocument .Redact();
10371037//Save the document
10381038loadedDocument.Save("Output.pdf");
10391039//Close the document
@@ -1053,10 +1053,10 @@ Dim loadedDocument As PdfLoadedDocument = New PdfLoadedDocument("input.pdf")
10531053'Create PDF redaction for the page
10541054Dim redaction As PdfRedaction = New PdfRedaction(New RectangleF(150, 150, 60, 24), Color.Transparent)
10551055redaction.TextOnly = true;
1056- For Each loadedPage As PdfLoadedPage In document .Pages
1056+ For Each loadedPage As PdfLoadedPage In loadedDocument .Pages
10571057 loadedPage.AddRedaction(redaction)
10581058Next
1059- document .Redact()
1059+ loadedDocument .Redact()
10601060'Save the document
10611061loadedDocument.Save("Output.pdf")
10621062'Close the document
@@ -1079,141 +1079,142 @@ The following code snippet explains how to find text by regular expression patte
10791079
10801080{% highlight c# tabtitle="C# [ Cross-platform] " playgroundButtonLink="https://raw.githubusercontent.com/SyncfusionExamples/PDF-Examples/master/Redaction/Find-text-by-regular-expression-pattern-and-redact-it-from-PDF-document/.NET/Find_text_by_regular_expression/Program.cs " %}
10811081
1082- using Syncfusion.Pdf.Parsing;
1083- using Syncfusion.Pdf.Redaction;
1084- using Syncfusion.Pdf;
1085- using System.Text.RegularExpressions;
1082+ using Syncfusion.Pdf.Parsing;
1083+ using Syncfusion.Pdf.Redaction;
1084+ using Syncfusion.Pdf;
1085+ using System.Text.RegularExpressions;
10861086
1087- //Load the existing PDF document.
1088- PdfLoadedDocument document = new PdfLoadedDocument("Input.pdf");
1087+ //Load the existing PDF document.
1088+ PdfLoadedDocument document = new PdfLoadedDocument("Input.pdf");
10891089
1090- //Get the first page from the document.
1091- PdfLoadedPage page = document.Pages[0] as PdfLoadedPage;
1090+ //Get the first page from the document.
1091+ PdfLoadedPage page = document.Pages[ 0] as PdfLoadedPage;
10921092
1093- TextLineCollection collection = new TextLineCollection();
1094- //Extract text from first page.
1095- string extractedText = page.ExtractText(out collection);
1093+ TextLineCollection collection = new TextLineCollection();
1094+ //Extract text from first page.
1095+ string extractedText = page.ExtractText(out collection);
10961096
1097- foreach (TextLine line in collection.TextLine)
1097+ foreach (TextLine line in collection.TextLine)
1098+ {
1099+ foreach (TextWord word in line.WordCollection)
10981100 {
1099- foreach (TextWord word in line.WordCollection)
1101+ //Define regular expression pattern to search for dates in the format MM/DD/YYYY
1102+ string datePattern = @"\b\d{1,2}\/ \d{1,2}\/ \d{4}\b";
1103+ //Search for dates
1104+ MatchCollection dateMatches = Regex.Matches(word.Text, datePattern);
1105+ //Add redaction if the match found
1106+ foreach (Match dateMatch in dateMatches)
11001107 {
1101- //Define regular expression pattern to search for dates in the format MM/DD/YYYY
1102- string datePattern = @"\b\d{1,2}\/\d{1,2}\/\d{4}\b";
1103- //Search for dates
1104- MatchCollection dateMatches = Regex.Matches(word.Text, datePattern);
1105- //Add redaction if the match found
1106- foreach (Match dateMatch in dateMatches)
1108+ string textToFindAndRedact = dateMatch.Value;
1109+ if (textToFindAndRedact == word.Text)
11071110 {
1108- string textToFindAndRedact = dateMatch.Value;
1109- if (textToFindAndRedact == word.Text)
1110- {
1111- //Create a redaction object.
1112- PdfRedaction redaction = new PdfRedaction(word.Bounds, Syncfusion.Drawing.Color.Black);
1113- //Add a redaction object into the redaction collection of loaded page.
1114- page.AddRedaction(redaction);
1115- }
1111+ //Create a redaction object.
1112+ PdfRedaction redaction = new PdfRedaction(word.Bounds, Syncfusion.Drawing.Color.Black);
1113+ //Add a redaction object into the redaction collection of loaded page.
1114+ page.AddRedaction(redaction);
11161115 }
11171116 }
11181117 }
1118+ }
11191119
1120- //Redact the contents from the PDF document.
1121- document.Redact();
1120+ //Redact the contents from the PDF document.
1121+ document.Redact();
11221122
1123- //Save and close the PDF document
1124- document.Save("Output.pdf");
1125- document.Close(true);
1123+ //Save and close the PDF document
1124+ document.Save("Output.pdf");
1125+ document.Close(true);
11261126
11271127{% endhighlight %}
11281128
11291129{% highlight c# tabtitle="C# [ Windows-specific] " %}
11301130
1131- using Syncfusion.Pdf.Parsing;
1132- using Syncfusion.Pdf.Redaction;
1133- using Syncfusion.Pdf;
1134- using System.Text.RegularExpressions;
1131+ using Syncfusion.Pdf.Parsing;
1132+ using Syncfusion.Pdf.Redaction;
1133+ using Syncfusion.Pdf;
1134+ using System.Text.RegularExpressions;
11351135
1136- //Load a PDF document
1137- PdfLoadedDocument document = new PdfLoadedDocument("Input.pdf");
1136+ //Load a PDF document
1137+ PdfLoadedDocument document = new PdfLoadedDocument("Input.pdf");
11381138
1139- //Get the first page from the document.
1140- PdfLoadedPage page = document.Pages[0] as PdfLoadedPage;
1139+ //Get the first page from the document.
1140+ PdfLoadedPage page = document.Pages[ 0] as PdfLoadedPage;
11411141
1142- TextLineCollection collection = new TextLineCollection();
1143- //Extract text from first page.
1144- string extractedText = page.ExtractText(out collection);
1142+ TextLineCollection collection = new TextLineCollection();
1143+ //Extract text from first page.
1144+ string extractedText = page.ExtractText(out collection);
11451145
1146- foreach (TextLine line in collection.TextLine)
1146+ foreach (TextLine line in collection.TextLine)
1147+ {
1148+ foreach (TextWord word in line.WordCollection)
11471149 {
1148- foreach (TextWord word in line.WordCollection)
1150+ //Define regular expression pattern to search for dates in the format MM/DD/YYYY
1151+ string datePattern = @"\b\d{1,2}\/ \d{1,2}\/ \d{4}\b";
1152+ //Search for dates
1153+ MatchCollection dateMatches = Regex.Matches(word.Text, datePattern);
1154+ //Add redaction if the match found
1155+ foreach (Match dateMatch in dateMatches)
11491156 {
1150- //Define regular expression pattern to search for dates in the format MM/DD/YYYY
1151- string datePattern = @"\b\d{1,2}\/\d{1,2}\/\d{4}\b";
1152- //Search for dates
1153- MatchCollection dateMatches = Regex.Matches(word.Text, datePattern);
1154- //Add redaction if the match found
1155- foreach (Match dateMatch in dateMatches)
1157+ string textToFindAndRedact = dateMatch.Value;
1158+ if (textToFindAndRedact == word.Text)
11561159 {
1157- string textToFindAndRedact = dateMatch.Value;
1158- if (textToFindAndRedact == word.Text)
1159- {
1160- //Create a redaction object.
1161- PdfRedaction redaction = new PdfRedaction(word.Bounds, Syncfusion.Drawing.Color.Black);
1162- //Add a redaction object into the redaction collection of loaded page.
1163- page.Redactions.Add(redaction);
1164- }
1160+ //Create a redaction object.
1161+ PdfRedaction redaction = new PdfRedaction(word.Bounds, Syncfusion.Drawing.Color.Black);
1162+ //Add a redaction object into the redaction collection of loaded page.
1163+ page.Redactions.Add(redaction);
11651164 }
11661165 }
11671166 }
1168- //Save and close the PDF document
1169- document.Save("Output.pdf");
1170- document.Close(true);
1167+ }
1168+
1169+ //Save and close the PDF document
1170+ document.Save("Output.pdf");
1171+ document.Close(true);
11711172
11721173{% endhighlight %}
11731174
11741175{% highlight vb.net tabtitle="VB.NET [ Windows-specific] " %}
11751176
1176- Imports Syncfusion.Pdf.Parsing
1177- Imports Syncfusion.Pdf.Redaction
1178- Imports Syncfusion.Pdf
1179- Imports System.Text.RegularExpressions
1180-
1181- 'Load the existing PDF document.
1182- Dim document As New PdfLoadedDocument("Input.pdf")
1183-
1184- 'Get the first page from the document.
1185- Dim page As PdfLoadedPage = TryCast(document.Pages(0), PdfLoadedPage)
1186-
1187- Dim collection As New TextLineCollection()
1188- 'Extract text from first page.
1189- Dim extractedText As String = page.ExtractText(collection)
1190-
1191- For Each line As TextLine In collection.TextLine
1192- For Each word As TextWord In line.WordCollection
1193- 'Define regular expression pattern to search for dates in the format MM/DD/YYYY
1194- Dim datePattern As String = "\b\d{1,2}\/\d{1,2}\/\d{4}\b"
1195- 'Search for dates
1196- Dim dateMatches As MatchCollection = Regex.Matches(word.Text, datePattern)
1197- 'Add redaction if the match found
1198- For Each dateMatch As Match In dateMatches
1199- Dim textToFindAndRedact As String = dateMatch.Value
1200- If textToFindAndRedact = word.Text Then
1201- 'Create a redaction object.
1202- Dim redaction As New PdfRedaction(word.Bounds, Syncfusion.Drawing.Color.Black)
1203- 'Add a redaction object into the redaction collection of loaded page.
1204- page.AddRedaction(redaction)
1205- End If
1206- Next
1207- Next
1177+ Imports Syncfusion.Pdf.Parsing
1178+ Imports Syncfusion.Pdf.Redaction
1179+ Imports Syncfusion.Pdf
1180+ Imports System.Text.RegularExpressions
1181+
1182+ 'Load the existing PDF document.
1183+ Dim document As New PdfLoadedDocument("Input.pdf")
1184+
1185+ 'Get the first page from the document.
1186+ Dim page As PdfLoadedPage = TryCast(document.Pages(0), PdfLoadedPage)
1187+
1188+ Dim collection As New TextLineCollection()
1189+ 'Extract text from first page.
1190+ Dim extractedText As String = page.ExtractText(collection)
1191+
1192+ For Each line As TextLine In Collection.TextLine
1193+ For Each word As TextWord In line.WordCollection
1194+ 'Define regular expression pattern to search for dates in the format MM/DD/YYYY
1195+ Dim datePattern As String = "\b\d{1,2}\/ \d{1,2}\/ \d{4}\b"
1196+ 'Search for dates
1197+ Dim dateMatches As MatchCollection = Regex.Matches(word.Text, datePattern)
1198+ 'Add redaction if the match found
1199+ For Each dateMatch As Match In dateMatches
1200+ Dim textToFindAndRedact As String = dateMatch.Value
1201+ If textToFindAndRedact = word.Text Then
1202+ 'Create a redaction object.
1203+ Dim redaction As New PdfRedaction(word.Bounds, Syncfusion.Drawing.Color.Black)
1204+ 'Add a redaction object into the redaction collection of loaded page.
1205+ Page.AddRedaction(redaction)
1206+ End If
12081207 Next
1208+ Next
1209+ Next
12091210
1210- 'Redact the contents from the PDF document.
1211- document.Redact()
1211+ 'Redact the contents from the PDF document.
1212+ document.Redact()
12121213
1213- 'Save the document
1214- document.Save("Output.pdf")
1215- 'Close the document
1216- document.Close(True)
1214+ 'Save the document
1215+ document.Save("Output.pdf")
1216+ 'Close the document
1217+ document.Close(True)
12171218
12181219{% endhighlight %}
12191220
0 commit comments