How To Redact PDF Documents In .NET Using The PDF Library
Introduction
A quick start .NET console project that shows how to redact PDF documents using the Syncfusion® PDF Library.
System requirement
Framework and SDKs
- .NET SDK (version 5.0 or later)
IDEs
- Visual Studio 2019/ Visual Studio 2022
Code snippet for Redact PDF content from PDF documents
We will create a new .NET console application, add the Syncfusion® PDF library package, and write the code
//Create stream from an existing PDF document.
FileStream docStream = new FileStream("Input.pdf", FileMode.Open, FileAccess.Read);
//Load the existing PDF document.
PdfLoadedDocument document = new PdfLoadedDocument(docStream);
//Get the first page from the document.
PdfLoadedPage page = document.Pages[0] as PdfLoadedPage;
//Create a redaction object.
PdfRedaction redaction = new PdfRedaction(new RectangleF(343, 147, 60, 17));
//Add a redaction object into the redaction collection of loaded page.
page.AddRedaction(redaction);
//Redact the contents from the PDF document.
document.Redact();
//Create file stream.
using (FileStream outputFileStream = new FileStream("Output.pdf", FileMode.Create, FileAccess.ReadWrite)) {
//Save the PDF document to file stream.
document.Save(outputFileStream);
}
//Close the document.
document.Close(true);
Output Image

Code snippet for Apply fill color to the redacted area
We will create a new .NET console application, add the Syncfusion® PDF library package, and write the code
//Get stream from an existing PDF document.
FileStream docStream = new FileStream("Input.pdf", FileMode.Open, FileAccess.Read);
//Load the PDF document.
PdfLoadedDocument document = new PdfLoadedDocument(docStream);
//Get the first page from the document.
PdfLoadedPage page = document.Pages[0] as PdfLoadedPage;
//Create a PDF redaction for the page.
PdfRedaction redaction = new PdfRedaction(new RectangleF(343, 147, 60, 17));
//Set fill color for the redaction bounds.
redaction.FillColor = Color.Red;
//Add a redaction object into the redaction collection of loaded page.
page.AddRedaction(redaction);
//Redact the contents from the PDF document.
document.Redact();
//Create file stream.
using (FileStream outputFileStream = new FileStream("Output1.pdf", FileMode.Create, FileAccess.ReadWrite)) {
//Save the PDF document to file stream.
document.Save(outputFileStream);
}
//Close the document.
document.Close(true);
Output Image

Code snippet for Display text on the redacted area
We will create a new .NET console application, add the Syncfusion® PDF library package, and write the code
//Get stream from an existing PDF document.
FileStream docStream = new FileStream("Input.pdf", FileMode.Open, FileAccess.Read);
//Load the existing PDF document.
PdfLoadedDocument document = new PdfLoadedDocument(docStream);
//Get the first page from the document.
PdfLoadedPage page = document.Pages[0] as PdfLoadedPage;
//Create a redaction object.
PdfRedaction redaction = new PdfRedaction(new RectangleF(343, 147, 60, 17));
//Font for the overlay text.
PdfStandardFont font = new PdfStandardFont(PdfFontFamily.Courier, 10);
//Draw text on the redacted area.
redaction.Appearance.Graphics.DrawString("Redacted", font, PdfBrushes.Red, new PointF(5, 5));
//Add a redaction object into the redaction collection of loaded page.
page.AddRedaction(redaction);
//Redact the contents from the PDF document.
document.Redact();
//Create file stream.
using (FileStream outputFileStream = new FileStream("Output2.pdf", FileMode.Create, FileAccess.ReadWrite)) {
//Save the PDF document to file stream.
document.Save(outputFileStream);
}
//Close the document.
document.Close(true);
Output Image

Code snippet for Display images on the redacted area
We will create a new .NET console application, add the Syncfusion® PDF library package, and write the code
//Get stream from an existing PDF document.
FileStream docStream = new FileStream("Input.pdf", FileMode.Open, FileAccess.Read);
//Load the existing PDF document.
PdfLoadedDocument document = new PdfLoadedDocument(docStream);
//Get the first page from the document.
PdfLoadedPage page = document.Pages[0] as PdfLoadedPage;
//Create a PDF redaction for the page.
PdfRedaction redaction = new PdfRedaction(new RectangleF(63, 57, 182, 157));
//Get stream from the image file.
FileStream imageStream = new FileStream("Image.jpg", FileMode.Open, FileAccess.Read);
//Load the image file.
PdfImage image = new PdfBitmap(imageStream);
//Draw image on the redaction appearance.
redaction.Appearance.Graphics.DrawImage(image, new RectangleF(0, 0, 182, 157));
//Add a redaction object into the redaction collection of loaded page.
page.AddRedaction(redaction);
//Redact the contents from the PDF document.
document.Redact();
//Create file stream.
using (FileStream outputFileStream = new FileStream("Output3.pdf", FileMode.Create, FileAccess.ReadWrite)) {
//Save the PDF document to file stream.
document.Save(outputFileStream);
}
//Close the document.
document.Close(true);
Output Image

How to run the examples
- Download this project to a location in your disk.
- Open the solution file using Visual Studio.
- Rebuild the solution to install the required NuGet package.
- Run the application.
Resources
- Product page: Syncfusion® PDF Framework
- Documentation page: Syncfusion® .NET PDF library
- Online demo: Syncfusion® .NET PDF library - Online demos
- Blog: Syncfusion® .NET PDF library - Blog
- Knowledge Base: Syncfusion® .NET PDF library - Knowledge Base
- EBooks: Syncfusion® .NET PDF library - EBooks
- FAQ: Syncfusion® .NET PDF library - FAQ
Support and feedback
- For any other queries, reach our Syncfusion® support team or post the queries through the community forums.
- Request new feature through Syncfusion® feedback portal.
License
This is a commercial product and requires a paid license for possession or use. Syncfusion’s licensed software, including this component, is subject to the terms and conditions of Syncfusion's EULA. You can purchase a licnense here or start a free 30-day trial here.
About Syncfusion®
Founded in 2001 and headquartered in Research Triangle Park, N.C., Syncfusion® has more than 26,000+ customers and more than 1 million users, including large financial institutions, Fortune 500 companies, and global IT consultancies.
Today, we provide 1600+ components and frameworks for web (Blazor, ASP.NET Core, ASP.NET MVC, ASP.NET WebForms, JavaScript, Angular, React, Vue, and Flutter), mobile (Xamarin, Flutter, UWP, and JavaScript), and desktop development (WinForms, WPF, WinUI(Preview), Flutter and UWP). We provide ready-to-deploy enterprise software for dashboards, reports, data integration, and big data processing. Many customers have saved millions in licensing fees by deploying our software.