Articles in this section
Category / Section

How can an EMF image be changed to a PNG with the same size in a word document?

3 mins read

Syncfusion® Essential® DocIO is a .NET Word library used to create, read, and edit Word documents programmatically without Microsoft Word or interop dependencies. Using this library, you can change the EMF image to PNG with same size in a word document using C#.

The conversion of metafile image formats (wmf or emf) to bitmap image formats (JPEG or PNG) externally uses the System.Drawing.Common package, which is only applicable in a Windows environment.  For more information regarding this limitation, please refer to this link. Therefore, it is important to acknowledge that there is no cross-platform solution available for converting metafile image formats to PNG files.

To convert and replace EMF image in word document to PNG of the same size using C#

  1. Create a new C# console application project in .NET Core.

ConsoleCore in ASP.NET Core Word

  1. Install the Syncfusion.DocIORenderer.Net.Core NuGet package as a reference to your .NET Core applications from NuGet.org.
    Nuget Package in ASP.NET Core Word
  2. Include the following namespace in the Program.cs file.

C#

using Syncfusion.DocIO;
using Syncfusion.DocIO.DLS;
using Syncfusion.DocIORenderer;
using Syncfusion.Pdf;
using System.Drawing;
using System.Drawing.Imaging;

VB

Imports System.Drawing
Imports System.Drawing.Imaging
Imports Syncfusion.DocIO
Imports Syncfusion.DocIO.DLS
Imports Syncfusion.DocIORenderer
Imports Syncfusion.Pdf
  1. Use the following code example to convert and replace EMF image to PNG in the Word to PDF conversion.

C#

//Open the file as a Stream.
using (FileStream docStream = new FileStream(@"../../../SyncfusionConvertWordToPdfIssueDoc.docx", FileMode.Open, FileAccess.Read))
            {
                //Load a file stream into a Word document.
                using (WordDocument wordDocument = new WordDocument(docStream, FormatType.Automatic))
                {
                    ConvertEMFToPNG(wordDocument);
                    //Instantiation of DocIORenderer for Word to PDF conversion.
                    DocIORenderer render = new DocIORenderer();
                    //Convert a Word document into a PDF document.
                    using (PdfDocument pdfDocument = render.ConvertToPDF(wordDocument))
                    {
                        //Save the PDF file.
                        using (FileStream outputFile = new FileStream("Output.pdf", FileMode.OpenOrCreate, FileAccess.ReadWrite))
                            pdfDocument.Save(outputFile);
                        
                    }
                }
            }

 

VB

       'Open the file as a Stream
        Using docStream As FileStream = New FileStream("../../../SyncfusionConvertWordToPdfIssueDoc.docx", FileMode.Open, FileAccess.Read)
            'Load a file stream into a Word document
            Using wordDocument As WordDocument = New WordDocument(docStream, FormatType.Automatic)
                ConvertEMFToPNG(wordDocument)
                'Instantiation of DocIORenderer for Word to PDF conversion
                Dim render As DocIORenderer = New DocIORenderer
                'Convert a Word document into a PDF document
                Using pdfDocument As PdfDocument = render.ConvertToPDF(wordDocument)
                    'Saves the PDF file
                    Using outputFile As FileStream = New FileStream("Output.pdf", FileMode.OpenOrCreate, FileAccess.ReadWrite)
                        pdfDocument.Save(outputFile)
                    End Using
                End Using
            End Using
        End Using

 

  1. Use the following helper method to convert EMG image format to PNG format with the same size.

C#

        private static void ConvertEMFToPNG(WordDocument wordDocument)
        {
            WTextBody textbody = wordDocument.Sections[0].Body;
            //Iterates through the paragraphs of the textbody
            foreach (WParagraph paragraph in textbody.Paragraphs)
            {
                //Iterates through the child elements of a paragraph
                foreach (ParagraphItem item in paragraph.ChildEntities)
                {
                    if (item is WPicture)
                    {
                        WPicture picture = item as WPicture;
 
                        Image image = Image.FromStream(new MemoryStream(picture.ImageBytes));
                        if (image.RawFormat.Equals(ImageFormat.Emf))
                        {
                            float height = picture.Height;
                            float width = picture.Width;
                            FileStream imgFile = new FileStream("Output.png", FileMode.OpenOrCreate, FileAccess.ReadWrite);
                            image.Save(imgFile, ImageFormat.Png);
                            imgFile.Dispose();
                            image.Dispose();
 
                            FileStream imageStream = new FileStream(@"Output.png", FileMode.Open, FileAccess.ReadWrite);
                            picture.LoadImage(imageStream);
                            picture.LockAspectRatio = false;
                            picture.Height = height;
                            picture.Width = width;
                        }
                    }
                }
            }
        }

 

VB

    

Private Sub ConvertEMFToPNG(ByVal wordDocument As WordDocument)
        Dim textbody As WTextBody = wordDocument.Sections(0).Body
        'Iterates through the paragraphs of the textbody
        For Each paragraph As WParagraph In textbody.Paragraphs
            'Iterates through the child elements of a paragraph
            For Each item As ParagraphItem In paragraph.ChildEntities
                If (TypeOf item Is WPicture) Then
                    Dim picture As WPicture = CType(item, WPicture)
                    Dim image As Image = Image.FromStream(New MemoryStream(picture.ImageBytes))
                    If image.RawFormat.Equals(ImageFormat.Emf) Then
                        Dim height As Single = picture.Height
                        Dim width As Single = picture.Width
                        Dim imgFile As FileStream = New FileStream("Output.png", FileMode.OpenOrCreate, FileAccess.ReadWrite)
                        image.Save(imgFile, ImageFormat.Png)
                        imgFile.Dispose()
                        image.Dispose
                        Dim imageStream As FileStream = New FileStream("Output.png", FileMode.Open, FileAccess.ReadWrite)
                        picture.LoadImage(imageStream)
                        picture.LockAspectRatio = False
                        picture.Height = height
                        picture.Width = width
                    End If
 
                End If
 
            Next
        Next
    End Sub

A complete working example in C# can be downloaded from the sample.

Take a moment to peruse the documentation, where you can find the basic Word document processing options along with the features like mail merge, merge and split documents, find and replace text in the Word document, protect the Word documents, and most importantly PDF and Image conversions with code examples.

Explore more about the rich set of Syncfusion® Word Framework features.

Note:

Starting with v16.2.0.x, if you refer to Syncfusion® assemblies from trial setup or from the NuGet feed, include a license key in your projects. Refer to the link to learn about generating and registering a Syncfusion® license key in your application to use the components without trail message.

 

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments
Please  to leave a comment
Access denied
Access denied