Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions PDFRedaction/PDFRedaction/ConsolePopup.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<Window x:Class="PDFRedaction.ConsolePopup"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:PDFRedaction"
mc:Ignorable="d"
Title="ConsolePopup" Height="450" Width="800">

<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="*" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<TextBox x:Name="ConsoleOutput"
FontFamily="Consolas"
FontSize="14"
IsReadOnly="True"
TextWrapping="Wrap"
VerticalScrollBarVisibility="Auto"
Margin="10"
Grid.Row="0"/>
<Button x:Name="CloseButton"
Content="Close"
HorizontalAlignment="Right"
VerticalAlignment="Top"
Margin="10"
Width="75"
Height="25"
Click="CloseButton_Click"
Grid.Row="1"/>
</Grid>

</Window>
40 changes: 40 additions & 0 deletions PDFRedaction/PDFRedaction/ConsolePopup.xaml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace PDFRedaction
{
/// <summary>
/// Interaction logic for ConsolePopup.xaml
/// </summary>
public partial class ConsolePopup : Window
{
public ConsolePopup()
{
InitializeComponent();
}
public void WriteLine(string message)
{
ConsoleOutput.AppendText(message + Environment.NewLine);
ConsoleOutput.ScrollToEnd();
}
public void Clear()
{ ConsoleOutput.Clear(); }

private void CloseButton_Click(object sender, RoutedEventArgs e)
{
ConsoleOutput.Clear(); // Clear the text
this.Close();
}
}
}
Binary file added PDFRedaction/PDFRedaction/Input/input.pdf
Binary file not shown.
28 changes: 21 additions & 7 deletions PDFRedaction/PDFRedaction/MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,32 @@
<Grid.RowDefinitions>
<RowDefinition Height="0.5*" ></RowDefinition>
<RowDefinition Height="0.5*" ></RowDefinition>
</Grid.RowDefinitions>
</Grid.RowDefinitions>
<TextBlock Margin="10" TextWrapping="Wrap" FontSize="18" Grid.Row="0">This sample demonstrates redacting text and leaving area blank, redacting the text with fill color, redacting the text with redaction codes,
and redacting images in the PDF using OCR.
<LineBreak/>
<LineBreak/>
Click the button to view an PDF document generated by Essential PDF. Please note that Adobe Reader or its equivalent is required to view the resultant document.
</TextBlock>
<StackPanel HorizontalAlignment="Center" Grid.Row="1" Orientation="Horizontal">
<Button Margin="10" Content="Redact PDF without Color" HorizontalAlignment="Left" VerticalAlignment="Top" x:Name="btnRedactPDF" Width="167" Click="btnRedactPDFwithoutColor" Height="48"/>
<Button Margin="10" Content="Redact PDF with Fill Color" HorizontalAlignment="Left" VerticalAlignment="Top" Width="160" x:Name="btnRedactPDFwithColor" Height="48" Click="btnRedactPDFwithColor_Click"/>
<Button Margin="10" Content="Redact PDF with Redaction Codes" HorizontalAlignment="Left" VerticalAlignment="Top" Width="188" x:Name="btnRedactPDFwithCodes" Height="48" Click="btnRedactPDFwithCodes_Click"/>
<Button Margin="10" Content="Redact from PDF Image" HorizontalAlignment="Left" VerticalAlignment="Top" Width="181" x:Name="btnRedactPDFImage" Height="48" Click="btnRedactPDFImage_Click"/>
</StackPanel>
<Grid Grid.Row="1" Margin="10">
<Grid.RowDefinitions>
<RowDefinition Height="*" ></RowDefinition>
<RowDefinition Height="*" ></RowDefinition>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Button Margin="10" Content="Redact PDF without Color" Grid.Row="0" Grid.Column="0" HorizontalAlignment="Left" VerticalAlignment="Top" x:Name="btnRedactPDF" Width="167" Click="btnRedactPDFwithoutColor" Height="48"/>
<Button Margin="10,10,0,0" Content="Redact PDF with Fill Color" Grid.Row="0" Grid.Column="1" HorizontalAlignment="Left" VerticalAlignment="Top" Width="167" x:Name="btnRedactPDFwithColor" Height="48" Click="btnRedactPDFwithColor_Click"/>
<Button Margin="0,10,0,0" Content="Redact PDF with Redaction Codes" Grid.Row="0" Grid.Column="2" HorizontalAlignment="Center" VerticalAlignment="Top" Width="194" x:Name="btnRedactPDFwithCodes" Height="48" Click="btnRedactPDFwithCodes_Click"/>
<Button Margin="10,10,0,0" Content="Redact from PDF Image" Grid.Row="0" Grid.Column="3" HorizontalAlignment="Left" VerticalAlignment="Top" Width="181" x:Name="btnRedactPDFImage" Height="48" Click="btnRedactPDFImage_Click"/>
<Button Margin="10,10,0,0" Content="Add Visual Appearance" Grid.Row="1" HorizontalAlignment="Left" VerticalAlignment="Top" x:Name="btnAddVisualElements" Height="48" Click="btnAddVisualElements_Click" Width="167"/>
<Button Margin="10,10,0,0" Content="Redact Text Only" Grid.Row="1" Grid.Column="1" HorizontalAlignment="Left" VerticalAlignment="Top" x:Name="btnTextOnly" Height="48" Click="btnTextOnly_Click" Width="167"/>
<Button Margin="0,10,0,0" Content="Find And Redact" Grid.Row="1" Grid.Column="2" HorizontalAlignment="Center" VerticalAlignment="Top" x:Name="btnFindAndRedact" Height="48" Click="btnFindAndRedact_Click" Width="194"/>
<Button Margin="10,10,0,0" Content="Track Redaction Progress" Grid.Row="1" Grid.Column="3" HorizontalAlignment="Left" VerticalAlignment="Top" x:Name="btnRedactionProgress" Height="48" Click="btnRedactionProgress_Click" Width="181"/>
</Grid>
</Grid>
</Window>
146 changes: 141 additions & 5 deletions PDFRedaction/PDFRedaction/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@
using Syncfusion.Pdf.Graphics;
using Syncfusion.Pdf.Parsing;
using Syncfusion.Pdf.Redaction;
using System;
using System.Diagnostics;
using System.Drawing;
using System.Text.RegularExpressions;
using System.Windows;
using System.Xml.Linq;

