Easy ways to redact PDF using C# | Syncfusion blogs
Live Chat Icon For mobile
Live Chat Icon
Popular Categories.NET  (174).NET Core  (29).NET MAUI  (207)Angular  (109)ASP.NET  (51)ASP.NET Core  (82)ASP.NET MVC  (89)Azure  (41)Black Friday Deal  (1)Blazor  (219)BoldSign  (14)DocIO  (24)Essential JS 2  (107)Essential Studio  (200)File Formats  (66)Flutter  (133)JavaScript  (221)Microsoft  (119)PDF  (81)Python  (1)React  (101)Streamlit  (1)Succinctly series  (131)Syncfusion  (918)TypeScript  (33)Uno Platform  (3)UWP  (4)Vue  (45)Webinar  (51)Windows Forms  (61)WinUI  (68)WPF  (159)Xamarin  (161)XlsIO  (36)Other CategoriesBarcode  (5)BI  (29)Bold BI  (8)Bold Reports  (2)Build conference  (8)Business intelligence  (55)Button  (4)C#  (149)Chart  (131)Cloud  (15)Company  (443)Dashboard  (8)Data Science  (3)Data Validation  (8)DataGrid  (63)Development  (632)Doc  (8)DockingManager  (1)eBook  (99)Enterprise  (22)Entity Framework  (5)Essential Tools  (14)Excel  (40)Extensions  (22)File Manager  (7)Gantt  (18)Gauge  (12)Git  (5)Grid  (31)HTML  (13)Installer  (2)Knockout  (2)Language  (1)LINQPad  (1)Linux  (2)M-Commerce  (1)Metro Studio  (11)Mobile  (507)Mobile MVC  (9)OLAP server  (1)Open source  (1)Orubase  (12)Partners  (21)PDF viewer  (43)Performance  (12)PHP  (2)PivotGrid  (4)Predictive Analytics  (6)Report Server  (3)Reporting  (10)Reporting / Back Office  (11)Rich Text Editor  (12)Road Map  (12)Scheduler  (52)Security  (3)SfDataGrid  (9)Silverlight  (21)Sneak Peek  (31)Solution Services  (4)Spreadsheet  (11)SQL  (11)Stock Chart  (1)Surface  (4)Tablets  (5)Theme  (12)Tips and Tricks  (112)UI  (387)Uncategorized  (68)Unix  (2)User interface  (68)Visual State Manager  (2)Visual Studio  (31)Visual Studio Code  (19)Web  (596)What's new  (333)Windows 8  (19)Windows App  (2)Windows Phone  (15)Windows Phone 7  (9)WinRT  (26)
Redact PDF using C#

Easy Ways to Redact PDFs Using C#

Redacting a PDF is the process of removing sensitive or confidential information from PDF documents. Syncfusion’s .NET PDF library provides an easy way to redact PDF using C#.

Redaction isn’t just placing a colored box over text or an image. When we try copying text from under the colored area, we can still see the content, so it’s not redacted. Syncfusion provides a 100% true redaction, which means we completely remove the content from the document. Once the content is redacted, it cannot be undone. It is always a good idea to have a backup of the master document.

Syncfusion PDF Library helps customers reach GDPR compliance by safely removing customer information from a PDF document. You can now distribute files securely by permanently removing confidential information such as financial account numbers, social security numbers, customer email addresses, phone numbers, and credit card information.

The PDF redaction feature is also available in WinForms, WPF, ASP.NET Web Forms, and ASP.NET MVC. Syncfusion PDF Library provides customization options for the redacted area, so you can use colored boxes or leave the area blank. You can specify custom text or redaction codes to appear over the redacted area.

Transform your PDF files effortlessly in C# with just five lines of code using Syncfusion's comprehensive PDF Library!

Let’s start with code to redact a PDF using C#

Already referencing the required assemblies from NuGet? Great! Now we need to add a namespace in our class, as in the following code sample.

using Syncfusion.Pdf;
using Syncfusion.Pdf.Graphics;
using Syncfusion.Pdf.Parsing;
using Syncfusion.Pdf.Redaction;

Here, we’ll just remove the email address from the PDF and leave the area blank.

Before redact text in PDF

PDF file before redaction

//Load a PDF document for redaction
PdfLoadedDocument ldoc = new PdfLoadedDocument("../../Input/RedactPDF.pdf");
//Get first page from document
PdfLoadedPage lpage = ldoc.Pages[0] as PdfLoadedPage;

//Create PDF redaction for the page
PdfRedaction redaction = new PdfRedaction(new RectangleF(340,120,140,20));

//Adds the redaction to loaded 1st page
lpage.Redactions.Add(redaction);

//Save the redacted PDF document to disk
ldoc.Save("RedactedPDF.pdf");

//Close the document instance
ldoc.Close(true);

As you can see in the screenshot, the email address in the PDF file is completely removed without any trace and you cannot find or select the redacted content.

Redacted PDF without overlay color

Redacted PDF without text and color

Boost your productivity with Syncfusion's PDF Library! Gain access to invaluable insights and tutorials in our extensive documentation.

Redact PDF with fill color

Now, we’ll load the same PDF file and redact with red color. This will completely remove the content from the PDF and apply red color over the redacted area.

//Create PDF redaction for the page
PdfRedaction redaction = new PdfRedaction(new RectangleF(340,120,140,20), System.Drawing.Color.Red);

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

Redacted PDF with red color

Redacted the text in PDF with red color

Redact PDF with code sets and entries

Certain PDF files, such as invoice, government official forms, contains text or images that are positioned at the fixed position in the PDF page. For example, employee addresses in W-4 tax forms will always be in the same place and can be redacted under the exemption code of US FOIA (b) (6).

