Skip to content

Commit 63e5c92

Browse files
committed
1005534: Added code example for redact text and replace image in a PDF
1 parent c28924a commit 63e5c92

File tree

6 files changed

+63
-0
lines changed

6 files changed

+63
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<Solution>
2+
<Project Path="Redact-Text-and-Replace-It-with-an-Image-in-a-PDF/Redact-Text-and-Replace-It-with-an-Image-in-a-PDF.csproj" />
3+
</Solution>
Loading

Text Extraction/Redact-Text-and-Replace-It-with-an-Image-in-a-PDF/.NET/Redact-Text-and-Replace-It-with-an-Image-in-a-PDF/Output/gitkeep.txt

Whitespace-only changes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
using Syncfusion.Drawing;
2+
using Syncfusion.Pdf;
3+
using Syncfusion.Pdf.Graphics;
4+
using Syncfusion.Pdf.Parsing;
5+
using Syncfusion.Pdf.Redaction;
6+
7+
// Open the image once; reuse for all redactions.
8+
using FileStream imageStream = new FileStream(Path.GetFullPath(@"Data/Image.jpg"), FileMode.Open, FileAccess.Read);
9+
PdfBitmap image = new PdfBitmap(imageStream);
10+
11+
// Load the source PDF.
12+
using (PdfLoadedDocument document = new PdfLoadedDocument(Path.GetFullPath(@"Data/Input.pdf")))
13+
{
14+
// Find all bounds of the search text across pages.
15+
Dictionary<int, List<RectangleF>> hits;
16+
document.FindText("Fusce", out hits);
17+
// For each page with matches
18+
foreach (KeyValuePair<int, List<RectangleF>> kvp in hits)
19+
{
20+
int pageIndex = kvp.Key;
21+
PdfLoadedPage page = document.Pages[pageIndex] as PdfLoadedPage;
22+
// Replace each matched region by drawing the image in a redaction appearance.
23+
foreach (RectangleF match in kvp.Value)
24+
{
25+
float pad = 1f; // Small padding around the text box.
26+
RectangleF box = new RectangleF(
27+
match.X - pad,
28+
match.Y - pad,
29+
match.Width + pad * 2,
30+
match.Height + pad * 2
31+
);
32+
// Create a transparent redaction over the text area.
33+
PdfRedaction redaction = new PdfRedaction(box, Color.Transparent);
34+
// Draw the image to fully cover the redaction area (stretched to fit).
35+
RectangleF dest = new RectangleF(0, 0, box.Width, box.Height);
36+
redaction.Appearance.Graphics.DrawImage(image, dest);
37+
// Add the redaction to the page.
38+
page.AddRedaction(redaction);
39+
}
40+
}
41+
// Apply all redactions so the text is removed and image overlays are finalized.
42+
document.Redact();
43+
// Save the result.
44+
document.Save(Path.GetFullPath(@"Output/Output.pdf"));
45+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net8.0</TargetFramework>
6+
<RootNamespace>Redact_Text_and_Replace_It_with_an_Image_in_a_PDF</RootNamespace>
7+
<ImplicitUsings>enable</ImplicitUsings>
8+
<Nullable>enable</Nullable>
9+
</PropertyGroup>
10+
11+
<ItemGroup>
12+
<PackageReference Include="Syncfusion.Pdf.Imaging.Net.Core" Version="*" />
13+
</ItemGroup>
14+
15+
</Project>

0 commit comments

Comments
 (0)