Skip to content

Commit 0b67877

Browse files
Merge pull request #244 from SyncfusionExamples/923107
923107: Added proper code example for replacing existing image in PDF
2 parents 5e91eb7 + f677483 commit 0b67877

File tree

8 files changed

+177
-0
lines changed

8 files changed

+177
-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="Replace-existing-text-in-a-PDF/Replace-existing-text-in-a-PDF.csproj" Id="27ffc4d8-a2ca-4a30-aba0-80a4db3a1895" />
3+
</Solution>
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<configuration>
3+
<startup>
4+
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.8" />
5+
</startup>
6+
<runtime>
7+
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
8+
<dependentAssembly>
9+
<assemblyIdentity name="System.Runtime.CompilerServices.Unsafe" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
10+
<bindingRedirect oldVersion="0.0.0.0-6.0.0.0" newVersion="6.0.0.0" />
11+
</dependentAssembly>
12+
<dependentAssembly>
13+
<assemblyIdentity name="System.Memory" publicKeyToken="cc7b13ffcd2ddd51" culture="neutral" />
14+
<bindingRedirect oldVersion="0.0.0.0-4.0.1.2" newVersion="4.0.1.2" />
15+
</dependentAssembly>
16+
</assemblyBinding>
17+
</runtime>
18+
</configuration>

Redaction/Replace-existing-text-in-a-PDF/.NET Framework/Replace-existing-text-in-a-PDF/Output/gitkeep.txt