namespace PDFRedaction
{
Expand All @@ -19,13 +21,14 @@ public partial class MainWindow : Window
public MainWindow()
{
InitializeComponent();
Syncfusion.Licensing.SyncfusionLicenseProvider.RegisterLicense("NRAiBiAaIQQuGjN/VkZ+XU9FfV5AQmBIYVp/TGpJfl96cVxMZVVBJAtUQF1hTH5Vd0ViUH5XdXxdT2dYWkd2");
}

private void btnRedactPDFwithoutColor(object sender, RoutedEventArgs e)
{
//Load a PDF document for redaction
PdfLoadedDocument ldoc = new PdfLoadedDocument("../../Input/RedactPDFwithEmail.pdf");

//Get first page from document
PdfLoadedPage lpage = ldoc.Pages[0] as PdfLoadedPage;

Expand Down Expand Up @@ -65,7 +68,7 @@ private void btnRedactPDFwithColor_Click(object sender, RoutedEventArgs e)
Process.Start("RedactedPDF.pdf");
}

private void btnRedactPDFwithCodes_Click(object sender, RoutedEventArgs e)
private void btnRedactPDFwithCodes_Click(object sender, RoutedEventArgs e)
{
//Load a PDF document for redaction
PdfLoadedDocument ldoc = new PdfLoadedDocument("../../Input/W4-tax-form.pdf");
Expand Down Expand Up @@ -100,7 +103,7 @@ private void btnRedactPDFwithCodes_Click(object sender, RoutedEventArgs e)

private void btnRedactPDFImage_Click(object sender, RoutedEventArgs e)
{
using (OCRProcessor processor = new OCRProcessor(@"../../TesseractBinaries/3.02"))
using (OCRProcessor processor = new OCRProcessor())
{
//Load the PDF document
PdfLoadedDocument lDoc = new PdfLoadedDocument(@"../../Input/FormWithSSN.pdf");
Expand Down Expand Up @@ -128,7 +131,7 @@ private void btnRedactPDFImage_Click(object sender, RoutedEventArgs e)
scaleX = imgInfo.Bounds.Height / ocrImage.Height;
scaleY = imgInfo.Bounds.Width / ocrImage.Width;
}

//Get the text from page and lines.
foreach (var page in result.Pages)
{
Expand All @@ -142,7 +145,7 @@ private void btnRedactPDFImage_Click(object sender, RoutedEventArgs e)
{
RectangleF redactionBound = new RectangleF(line.Rectangle.X * scaleX, line.Rectangle.Y * scaleY,
(line.Rectangle.Width - line.Rectangle.X) * scaleX, (line.Rectangle.Height - line.Rectangle.Y) * scaleY);

//Create PDF redaction for the found SSN location
PdfRedaction redaction = new PdfRedaction(redactionBound);

Expand All @@ -163,5 +166,138 @@ private void btnRedactPDFImage_Click(object sender, RoutedEventArgs e)
Process.Start("RedactedPDF.pdf");
}
}

private void btnAddVisualElements_Click(object sender, RoutedEventArgs e)
{
using (PdfLoadedDocument loadedDocument = new PdfLoadedDocument("../../Input/RedactPDFwithEmail.pdf"))
{
//Get the first page from the document
PdfLoadedPage page = loadedDocument.Pages[0] as PdfLoadedPage;

//Create a PDF redaction for the page
PdfRedaction redaction = new PdfRedaction(new RectangleF(341, 149, 64, 14));

//Draw the patten on the redaction area
PdfHatchBrush pdfHatchBrush = new PdfHatchBrush(PdfHatchStyle.BackwardDiagonal, Color.Red, Color.Transparent);

redaction.Appearance.Graphics.DrawRectangle(pdfHatchBrush, new RectangleF(0, 0, 64, 14));

//Add the redaction to the loaded page
page.Redactions.Add(redaction);

//Save the redacted PDF document to disk
loadedDocument.Save("RedactedPDFWithVisualAppearance.pdf");
//Close the document instance
loadedDocument.Close(true);

Process.Start("RedactedPDFWithVisualAppearance.pdf");
}

}

private void btnTextOnly_Click(object sender, RoutedEventArgs e)
{
using (PdfLoadedDocument loadedDocument = new PdfLoadedDocument("../../Input/RedactPDFwithEmail.pdf"))
{
//Get the first page from the document
PdfLoadedPage page = loadedDocument.Pages[0] as PdfLoadedPage;

//Create a PDF redaction for the page
PdfRedaction redaction = new PdfRedaction(new RectangleF(343, 280, 100, 16));

//Set text only redaction
redaction.TextOnly = true;

//Add the redaction to the loaded page
page.Redactions.Add(redaction);

//Save the redacted PDF document to disk
loadedDocument.Save("RedactedPDFTextOnly.pdf");
//Close the document instance
loadedDocument.Close(true);

Process.Start("RedactedPDFTextOnly.pdf");
}
}

private void btnFindAndRedact_Click(object sender, RoutedEventArgs e)
{
using (PdfLoadedDocument loadedDocument = new PdfLoadedDocument("../../Input/RedactPDFwithEmail.pdf"))
{
//Get the first page from the document
PdfLoadedPage page = loadedDocument.Pages[0] as PdfLoadedPage;

//Extract text from the page
page.ExtractText(out TextLines textLines);

if (textLines != null && textLines.Count > 0)
{
//Define regular expression pattern to search for dates in the format MM/DD/YYYY
string datePattern = @"\b\d{1,2}\/\d{1,2}\/\d{4}\b";

//Find the text to redact
foreach (TextLine line in textLines)
{
foreach (TextWord word in line.WordCollection)
{
//Match the text against the date pattern
MatchCollection dateMatches = Regex.Matches(word.Text, datePattern);
//Add redaction if the match found
foreach (Match dateMatch in dateMatches)
{
string textToFindAndRedact = dateMatch.Value;
if (textToFindAndRedact == word.Text)
{
//Create a redaction object.
PdfRedaction redaction = new PdfRedaction(word.Bounds, Color.Black);
//Add a redaction object into the redaction collection of loaded page.
page.AddRedaction(redaction);
}
}
}

}
}
//Save the redacted PDF document to disk
loadedDocument.Save("RedactedPDFRegex.pdf");
//Close the document instance
loadedDocument.Close(true);

Process.Start("RedactedPDFRegex.pdf");
}
}

private void btnRedactionProgress_Click(object sender, RoutedEventArgs e)
{
//Load the PDF document for redaction
using (PdfLoadedDocument loadedDocument = new PdfLoadedDocument("../../Input/input.pdf"))
{
ConsolePopup consolePopup = new ConsolePopup();
consolePopup.Show();

//Set the redaction progress handler to track the progress of redaction
loadedDocument.RedactionProgress += (redactSender, args) =>
{
//Display the progress in the console
consolePopup.WriteLine($"Redaction Progress: {args.Progress}%");
};

//Iterate through each page in the document
foreach (PdfLoadedPage page in loadedDocument.Pages)
{
//Create a redaction area for the page
PdfRedaction redaction = new PdfRedaction(new RectangleF(120, 200, 140, 20));
redaction.Appearance.Graphics.DrawRectangle(PdfBrushes.Black, new RectangleF(0, 0, 140, 20));
//Add the redaction to the loaded page
page.Redactions.Add(redaction);
}
//Save the redacted PDF document to disk
loadedDocument.Save("RedactedPDFWithProgress.pdf");
//Close the document instance
loadedDocument.Close(true);
//Open the redacted PDF document
Process.Start("RedactedPDFWithProgress.pdf");
}
}
}
}
Loading