We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

How to convert and replace EMF image in word document to PNG with same size?

Platform: ASP.NET Core |
Control: DocIO
Tags: c#, png, vb.net, emf

Syncfusion Essential DocIO is a .NET Word library used to create, read, and edit Word documents programmatically without Microsoft Word or interop dependencies. In .NET Core targeting applications, metafile images (.wmf and *.emf) have some limitations. Internally, if the Word document contains metafile images (.wmf” or *.emf”), Essential DocIO preserves those images as RedX images with the same size as the original metafile images during Word to PDF or Image conversions to avoid pagination issues.

To preserve the expected images in the PDF, we suggest converting the metafile image formats to bitmap image formats (JPEG or PNG) and then performing Word to PDF conversion.

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.

By executing the program, you will get the output PDF document as follows.

Graphical user interface, table in ASP.NET Core Word

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.

 

2X faster development

The ultimate ASP.NET Core UI toolkit to boost your development speed.
ADD COMMENT
You must log in to leave a comment

Please sign in to access our KB

This page will automatically be redirected to the sign-in page in 10 seconds.

Up arrow icon

Warning Icon You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.Close Icon

Live Chat Icon For mobile