Articles in this section
Category / Section

How to redact content from PDF document in ASP.NET Core

2 mins read

The Syncfusion Essential PDF is a .NET Core PDF library used to create, read, and edit PDF documents. Using this library, you can redact content from the PDF documents in the ASP.NET Core platform.

Steps to redact content from the PDF document in ASP.NET Core using C#

1. Create a new ASP.NET Core MVC application.

Create ASP.NET core MVC application

2. Install the Syncfusion.Pdf.Imaging.Net.Core NuGet packages as a reference to your .NET Core project from NuGet.org.

Install the nuget packages

3. Add a new button (Redact PDF) in the Index.cshtml as follows.

Add a button in cs html page

@{ Html.BeginForm("RedactPDF", "Home", FormMethod.Post);
    {
        <input type="submit" value="Redact PDF Document" class=" btn" />
    }
}

 

4. Include the following namespaces in the HomeController.cs file.

using Syncfusion.Pdf;
using Syncfusion.Pdf.Parsing;
using Syncfusion.Pdf.Redaction;
using System.IO;

 

5. Add a new action method RedactPDF in the HomeController.cs and include the following code sample to redact the content from a PDF file and download it.

//To get the content root path of a project.
private readonly IHostingEnvironment _hostingEnvironment;
public HomeController(IHostingEnvironment hostingEnvironment)
{
   _hostingEnvironment = hostingEnvironment;
}
public IActionResult RedactPDF()
{
string path = Path.Combine(_hostingEnvironment.ContentRootPath, "Data", "Input.pdf");
FileStream inputDocument = new FileStream(path, FileMode.Open);
//Load an existing PDF document.
PdfLoadedDocument loadedDocument = new PdfLoadedDocument(inputDocument);
//Get the first page from the document.
PdfLoadedPage page = loadedDocument.Pages[0] as PdfLoadedPage;
//Create a redaction object.
PdfRedaction redaction = new PdfRedaction(new RectangleF(343, 147, 60, 17), Color.Black);
//Add a redaction object into the redaction collection of the loaded page.
page.AddRedaction(redaction);
//Redact the contents from the PDF document.
loadedDocument.Redact();
//Save the PDF document.
MemoryStream stream = new MemoryStream();
//Save the PDF document.
loadedDocument.Save(stream);
stream.Position = 0;
//Close the document.
loadedDocument.Close(true);
//Download the PDF document in the browser.
FileStreamResult fileStreamResult = new FileStreamResult(stream, "application/pdf");
fileStreamResult.FileDownloadName = "RedactPDF.pdf";
return fileStreamResult;}

 

6. Build and run the application, the website will open in the browser, then, you can redact the content from a PDF document.

Browser page

7. By executing the program, you will get the PDF document as follows.

Output screenshot

 

A complete work sample for redact content from a PDF document can be downloaded from RedactionSample.zip.

Take a moment to peruse the documentation. You can find the other options like display text, image, and pattern on the redacted area, fill color on the redacted area. Also, redact the PDF without drawing the fill color, appearance, and features like encrypt and decrypt PDF document, and digitally sign a PDF file with code examples.

 

Refer to this link to explore a rich set of Syncfusion Essential PDF features.

 

Also see:

Compress a PDF file in WinForms

Create a PDF file in Xamarin

Create a PDF file in ASP.NET MVC

Create a PDF file in WPF

Create a PDF file in Windows Forms

 

Note:

Starting with v16.2.0.x, if you reference Syncfusion assemblies from the trial setup or the NuGet feed, include a license key in your projects. Refer to the link to learn about generating and registering the Syncfusion license key in your application to use the components without trail message.

 

 

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please sign in to leave a comment
Access denied
Access denied