Articles in this section
Category / Section

How to convert HTML to EMF in C# and VB.NET WinForms PDF?

6 mins read

Syncfusion Essential® PDF is a .NET PDF Library used to create, read, and edit PDF documents. Using this library, you can convert HTML to EMF in C# and VB.NET using IE Rendering.

Steps to convert HTML to EMF programmatically:

  1. Create a new C# console application project. Create a console application project
  2. Install the Syncfusion.HtmlToPdfConverter.IE.WinForms NuGet package as reference to your .NET Framework application from NuGet.org. NuGet package reference screenshot
  3. Include the following namespace in the Program.cs file.

C#

using Syncfusion.HtmlConverter;
using Syncfusion.Pdf.HtmlToPdf;
using System.Drawing;
using System.Drawing.Imaging;

 

VB.NET

Imports Syncfusion.HtmlConverter
Imports Syncfusion.Pdf.HtmlToPdf
Imports System.Drawing.Imaging
Imports System.Drawing

 

  1. You cannot directly save the metafile as EMF. When you use the Save method to save a graphic image as Enhanced Metafile Format (EMF) file, the resulting file is saved as a Portable Network Graphics (PNG) file.  This is the behavior of Microsoft. However, as a workaround, you can convert HTML to EMF using the following code snippet.

C#

//Initialize HTML converter
HtmlConverter html = new HtmlConverter();
//Convert the URL
HtmlToPdfResult result1 = html.Convert(System.IO.Path.GetFullPath("input.html"), ImageType.Metafile, 500, 500, AspectRatio.None); 
//Convert EMF to the supported format
Metafile file = CreateMetaImage(result1.RenderedImage as Metafile);
Graphics g = Graphics.FromImage(file);
g.DrawImage(result1.RenderedImage, Point.Empty);
g.Dispose();
file.Dispose();

 

VB.NET

'Initialize HTML converter
Dim html As New HtmlConverter()
'Convert the URL
Dim result1 As HtmlToPdfResult = html.Convert(System.IO.Path.GetFullPath("Input.html"), ImageType.Metafile, 500, 500, AspectRatio.None)
'Convert EMF to the supported format
Dim file As Metafile = CreateMetaImage(TryCast(result1.RenderedImage, Metafile))
Dim g As Graphics = Graphics.FromImage(file)
g.DrawImage(result1.RenderedImage, Point.Empty)
g.Dispose()
file.Dispose()

 

  1. Add the following code in CreateMetaImage method to check the format of the metafile.

C#

public static Metafile CreateMetaImage(Metafile image)
{
    Metafile metaImage;
    MemoryStream stream = new MemoryStream();
    using (Bitmap bitmap = new Bitmap(image.Width, image.Height))
    {
        using (Graphics g = Graphics.FromImage(bitmap))
        {
            bitmap.SetResolution(g.DpiX, g.DpiY);
            IntPtr hdc = g.GetHdc();
            Rectangle rect = new Rectangle(0, 0, image.Width, image.Height);
            metaImage = new Metafile("HTMLToEMF.emf", hdc, rect, MetafileFrameUnit.Pixel, EmfType.EmfOnly);
            g.ReleaseHdc();
        }
    }
    return metaImage;
}

 

VB.NET

Public Function CreateMetaImage(image As Metafile) As Metafile
    Dim metaImage As Metafile
    Dim stream As New MemoryStream()
    Using bitmap As New Bitmap(image.Width, image.Height)
        Using g As Graphics = Graphics.FromImage(bitmap)
            bitmap.SetResolution(g.DpiX, g.DpiY)
            Dim hdc As IntPtr = g.GetHdc()
            Dim rect As New Rectangle(0, 0, image.Width, image.Height)
            metaImage = New Metafile("HTMLToEMF.emf", hdc, rect, MetafileFrameUnit.Pixel, EmfType.EmfOnly)
            g.ReleaseHdc()
        End Using
    End Using
    Return metaImage
End Function

 

You can download the work sample from HTMLToEMFSample.zip.

By executing the program, you will get the EMF file as follows. Output EMF file screenshot in PDF

Take a moment to peruse the documentation, where you can find options like conversion of HTML to PDF using Blink Rendering and features like MHTML to PDFHTML to MHTMLHTML to Raster Image, and HTML to SVG with code examples.

Refer here to explore the rich set of Syncfusion Essential® PDF features.

Note:

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


Conclusion

I hope you enjoyed learning how to convert HTML to EMF in C# and VB.NET WinForms PDF.
You can refer to our WinForms PDF 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 WinForms PDF example to understand how to create and manipulate data.
For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion®, you can try our 30-day free trial to check out our other 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!
Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please  to leave a comment
Access denied
Access denied