Skip to content

Commit deca967

Browse files
Merge pull request #2084 from Syncfusion-Content/hotfix/hotfix-v25.1.35
DOCINFRA-2341_merged_using_automation
2 parents 0df603d + 491c168 commit deca967

File tree

1 file changed

+152
-3
lines changed

1 file changed

+152
-3
lines changed

File-Formats/PDF/Working-with-Redaction.md

Lines changed: 152 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -741,9 +741,9 @@ loadedDocument.Close(True)
741741

742742
You can download a complete working sample from [GitHub](https://github.com/SyncfusionExamples/PDF-Examples/tree/master/Redaction/Get-the-result-of-redaction-with-other-information/).
743743

744-
## Redact text content alone on the redated area
744+
## Redact text content alone on the redacted area
745745

746-
You can get the Redact text content alone on the redated area using [RedactionProgress](https://help.syncfusion.com/cr/file-formats/Syncfusion.Pdf.Parsing.PdfLoadedDocument.html#Syncfusion_Pdf_Parsing_PdfLoadedDocument_RedactionProgress) event in [PdfLoadedDocument](https://help.syncfusion.com/cr/file-formats/Syncfusion.Pdf.Parsing.PdfLoadedDocument.html) class.
746+
You can get the Redact text content alone on the redacted area using [RedactionProgress](https://help.syncfusion.com/cr/file-formats/Syncfusion.Pdf.Parsing.PdfLoadedDocument.html#Syncfusion_Pdf_Parsing_PdfLoadedDocument_RedactionProgress) event in [PdfLoadedDocument](https://help.syncfusion.com/cr/file-formats/Syncfusion.Pdf.Parsing.PdfLoadedDocument.html) class.
747747

748748
The code snippet to illustrate the same is given below.
749749

@@ -812,4 +812,153 @@ loadedDocument.Close(True)
812812

813813
{% endtabs %}
814814

815-
You can download a complete working sample from [GitHub](https://github.com/SyncfusionExamples/PDF-Examples/tree/master/Redaction/Redaction/Redact-text-content-alone-on-the-redated-area/).
815+
You can download a complete working sample from [GitHub](https://github.com/SyncfusionExamples/PDF-Examples/tree/master/Redaction/Redact-text-content-alone-on-the-redated-area/).
816+
817+
818+
## Find text by regular expression pattern and redact it from PDF document.
819+
820+
You can find text by regular expression pattern and redact it from PDF document using the [PdfRedaction](https://help.syncfusion.com/cr/file-formats/Syncfusion.Pdf.Redaction.PdfRedaction.html#Syncfusion_Pdf_Redaction_PdfRedaction__ctor_System_Drawing_RectangleF_) class.
821+
822+
The following code snippet explains how to find text by regular expression pattern and redact it from PDF document.
823+
824+
{% tabs %}
825+
826+
{% highlight c# tabtitle="C# [Cross-platform]" %}
827+
828+
//Create stream from an existing PDF document.
829+
FileStream docStream = new FileStream(Path.GetFullPath("Input.pdf"), FileMode.Open, FileAccess.Read);
830+
831+
//Load the existing PDF document.
832+
PdfLoadedDocument document = new PdfLoadedDocument(docStream);
833+
834+
//Get the first page from the document.
835+
PdfLoadedPage page = document.Pages[0] as PdfLoadedPage;
836+
837+
TextLineCollection collection = new TextLineCollection();
838+
//Extract text from first page.
839+
string extractedText = page.ExtractText(out collection);
840+
841+
foreach (TextLine line in collection.TextLine)
842+
{
843+
foreach (TextWord word in line.WordCollection)
844+
{
845+
//Define regular expression pattern to search for dates in the format MM/DD/YYYY
846+
string datePattern = @"\b\d{1,2}\/\d{1,2}\/\d{4}\b";
847+
//Search for dates
848+
MatchCollection dateMatches = Regex.Matches(word.Text, datePattern);
849+
//Add redaction if the match found
850+
foreach (Match dateMatch in dateMatches)
851+
{
852+
string textToFindAndRedact = dateMatch.Value;
853+
if (textToFindAndRedact == word.Text)
854+
{
855+
//Create a redaction object.
856+
PdfRedaction redaction = new PdfRedaction(word.Bounds, Syncfusion.Drawing.Color.Black);
857+
//Add a redaction object into the redaction collection of loaded page.
858+
page.AddRedaction(redaction);
859+
}
860+
}
861+
}
862+
}
863+
864+
//Redact the contents from the PDF document.
865+
document.Redact();
866+
867+
//Creating the stream object
868+
MemoryStream stream = new MemoryStream();
869+
//Save the document
870+
document.Save(stream);
871+
//Close the document
872+
document.Close(true);
873+
874+
{% endhighlight %}
875+
876+
{% highlight c# tabtitle="C# [Windows-specific]" %}
877+
878+
//Load a PDF document
879+
PdfLoadedDocument document = new PdfLoadedDocument("Input.pdf");
880+
881+
//Get the first page from the document.
882+
PdfLoadedPage page = document.Pages[0] as PdfLoadedPage;
883+
884+
TextLineCollection collection = new TextLineCollection();
885+
//Extract text from first page.
886+
string extractedText = page.ExtractText(out collection);
887+
888+
foreach (TextLine line in collection.TextLine)
889+
{
890+
foreach (TextWord word in line.WordCollection)
891+
{
892+
//Define regular expression pattern to search for dates in the format MM/DD/YYYY
893+
string datePattern = @"\b\d{1,2}\/\d{1,2}\/\d{4}\b";
894+
//Search for dates
895+
MatchCollection dateMatches = Regex.Matches(word.Text, datePattern);
896+
//Add redaction if the match found
897+
foreach (Match dateMatch in dateMatches)
898+
{
899+
string textToFindAndRedact = dateMatch.Value;
900+
if (textToFindAndRedact == word.Text)
901+
{
902+
//Create a redaction object.
903+
PdfRedaction redaction = new PdfRedaction(word.Bounds, Syncfusion.Drawing.Color.Black);
904+
//Add a redaction object into the redaction collection of loaded page.
905+
page.Redactions.Add(redaction);
906+
}
907+
}
908+
}
909+
}
910+
//Save and close the PDF document
911+
document.Save("Output.pdf");
912+
document.Close(true);
913+
914+
{% endhighlight %}
915+
916+
{% highlight vb.net tabtitle="VB.NET [Windows-specific]" %}
917+
918+
'Create stream from an existing PDF document.
919+
Dim docStream As New FileStream(Path.GetFullPath("Input.pdf"), FileMode.Open, FileAccess.Read)
920+
921+
'Load the existing PDF document.
922+
Dim document As New PdfLoadedDocument(docStream)
923+
924+
'Get the first page from the document.
925+
Dim page As PdfLoadedPage = TryCast(document.Pages(0), PdfLoadedPage)
926+
927+
Dim collection As New TextLineCollection()
928+
'Extract text from first page.
929+
Dim extractedText As String = page.ExtractText(collection)
930+
931+
For Each line As TextLine In collection.TextLine
932+
For Each word As TextWord In line.WordCollection
933+
'Define regular expression pattern to search for dates in the format MM/DD/YYYY
934+
Dim datePattern As String = "\b\d{1,2}\/\d{1,2}\/\d{4}\b"
935+
'Search for dates
936+
Dim dateMatches As MatchCollection = Regex.Matches(word.Text, datePattern)
937+
'Add redaction if the match found
938+
For Each dateMatch As Match In dateMatches
939+
Dim textToFindAndRedact As String = dateMatch.Value
940+
If textToFindAndRedact = word.Text Then
941+
'Create a redaction object.
942+
Dim redaction As New PdfRedaction(word.Bounds, Syncfusion.Drawing.Color.Black)
943+
'Add a redaction object into the redaction collection of loaded page.
944+
page.AddRedaction(redaction)
945+
End If
946+
Next
947+
Next
948+
Next
949+
950+
'Redact the contents from the PDF document.
951+
document.Redact()
952+
953+
'Creating the stream object
954+
Dim stream As New MemoryStream()
955+
'Save the document
956+
document.Save(stream)
957+
'Close the document
958+
document.Close(True)
959+
960+
{% endhighlight %}
961+
962+
{% endtabs %}
963+
964+
You can download a complete working sample from [GitHub](https://github.com/SyncfusionExamples/PDF-Examples/tree/master/Redaction/Find-text-by-regular-expression-pattern-and-redact-it-from-PDF-document/.NET-Standard).

0 commit comments

Comments
 (0)