How to replace text with an image in a PDF document using .NET Core?
Syncfusion Essential® PDF is a comprehensive, high‑performance .NET PDF library designed for creating, reading, and editing PDF documents. Learn how to search for specific text in a PDF using the .NET PDF Library, apply redaction over the matched areas, and draw an image in each redacted region so that it accurately replaces the original text. This method permanently removes the underlying content.
Steps to find and redact text in a PDF, then overlay an image at the same location
- Create a new project: Start a new console application in .NET core.
- Install required packages: Add the Syncfusion.Pdf.Imaging.Net.Core NuGet package as a reference in the console application from Nuget.org.
- Set up the environment: In the
Program.csfile, include the following namespaces.
using Syncfusion.Drawing;
using Syncfusion.Pdf;
using Syncfusion.Pdf.Graphics;
using Syncfusion.Pdf.Parsing;
using Syncfusion.Pdf.Redaction;
- Implement main logic: Use the provided code sample in
Program.csto find and redact text in a PDF, then overlay an image at the same location.
// Open the image once; reuse for all redactions.
using FileStream imageStream = new FileStream(Path.GetFullPath(@"Data/Image.jpg"), FileMode.Open, FileAccess.Read);
PdfBitmap image = new PdfBitmap(imageStream);
// Load the source PDF.
using (PdfLoadedDocument document = new PdfLoadedDocument(Path.GetFullPath(@"Data/Input.pdf")))
{
// Find all bounds of the search text across pages.
Dictionary<int, List<RectangleF>> hits;
document.FindText("Fusce", out hits);
// For each page with matches
foreach (KeyValuePair<int, List<RectangleF>> kvp in hits)
{
int pageIndex = kvp.Key;
PdfLoadedPage page = document.Pages[pageIndex] as PdfLoadedPage;
// Replace each matched region by drawing the image in a redaction appearance.
foreach (RectangleF match in kvp.Value)
{
float pad = 1f; // Small padding around the text box.
RectangleF box = new RectangleF(
match.X - pad,
match.Y - pad,
match.Width + pad * 2,
match.Height + pad * 2
);
// Create a transparent redaction over the text area.
PdfRedaction redaction = new PdfRedaction(box, Color.Transparent);
// Draw the image to fully cover the redaction area (stretched to fit).
RectangleF dest = new RectangleF(0, 0, box.Width, box.Height);
redaction.Appearance.Graphics.DrawImage(image, dest);
// Add the redaction to the page.
page.AddRedaction(redaction);
}
}
// Apply all redactions so the text is removed and image overlays are finalized.
document.Redact();
// Save the result.
document.Save(Path.GetFullPath(@"Output/Output.pdf"));
}
A complete working sample is available for download from GitHub.
By executing the program, you will generate the following PDF document.
Take a moment to peruse the documentation to learn how to locate and redact text in a PDF document.
Conclusion
I hope you enjoyed learning how to find and redact text in a PDF and overlay an image at the same location.
You can refer to our ASP.NET Core PDF feature tour page to know about its other groundbreaking feature representations and documentation, and how to quickly get started for configuration specifications. You can also explore our ASP.NET Core PDF example to understand how to create and manipulate data.
For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion®, you can try our 30-day free trial to check out our other controls.
If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forums, Direct-Trac, or feedback portal. We are always happy to assist you!