Articles in this section
Category / Section

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

6 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. 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.

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

  1. Install the Syncfusion.DocIORenderer.Net.Core NuGet package as a reference to your .NET Core applications from NuGet.org.
    Install 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)
{
    //Find all images by EntityType in Word document.
    List<Entity> images = wordDocument.FindAllItemsByProperty(EntityType.Picture, null, null);
    //Replace EMF with PNG images.
    for (int i = 0; i < images.Count; i++)
    {
        WPicture picture = images[i] as WPicture;
        //Convert EMG image format to PNG format with the same size.
        Image image = Image.FromStream(new MemoryStream(picture.ImageBytes));
        if (image.RawFormat.Equals(ImageFormat.Emf) || image.RawFormat.Equals(ImageFormat.Wmf))
        {
            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;
            imageStream.Dispose();
        }
    }
}

VB

Private Sub ConvertEMFToPNG(ByVal wordDocument As WordDocument)
        'Find all images by EntityType in Word document.
        Dim images As List(Of Entity) = wordDocument.FindAllItemsByProperty(EntityType.Picture, Nothing, Nothing)
        'Replace EMF with PNG images.
        For i = 0 To images.Count - 1
            Dim picture As WPicture = TryCast(images(i), WPicture)
            'Convert EMG image format to PNG format with the same size.
            Dim image As Image = Image.FromStream(New MemoryStream(picture.ImageBytes))
            If image.RawFormat.Equals(ImageFormat.Emf) OrElse image.RawFormat.Equals(ImageFormat.Wmf) 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
                imageStream.Dispose()
            End If
        Next
    End Sub

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

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

Output for word document to png.


Note:

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

Conclusion

I hope you enjoyed learning about how to convert and replace EMF image in word document to PNG with same size in ASP.NET Core File Format Libraries.

You can refer to our File Formats Libraries feature tour page to know about its other groundbreaking feature representations and documentation, and how to quickly get started for configuration specifications. You can also explore our PDF example to understand how to create and manipulate data in the .NET PDF. 

For current customers, you can check out our Document processing libraries from the License and Downloads page. If you are new to Syncfusion, you can try our 30-day free trial to check out our .NET Core Controls.

If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forumsDirect-Trac, or feedback portal. We are always happy to assist you!


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 sign in to leave a comment
Access denied
Access denied