//Create redaction area for redacting telephone number with code set.
RectangleF redactionBound = new RectangleF(50, 568, 120, 13);

PdfRedaction redaction = new PdfRedaction(redactionBound);
redaction.Appearance.Graphics.DrawRectangle(PdfBrushes.Black, new RectangleF(0, 0, redactionBound.Width, redactionBound.Height));
redaction.Appearance.Graphics.DrawString("(b) (6)", new PdfStandardFont(PdfFontFamily.Helvetica, 11), PdfBrushes.White, new PointF(0, 0));

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

//Create redaction area for redacting address with code set.
RectangleF addressRedaction = new RectangleF(50, 592, 75, 13);
redaction = new PdfRedaction(addressRedaction);
redaction.Appearance.Graphics.DrawRectangle(PdfBrushes.Black, new RectangleF(0, 0, addressRedaction.Width, addressRedaction.Height));
redaction.Appearance.Graphics.DrawString("(b) (6)", new PdfStandardFont(PdfFontFamily.Helvetica, 11), PdfBrushes.White, new PointF(0, 0));
lpage.Redactions.Add(redaction);

Redact PDF with redaction code

Redacted the PDF content with code sets

See a world of document processing possibilities in Syncfusion's PDF Library as we unveil its features in interactive demonstrations.

Redact image in PDF – OCR

PDF Library provides another great feature— OCR a scanned document image in a PDF and redact PDF content using C#. Sometimes, we may have social security numbers (SSN), employee identification numbers, addresses, email IDs, in a scanned PDF file. In those cases, it is very hard to search manually for a specific pattern to redact it. Syncfusion offers an efficient way to find sensitive information in a PDF image using OCR and redact it from the PDF file.

Before redacting PDF
After PDF redaction
 Redact a Social Security Number from the PDF

To do this, install the Syncfusion.PDF.OCR.WPF from NuGet. Copy the Tesseract binaries and language data from the NuGet package location to your application and refer the path to your OCR processor. Add the following namespace and code snippet to your class.

using Syncfusion.OCRProcessor;
using Syncfusion.Pdf.Exporting;

//Initialize the OCR processor
using (OCRProcessor processor = new OCRProcessor(@"../../TesseractBinaries/3.02/"))
{
    //Load the PDF document 
    PdfLoadedDocument lDoc = new PdfLoadedDocument(@"../../Input/FormWithSSN.pdf");

    //Load the PDF page
    PdfLoadedPage loadedPage = lDoc.Pages[0] as PdfLoadedPage;
    //Language to process the OCR
    processor.Settings.Language = Languages.English;

    //Extract image and information from the PDF for processing OCR
    PdfImageInfo[] imageInfoCollection = loadedPage.ImagesInfo;

    foreach (PdfImageInfo imgInfo in imageInfoCollection)
    {
        Bitmap ocrImage = imgInfo.Image as Bitmap;
        OCRLayoutResult result = null;
        float scaleX = 0, scaleY = 0;
        if (ocrImage != null)
        {
            //Process OCR by providing loaded PDF document, Data dictionary and language
            string text = processor.PerformOCR(ocrImage, @"../../LanguagePack/", out result);

            //Calculate the scale factor for the image used in the PDF
            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)
        {
            foreach (var line in page.Lines)
            {
                if (line.Text != null)
                {
                    //Regular expression for social security number
                    var ssnMatches = Regex.Matches(line.Text, @"(\d{3})+[ -]*(\d{2})+[ -]*\d{4}", RegexOptions.IgnorePatternWhitespace);
                    if (ssnMatches.Count >= 1)
                    {
                        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);

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


                    }
                }
            }
        }
    }

    //Save the redacted PDF document in the disk
    lDoc.Save("RedactedPDF.pdf");
    lDoc.Close(true);

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

GitHub reference

You can download the sample demonstrating the available redaction options using the Syncfusion PDF library on the GitHub repository.

Syncfusion’s high-performance PDF Library allows you to create PDF documents from scratch without Adobe dependencies.

Wrap up

As you can see, Syncfusion .NET PDF Library provides easy and advanced options to redact PDFs using C#. With Syncfusion PDF Library, you can automate the process to ensure customers’ sensitive information is redacted efficiently without manual work, before sharing with third parties.

To evaluate our PDF redaction using C#, try our online demo. Take a moment to peruse the documentation, where you’ll find other options and features, all with accompanying code examples.

If you have any questions or require clarification about these features, please let us know in the comments below. You can also contact us through our support forum or Direct-Trac. We are happy to assist you!

If you like this post, you may also like:

Tags:

Share this post:

Comments (4)

[…] Easy Ways to Redact PDFs Using C# (George Livingston) […]

Your site is very cool, i have bookmarked it.

Syncfusion.Pdf.Redaction this reference i am not able to find. I am working on DotNetCore I have added Syncfusion.Pdf.Net.Core reference for my code. Can you please let me know how to add a reference.

Chinnu Muniyappan
Chinnu Muniyappan

Hi SOMNAT,

Thank you for contacting Syncfusion support.

To redact the content from the existing PDF document in .NET Core, you need to include the Syncfusion.Pdf.Imaging.Portable assembly reference in the .NET Core project. Please refer to the below link for more information,

KB: https://www.syncfusion.com/kb/11981/how-to-redact-content-from-pdf-document-in-asp-net-core
UG: https://help.syncfusion.com/file-formats/pdf/working-with-redaction
NuGet package link for Pdf.Imaging.Portable: https://www.nuget.org/packages/Syncfusion.Pdf.Imaging.Net.Core/

Regards,
Chinnu M

Comments are closed.

Popular Now

Be the first to get updates

Subscribe RSS feed

Be the first to get updates

Subscribe RSS feed