Skip to content

ES-942103- Add the sample Replace-image-with-new-image #349

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 16
VisualStudioVersion = 16.0.31911.196
MinimumVisualStudioVersion = 10.0.40219.1
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}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{C17B90BC-F559-456B-B189-90B53FF6CDD4}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{C17B90BC-F559-456B-B189-90B53FF6CDD4}.Debug|Any CPU.Build.0 = Debug|Any CPU
{C17B90BC-F559-456B-B189-90B53FF6CDD4}.Release|Any CPU.ActiveCfg = Release|Any CPU
{C17B90BC-F559-456B-B189-90B53FF6CDD4}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {EF357FC6-E9E5-4E3C-B932-43F727BE1DE4}
EndGlobalSection
EndGlobal
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
using Syncfusion.DocIO;
using Syncfusion.DocIO.DLS;
using System.IO;

namespace Replace_image_with_new_image
{
class Program
{
static void Main(string[] args)
{

// Open the input Word document for reading and writing
using (FileStream inputStream = new FileStream(@"Data/Template.docx", FileMode.Open, FileAccess.ReadWrite))
{
WordDocument document = new WordDocument(inputStream, FormatType.Docx);
// Find and process all images in the document
foreach (Entity entity in document.FindAllItemsByProperty(EntityType.Picture, null, null))
{
WPicture picture = (WPicture)entity;
float width = picture.Width, height = picture.Height;
// Check if the image is not an SVG
if (picture.SvgData == null)
{
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does the input document covers both IF, Else cases?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes sir used png image in header and svg image in table

// Replace the existing image with a new one
using (FileStream imageStream = new FileStream(@"Data/Picture.png", FileMode.Open))
{
picture.LoadImage(imageStream);
}
// Preserve the original dimensions
picture.LockAspectRatio = false;
picture.Width = width;
picture.Height = height;
picture.LockAspectRatio = true;
}
else
{
// Handle SVG conversion to raster image
WParagraph ownerParagraph = picture.OwnerParagraph;
int index = ownerParagraph.ChildEntities.IndexOf(picture);
// Remove the existing SVG image
ownerParagraph.ChildEntities.Remove(picture);
// Create a new image and insert it in the same place
WPicture newPicture = new WPicture(document);
using (FileStream imageStream = new FileStream(@"Data/Picture.png", FileMode.Open))
{
newPicture.LoadImage(imageStream);
}
// Set the same dimensions as the original SVG
newPicture.Width = width;
newPicture.Height = height;
ownerParagraph.ChildEntities.Insert(index, newPicture);
}
}
// Save the modified document
using (FileStream outputStream = new FileStream(@"Output/Result.docx", FileMode.Create, FileAccess.Write))
{
document.Save(outputStream, FormatType.Docx);
}
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<RootNamespace>Replace_image_with_new_image</RootNamespace>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Syncfusion.DocIO.Net.Core" Version="*" />
</ItemGroup>

<ItemGroup>
<None Update="Data\Picture.png">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="Data\Template.docx">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="Output\.gitkeep">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
</ItemGroup>

</Project>
Loading