Skip to content

Commit 70d0b39

Browse files
ES-942103-Replace-image-with-new-image
1 parent b78a969 commit 70d0b39

File tree

6 files changed

+113
-0
lines changed

6 files changed

+113
-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-image-with-new-image", "Replace-image-with-new-image\Replace-image-with-new-image.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
8.55 KB
Loading
Binary file not shown.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
using Syncfusion.DocIO;
2+
using Syncfusion.DocIO.DLS;
3+
using System.IO;
4+
5+
namespace Replace_image_with_new_image
6+
{
7+
class Program
8+
{
9+
static void Main(string[] args)
10+
{
11+
12+
// Open the input Word document for reading and writing
13+
using (FileStream inputStream = new FileStream(@"Data/Template.docx", FileMode.Open, FileAccess.ReadWrite))
14+
{
15+
WordDocument document = new WordDocument(inputStream, FormatType.Docx);
16+
// Find and process all images in the document
17+
foreach (Entity entity in document.FindAllItemsByProperty(EntityType.Picture, null, null))
18+
{
19+
WPicture picture = (WPicture)entity;
20+
float width = picture.Width, height = picture.Height;
21+
// Check if the image is not an SVG
22+
if (picture.SvgData == null)
23+
{
24+
// Replace the existing image with a new one
25+
using (FileStream imageStream = new FileStream(@"Data/Picture.png", FileMode.Open))
26+
{
27+
picture.LoadImage(imageStream);
28+
}
29+
// Preserve the original dimensions
30+
picture.LockAspectRatio = false;
31+
picture.Width = width;
32+
picture.Height = height;
33+
picture.LockAspectRatio = true;
34+
}
35+
else
36+
{
37+
// Handle SVG conversion to raster image
38+
WParagraph ownerParagraph = picture.OwnerParagraph;
39+
int index = ownerParagraph.ChildEntities.IndexOf(picture);
40+
// Remove the existing SVG image
41+
ownerParagraph.ChildEntities.Remove(picture);
42+
// Create a new image and insert it in the same place
43+
WPicture newPicture = new WPicture(document);
44+
using (FileStream imageStream = new FileStream(@"Data/Picture.png", FileMode.Open))
45+
{
46+
newPicture.LoadImage(imageStream);
47+
}
48+
// Set the same dimensions as the original SVG
49+
newPicture.Width = width;
50+
newPicture.Height = height;
51+
ownerParagraph.ChildEntities.Insert(index, newPicture);
52+
}
53+
}
54+
// Save the modified document
55+
using (FileStream outputStream = new FileStream(@"Output/Result.docx", FileMode.Create, FileAccess.Write))
56+
{
57+
document.Save(outputStream, FormatType.Docx);
58+
}
59+
}
60+
}
61+
}
62+
}
Lines changed: 25 additions & 0 deletions
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_image_with_new_image</RootNamespace>
7+
</PropertyGroup>
8+
9+
<ItemGroup>
10+
<PackageReference Include="Syncfusion.DocIO.Net.Core" Version="*" />
11+
</ItemGroup>
12+
13+
<ItemGroup>
14+
<None Update="Data\Picture.png">
15+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
16+
</None>
17+
<None Update="Data\Template.docx">
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)