Skip to content

Commit fa6d961

Browse files
Merge pull request #279 from VijayadharshiniMathiyalagan/ES-903880-How-to-replace-an-image-with-same-size-in-a-Word-document
Add sample for replace image with same size
2 parents fb1278c + f2fdf59 commit fa6d961

File tree

6 files changed

+79
-0
lines changed

6 files changed

+79
-0
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio Version 17
4+
VisualStudioVersion = 17.12.35417.141 d17.12
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Replace-image-with-same-size", "Replace-image-with-same-size\Replace-image-with-same-size.csproj", "{A03D22EE-16FA-4703-90BF-1C5B4AE5FDD8}"
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+
{A03D22EE-16FA-4703-90BF-1C5B4AE5FDD8}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{A03D22EE-16FA-4703-90BF-1C5B4AE5FDD8}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{A03D22EE-16FA-4703-90BF-1C5B4AE5FDD8}.Release|Any CPU.ActiveCfg = Release|Any CPU
17+
{A03D22EE-16FA-4703-90BF-1C5B4AE5FDD8}.Release|Any CPU.Build.0 = Release|Any CPU
18+
EndGlobalSection
19+
GlobalSection(SolutionProperties) = preSolution
20+
HideSolutionNode = FALSE
21+
EndGlobalSection
22+
EndGlobal
87.5 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: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
using Syncfusion.DocIO;
2+
using Syncfusion.DocIO.DLS;
3+
4+
using (FileStream fileStreamPath = new FileStream(Path.GetFullPath(@"Data/Template.docx"), FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
5+
{
6+
// Opens the template Word document.
7+
using (WordDocument document = new WordDocument(fileStreamPath, FormatType.Automatic))
8+
{
9+
// Variables to store the original width and height of each picture.
10+
float pictureWidth, pictureHeight = 0;
11+
// Find the picture with alternative text.
12+
WPicture picture = document.FindItemByProperty(EntityType.Picture, "AlternativeText", "Adventure") as WPicture;
13+
// Store the original width and height of the picture.
14+
pictureWidth = picture.Width;
15+
pictureHeight = picture.Height;
16+
// Open the new image file to replace the existing picture.
17+
FileStream imageStream = new FileStream(Path.GetFullPath(@"Data/Image.png"), FileMode.Open, FileAccess.ReadWrite);
18+
// Load the new image into the existing picture element.
19+
picture.LoadImage(imageStream);
20+
// Restore the original width and height to maintain the picture's dimensions.
21+
picture.Width = pictureWidth;
22+
picture.Height = pictureHeight;
23+
using (FileStream outputFileStream = new FileStream(Path.GetFullPath(@"Output/Result.docx"), FileMode.Create, FileAccess.ReadWrite))
24+
{
25+
// Saves the modified Word document.
26+
document.Save(outputFileStream, FormatType.Docx);
27+
}
28+
}
29+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net8.0</TargetFramework>
6+
<RootNamespace>Replace_image_with_same_size</RootNamespace>
7+
<ImplicitUsings>enable</ImplicitUsings>
8+
<Nullable>enable</Nullable>
9+
</PropertyGroup>
10+
11+
<ItemGroup>
12+
<PackageReference Include="Syncfusion.DocIO.Net.Core" Version="*" />
13+
</ItemGroup>
14+
15+
<ItemGroup>
16+
<None Update="Data\Image.png">
17+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
18+
</None>
19+
<None Update="Data\Template.docx">
20+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
21+
</None>
22+
<None Update="Output\.gitkeep">
23+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
24+
</None>
25+
</ItemGroup>
26+
27+
</Project>

0 commit comments

Comments
 (0)