Skip to content

Commit 82e5252

Browse files
Merge pull request #239 from SyncfusionExamples/1005967
1005967: How to replaces the image with text in PDF document
2 parents afcdc56 + bed8cae commit 82e5252

File tree

5 files changed

+71
-0
lines changed

5 files changed

+71
-0
lines changed
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<Solution>
2+
<Project Path="Image-to-Text-Replacement-in-PDF/Image-to-Text-Replacement-in-PDF.csproj" />
3+
</Solution>
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>Image_to_Text_Replacement_in_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>

Image Extraction/Image-to-Text-Replacement-in-PDF/.NET/Image-to-Text-Replacement-in-PDF/Output/gitkeep.txt

Whitespace-only changes.
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
using Syncfusion.Pdf;
2+
using Syncfusion.Pdf.Exporting;
3+
using Syncfusion.Pdf.Graphics;
4+
using Syncfusion.Pdf.Parsing;
5+
6+
// Load the input PDF document
7+
using (PdfLoadedDocument document = new PdfLoadedDocument(Path.GetFullPath(@"Data/Input.pdf")))
8+
{
9+
// Loop through every page in the PDF
10+
foreach (PdfLoadedPage page in document.Pages)
11+
{
12+
PdfLoadedPage loadedPage = page as PdfLoadedPage;
13+
// Get graphics object to draw on the page
14+
PdfGraphics graphics = loadedPage.Graphics;
15+
// Get all images present in the current page
16+
PdfImageInfo[] imageInfos = loadedPage.GetImagesInfo();
17+
// If no images found, skip this page
18+
if (imageInfos == null || imageInfos.Length == 0) continue;
19+
// Font for overlay text
20+
PdfFont font = new PdfStandardFont(PdfFontFamily.Helvetica, 12f);
21+
// Center the overlay text inside the image area
22+
PdfStringFormat centerFormat = new PdfStringFormat
23+
{
24+
Alignment = PdfTextAlignment.Center,
25+
LineAlignment = PdfVerticalAlignment.Middle
26+
};
27+
// Save the current graphics state
28+
graphics.Save();
29+
// Draw overlay text ("Image removed") on top of each image area
30+
foreach (PdfImageInfo info in imageInfos)
31+
{
32+
string overlayText = "Image removed";
33+
34+
graphics.DrawString(
35+
overlayText,
36+
font,
37+
PdfBrushes.Red,
38+
info.Bounds, // Draw the text inside the image area
39+
centerFormat
40+
);
41+
}
42+
// Restore the graphics state after drawing
43+
graphics.Restore();
44+
// Remove the images from the PDF page
45+
foreach (PdfImageInfo info in imageInfos)
46+
{
47+
loadedPage.RemoveImage(info);
48+
}
49+
}
50+
// Save the output PDF
51+
document.Save(Path.GetFullPath(@"Output/Output.pdf"));
52+
}
53+

0 commit comments

Comments
 (0)