Whitespace-only changes.
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
using System.Drawing;
2+
using Syncfusion.Pdf;
3+
using Syncfusion.Pdf.Graphics;
4+
using Syncfusion.Pdf.Parsing;
5+
using Syncfusion.Pdf.Redaction;
6+
using System.Collections.Generic;
7+
using System.IO;
8+
9+
namespace Replace_existing_text_in_a_PDF
10+
{
11+
internal class Program
12+
{
13+
static void Main(string[] args)
14+
{
15+
// Load the PDF document
16+
using (PdfLoadedDocument ldoc = new PdfLoadedDocument(Path.GetFullPath(@"Data/Input.pdf")))
17+
{
18+
// Dictionary to store the found text bounds for each page
19+
Dictionary<int, List<RectangleF>> matchedTextbounds = new Dictionary<int, List<RectangleF>>();
20+
// Get the first page of the PDF
21+
PdfLoadedPage page = ldoc.Pages[0] as PdfLoadedPage;
22+
// Search for the text and get its bounding rectangles
23+
ldoc.FindText("Fusce", out matchedTextbounds);
24+
// Loop through each page with matches
25+
for (int i = 0; i < matchedTextbounds.Count; i++)
26+
{
27+
// Get all rectangles where the text was found on this page
28+
List<RectangleF> rectangles = matchedTextbounds[i];
29+
// Loop through each found rectangle
30+
for (int j = 0; j < rectangles.Count; j++)
31+
{
32+
// Create a redaction area with transparent fill over the found text
33+
PdfRedaction redaction = new PdfRedaction(rectangles[j], Color.Transparent);
34+
// Draw the replacement text on the redaction area
35+
redaction.Appearance.Graphics.DrawString(
36+
"Hello",
37+
new PdfStandardFont(PdfFontFamily.Helvetica, 9),
38+
PdfBrushes.Black,
39+
PointF.Empty
40+
);
41+
// Add the redaction to the page
42+
page.AddRedaction(redaction);
43+
}
44+
}
45+
// Save the PDF document
46+
ldoc.Save(Path.GetFullPath(@"Output/Output.pdf"));
47+
}
48+
}
49+
}
50+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
using System.Reflection;
2+
using System.Runtime.CompilerServices;
3+
using System.Runtime.InteropServices;
4+
5+
// General Information about an assembly is controlled through the following
6+
// set of attributes. Change these attribute values to modify the information
7+
// associated with an assembly.
8+
[assembly: AssemblyTitle("Replace-existing-text-in-a-PDF")]
9+
[assembly: AssemblyDescription("")]
10+
[assembly: AssemblyConfiguration("")]
11+
[assembly: AssemblyCompany("")]
12+
[assembly: AssemblyProduct("Replace-existing-text-in-a-PDF")]
13+
[assembly: AssemblyCopyright("Copyright © 2026")]
14+
[assembly: AssemblyTrademark("")]
15+
[assembly: AssemblyCulture("")]
16+
17+
// Setting ComVisible to false makes the types in this assembly not visible
18+
// to COM components. If you need to access a type in this assembly from
19+
// COM, set the ComVisible attribute to true on that type.
20+
[assembly: ComVisible(false)]
21+
22+
// The following GUID is for the ID of the typelib if this project is exposed to COM
23+
[assembly: Guid("27ffc4d8-a2ca-4a30-aba0-80a4db3a1895")]
24+
25+
// Version information for an assembly consists of the following four values:
26+
//
27+
// Major Version
28+
// Minor Version
29+
// Build Number
30+
// Revision
31+
//
32+
[assembly: AssemblyVersion("1.0.0.0")]
33+
[assembly: AssemblyFileVersion("1.0.0.0")]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
4+
<PropertyGroup>
5+
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
6+
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
7+
<ProjectGuid>{27FFC4D8-A2CA-4A30-ABA0-80A4DB3A1895}</ProjectGuid>
8+
<OutputType>Exe</OutputType>
9+
<RootNamespace>Replace_existing_text_in_a_PDF</RootNamespace>
10+
<AssemblyName>Replace-existing-text-in-a-PDF</AssemblyName>
11+
<TargetFrameworkVersion>v4.8</TargetFrameworkVersion>
12+
<FileAlignment>512</FileAlignment>
13+
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
14+
<Deterministic>true</Deterministic>
15+
<NuGetPackageImportStamp>
16+
</NuGetPackageImportStamp>
17+
</PropertyGroup>
18+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
19+
<PlatformTarget>AnyCPU</PlatformTarget>
20+
<DebugSymbols>true</DebugSymbols>
21+
<DebugType>full</DebugType>
22+
<Optimize>false</Optimize>
23+
<OutputPath>bin\Debug\</OutputPath>
24+
<DefineConstants>DEBUG;TRACE</DefineConstants>
25+
<ErrorReport>prompt</ErrorReport>
26+
<WarningLevel>4</WarningLevel>
27+
</PropertyGroup>
28+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
29+
<PlatformTarget>AnyCPU</PlatformTarget>
30+
<DebugType>pdbonly</DebugType>
31+
<Optimize>true</Optimize>
32+
<OutputPath>bin\Release\</OutputPath>
33+
<DefineConstants>TRACE</DefineConstants>
34+
<ErrorReport>prompt</ErrorReport>
35+
<WarningLevel>4</WarningLevel>
36+
</PropertyGroup>
37+
<ItemGroup>
38+
<Reference Include="Syncfusion.Compression.Base, Version=32.2462.5.0, Culture=neutral, PublicKeyToken=3d67ed1f87d44c89, processorArchitecture=MSIL">
39+
<HintPath>..\packages\Syncfusion.Compression.Base.32.2.5\lib\net462\Syncfusion.Compression.Base.dll</HintPath>
40+
</Reference>
41+
<Reference Include="Syncfusion.Licensing, Version=32.2462.5.0, Culture=neutral, PublicKeyToken=632609b4d040f6b4, processorArchitecture=MSIL">
42+
<HintPath>..\packages\Syncfusion.Licensing.32.2.5\lib\net462\Syncfusion.Licensing.dll</HintPath>
43+
</Reference>
44+
<Reference Include="Syncfusion.Pdf.Base, Version=32.2462.5.0, Culture=neutral, PublicKeyToken=3d67ed1f87d44c89, processorArchitecture=MSIL">
45+
<HintPath>..\packages\Syncfusion.Pdf.WinForms.32.2.5\lib\net462\Syncfusion.Pdf.Base.dll</HintPath>
46+
</Reference>
47+
<Reference Include="System" />
48+
<Reference Include="System.Core" />
49+
<Reference Include="System.Drawing" />
50+
<Reference Include="System.Numerics" />
51+
<Reference Include="System.Xml.Linq" />
52+
<Reference Include="System.Data.DataSetExtensions" />
53+
<Reference Include="Microsoft.CSharp" />
54+
<Reference Include="System.Data" />
55+
<Reference Include="System.Net.Http" />
56+
<Reference Include="System.Xml" />
57+
</ItemGroup>
58+
<ItemGroup>
59+
<Compile Include="Program.cs" />
60+
<Compile Include="Properties\AssemblyInfo.cs" />
61+
</ItemGroup>
62+
<ItemGroup>
63+
<None Include="App.config" />
64+
<None Include="packages.config" />
65+
</ItemGroup>
66+
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
67+
</Project>
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<packages>
3+
<package id="Syncfusion.Compression.Base" version="32.2.9" targetFramework="net48" />
4+
<package id="Syncfusion.Licensing" version="32.2.9" targetFramework="net48" />
5+
<package id="Syncfusion.Pdf.WinForms" version="32.2.9" targetFramework="net48" />
6+
</packages>

0 commit comments

Comments
 (0)