How to convert HTML to EMF in C# and VB.NET WinForms PDF?
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:
- Create a new C# console application project.

- Install the Syncfusion.HtmlToPdfConverter.IE.WinForms NuGet package as reference to your .NET Framework application from NuGet.org.

- 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
- 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()
- 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. 
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 PDF, HTML to MHTML, HTML 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