diff --git a/Paragraphs/Replace-image-with-new-image/.NET/Replace-image-with-new-image.sln b/Paragraphs/Replace-image-with-new-image/.NET/Replace-image-with-new-image.sln new file mode 100644 index 000000000..74b1e87b1 --- /dev/null +++ b/Paragraphs/Replace-image-with-new-image/.NET/Replace-image-with-new-image.sln @@ -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 diff --git a/Paragraphs/Replace-image-with-new-image/.NET/Replace-image-with-new-image/Data/Picture.png b/Paragraphs/Replace-image-with-new-image/.NET/Replace-image-with-new-image/Data/Picture.png new file mode 100644 index 000000000..d16736cf5 Binary files /dev/null and b/Paragraphs/Replace-image-with-new-image/.NET/Replace-image-with-new-image/Data/Picture.png differ diff --git a/Paragraphs/Replace-image-with-new-image/.NET/Replace-image-with-new-image/Data/Template.docx b/Paragraphs/Replace-image-with-new-image/.NET/Replace-image-with-new-image/Data/Template.docx new file mode 100644 index 000000000..654f7566c Binary files /dev/null and b/Paragraphs/Replace-image-with-new-image/.NET/Replace-image-with-new-image/Data/Template.docx differ diff --git a/Paragraphs/Replace-image-with-new-image/.NET/Replace-image-with-new-image/Output/.gitkeep b/Paragraphs/Replace-image-with-new-image/.NET/Replace-image-with-new-image/Output/.gitkeep new file mode 100644 index 000000000..5f282702b --- /dev/null +++ b/Paragraphs/Replace-image-with-new-image/.NET/Replace-image-with-new-image/Output/.gitkeep @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/Paragraphs/Replace-image-with-new-image/.NET/Replace-image-with-new-image/Program.cs b/Paragraphs/Replace-image-with-new-image/.NET/Replace-image-with-new-image/Program.cs new file mode 100644 index 000000000..80bea74cc --- /dev/null +++ b/Paragraphs/Replace-image-with-new-image/.NET/Replace-image-with-new-image/Program.cs @@ -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) + { + // 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); + } + } + } + } +} diff --git a/Paragraphs/Replace-image-with-new-image/.NET/Replace-image-with-new-image/Replace-image-with-new-image.csproj b/Paragraphs/Replace-image-with-new-image/.NET/Replace-image-with-new-image/Replace-image-with-new-image.csproj new file mode 100644 index 000000000..831b449cf --- /dev/null +++ b/Paragraphs/Replace-image-with-new-image/.NET/Replace-image-with-new-image/Replace-image-with-new-image.csproj @@ -0,0 +1,25 @@ + + + + Exe + net8.0 + Replace_image_with_new_image + + + + + + + + + Always + + + Always + + + Always + + + +