Skip to content

Commit 4974076

Browse files
Added Sample for replace image with same size
1 parent e9e71c5 commit 4974076

File tree

6 files changed

+93
-0
lines changed

6 files changed

+93
-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
73.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: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
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+
// Retrieve the body of the first section.
12+
WTextBody textbody = document.Sections[0].Body;
13+
// Iterates through each paragraph in the document's body.
14+
foreach (WParagraph paragraph in textbody.Paragraphs)
15+
{
16+
// Iterates through each item within the paragraph.
17+
foreach (ParagraphItem item in paragraph.ChildEntities)
18+
{
19+
// Checks if the item is a picture.
20+
if (item is WPicture)
21+
{
22+
// Casts the item to WPicture to access picture properties.
23+
WPicture picture = item as WPicture;
24+
// Store the original width and height of the picture.
25+
pictureWidth = picture.Width;
26+
pictureHeight = picture.Height;
27+
// Open the new image file to replace the existing picture.
28+
FileStream imageStream = new FileStream(Path.GetFullPath(@"Data/Image.png"), FileMode.Open, FileAccess.ReadWrite);
29+
// Load the new image into the existing picture element.
30+
picture.LoadImage(imageStream);
31+
// Restore the original width and height to maintain the picture's dimensions.
32+
picture.Width = pictureWidth;
33+
picture.Height = pictureHeight;
34+
}
35+
}
36+
}
37+
using (FileStream outputFileStream = new FileStream(Path.GetFullPath(@"Output/Result.docx"), FileMode.Create, FileAccess.ReadWrite))
38+
{
39+
// Saves the modified Word document.
40+
document.Save(outputFileStream, FormatType.Docx);
41+
}
42+
}
43+
}
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)