Skip to content

Commit 05ab484

Browse files
Merge pull request #511 from SyncfusionExamples/963169-EMF
963169-How to convert EMF images to PNG in a Word document using MetafileRenderer
2 parents 563b6f5 + 76c7913 commit 05ab484

File tree

6 files changed

+151
-0
lines changed

6 files changed

+151
-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 17
4+
VisualStudioVersion = 17.14.36908.2 d17.14
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Convert_EMF-images_to_PNG_in_Word-document", "Convert_EMF-images_to_PNG_in_Word-document\Convert_EMF-images_to_PNG_in_Word-document.csproj", "{0FB638DB-2793-4FB6-B1B3-18F786EA8EF5}"
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+
{0FB638DB-2793-4FB6-B1B3-18F786EA8EF5}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{0FB638DB-2793-4FB6-B1B3-18F786EA8EF5}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{0FB638DB-2793-4FB6-B1B3-18F786EA8EF5}.Release|Any CPU.ActiveCfg = Release|Any CPU
17+
{0FB638DB-2793-4FB6-B1B3-18F786EA8EF5}.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 = {3AC4FA9F-473F-4350-B422-A0C4691A8EA6}
24+
EndGlobalSection
25+
EndGlobal
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net8.0</TargetFramework>
6+
<RootNamespace>Convert_EMF_images_to_PNG_in_Word_document</RootNamespace>
7+
<ImplicitUsings>enable</ImplicitUsings>
8+
<Nullable>enable</Nullable>
9+
</PropertyGroup>
10+
11+
<ItemGroup>
12+
<PackageReference Include="Syncfusion.DocIO.Net.Core" Version="*" />
13+
<PackageReference Include="Syncfusion.MetafileRenderer.Net.Core" Version="*" />
14+
</ItemGroup>
15+
<ItemGroup>
16+
<None Update="Data\Input.docx">
17+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
18+
</None>
19+
<None Update="Output\.gitkeep">
20+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
21+
</None>
22+
</ItemGroup>
23+
</Project>

HTML-conversions/Convert_EMF-images_to_PNG_in_Word-document/.NET/Convert_EMF-images_to_PNG_in_Word-document/Output/Result.html

Lines changed: 45 additions & 0 deletions
Large diffs are not rendered by default.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
using SkiaSharp;
2+
using Syncfusion.DocIO;
3+
using Syncfusion.DocIO.DLS;
4+
using Syncfusion.Drawing;
5+
using Syncfusion.Metafile;
6+
7+
8+
9+
using (FileStream inputStream = new FileStream(Path.GetFullPath(@"Data/Input.docx", FileMode.Open, FileAccess.ReadWrite))
10+
{
11+
// Opens an input Word template.
12+
using (WordDocument document = new WordDocument(inputStream, FormatType.Docx))
13+
{
14+
// Find all picture in the document.
15+
List<Entity> images = document.FindAllItemsByProperty(EntityType.Picture, null, null);
16+
// Iterate through each image in the document.
17+
foreach (Entity entity in images)
18+
{
19+
WPicture picture = entity as WPicture;
20+
float width = picture.Width;
21+
float height = picture.Height;
22+
23+
// Convert the image bytes into a memory stream.
24+
MemoryStream stream = new MemoryStream(picture.ImageBytes);
25+
// Create an Image object from the memory stream.
26+
Image image = Image.FromStream(stream);
27+
// Check if the image format is EMF.
28+
if (image.RawFormat.Equals(ImageFormat.Emf))
29+
{
30+
MemoryStream imageByteStream = new MemoryStream(picture.ImageBytes);
31+
//Create a new instance for the MetafileRenderer
32+
MetafileRenderer renderer = new MetafileRenderer();
33+
//Convert the Metafile to SKBitmap Image.
34+
SKBitmap skBitmap = renderer.ConvertToImage(imageByteStream);
35+
//Save the image as stream
36+
using (SKImage skImage = SKImage.FromBitmap(skBitmap))
37+
using (SKData data = skImage.Encode(SKEncodedImageFormat.Png, 100))
38+
39+
using (MemoryStream pngStream = new MemoryStream())
40+
{
41+
data.SaveTo(pngStream);
42+
pngStream.Position = 0;
43+
picture.LoadImage(pngStream);
44+
picture.Height = height;
45+
picture.Width = width;
46+
}
47+
}
48+
}
49+
// Save the modified document to the specified output file.
50+
using (FileStream outputStream = new FileStream(Path.GetFullPath(@"Output/Result.html"), FileMode.Create, FileAccess.Write))
51+
{
52+
document.Save(outputStream, FormatType.Html);
53+
}
54+
}
55+
}
56+
57+

0 commit comments

Comments
 (0)