Skip to content

Commit 0a9b2d9

Browse files
Added sample to replace a logo with text and another image with different text
1 parent f4b8cb0 commit 0a9b2d9

File tree

6 files changed

+102
-0
lines changed

6 files changed

+102
-0
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio Version 16
4+
VisualStudioVersion = 16.0.31911.196
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Replace-logo-and-image-with-text", "Replace-logo-and-image-with-text\Replace-logo-and-image-with-text.csproj", "{C17B90BC-F559-456B-B189-90B53FF6CDD4}"
7+
EndProject
8+
Global
9+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
10+
Debug|Any CPU = Debug|Any CPU
11+
Release|Any CPU = Release|Any CPU
12+
EndGlobalSection
13+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
14+
{C17B90BC-F559-456B-B189-90B53FF6CDD4}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{C17B90BC-F559-456B-B189-90B53FF6CDD4}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{C17B90BC-F559-456B-B189-90B53FF6CDD4}.Release|Any CPU.ActiveCfg = Release|Any CPU
17+
{C17B90BC-F559-456B-B189-90B53FF6CDD4}.Release|Any CPU.Build.0 = Release|Any CPU
18+
EndGlobalSection
19+
GlobalSection(SolutionProperties) = preSolution
20+
HideSolutionNode = FALSE
21+
EndGlobalSection
22+
GlobalSection(ExtensibilityGlobals) = postSolution
23+
SolutionGuid = {EF357FC6-E9E5-4E3C-B932-43F727BE1DE4}
24+
EndGlobalSection
25+
EndGlobal
5.01 KB
Loading
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
using Syncfusion.DocIO;
2+
using Syncfusion.DocIO.DLS;
3+
using System.Collections.Generic;
4+
using System.IO;
5+
6+
namespace Replace_logo_and_image_with_text
7+
{
8+
class Program
9+
{
10+
static void Main(string[] args)
11+
{
12+
using (FileStream fileStreamPath = new FileStream(Path.GetFullPath(@"Data/Input.docx"), FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
13+
{
14+
//Open an existing Word document.
15+
using (WordDocument document = new WordDocument(fileStreamPath, FormatType.Automatic))
16+
{
17+
//Open the logo image stream to compare with existing images in the document.
18+
FileStream logoImageStream = new FileStream(@"Data/LogoImage.jpg", FileMode.OpenOrCreate, FileAccess.ReadWrite);
19+
//Find all pictures in the Word document by their EntityType
20+
List<Entity> pictures = document.FindAllItemsByProperty(EntityType.Picture, null, null);
21+
//Iterate through each picture found in the document.
22+
foreach (WPicture picture in pictures)
23+
{
24+
WParagraph ownerParagraph = picture.OwnerParagraph;
25+
//Find the index of picture in the owner paragraph.
26+
int index = ownerParagraph.ChildEntities.IndexOf(picture);
27+
//Remove the picture from its owner paragraph.
28+
ownerParagraph.ChildEntities.Remove(picture);
29+
//Create a new text range for inserting text.
30+
WTextRange textRange = new WTextRange(document);
31+
//Check if the current picture's image byte length matches the logo image's length.
32+
if (picture.ImageBytes.Length == logoImageStream.Length)
33+
//Replace the logo image with the text "< Logo image here >".
34+
textRange.Text = "< Logo image here >";
35+
else
36+
//Replace other images with the text "< Product image here >".
37+
textRange.Text = "< Product image here >";
38+
//Insert the newly created text range at the index of the removed picture.
39+
ownerParagraph.ChildEntities.Insert(index, textRange);
40+
}
41+
//Create file stream.
42+
using (FileStream outputFileStream = new FileStream(Path.GetFullPath(@"Output/Output.docx"), FileMode.Create, FileAccess.ReadWrite))
43+
{
44+
//Save the Word document to file stream.
45+
document.Save(outputFileStream, FormatType.Docx);
46+
}
47+
}
48+
}
49+
}
50+
}
51+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net8.0</TargetFramework>
6+
<RootNamespace>Replace_logo_and_image_with_text</RootNamespace>
7+
</PropertyGroup>
8+
9+
<ItemGroup>
10+
<PackageReference Include="Syncfusion.DocIO.Net.Core" Version="*" />
11+
</ItemGroup>
12+
13+
<ItemGroup>
14+
<None Update="Data\Input.docx">
15+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
16+
</None>
17+
<None Update="Data\LogoImage.jpg">
18+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
19+
</None>
20+
<None Update="Output\.gitkeep">
21+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
22+
</None>
23+
</ItemGroup>
24+
25+
</Project>

0 commit comments

Comments
 (0)