How to Use print Media Type in HTML to WinForms PDF Conversion?
The Syncfusion® HTML to PDF converter is a .NET PDF library for converting webpages, SVG, MHTML, and HTML to PDF using C#. It is reliable and accurate. The result preserves all graphics, images, texts, fonts, and the layout of the original HTML documents or webpages.
Using this library, you can set the print media type while converting an HTML to PDF document.
Steps to use print media type while converting HTML to PDF:
- Create a new C# Windows Forms application project.
- Install the Syncfusion.HtmlToPdfConverter.WinForms NuGet package as a reference to your .NET Framework application from NuGet.org.
- Include the following namespaces in the Form1.cs file:
C#
using Syncfusion.HtmlConverter; using Syncfusion.Pdf; using System.IO;
- Set the MediaType property available in the BlinkConverterSettings instance to set the print media type during HTML to PDF conversion. Please find the code snippet for the same below.
C#
string url = Path.GetFullPath("../../Data/input.html");
// Initialize HTML to PDF converter
HtmlToPdfConverter htmlConverter = new HtmlToPdfConverter();
BlinkConverterSettings blinkConverterSettings = new BlinkConverterSettings();
// Set MediaType property of BlinkConverterSettings
blinkConverterSettings.MediaType = MediaType.Print;
// Assign Blink settings to HTML converter
htmlConverter.ConverterSettings = blinkConverterSettings;
// Convert HTML to PDF
PdfDocument document = htmlConverter.Convert(url);
// Save and close the PDF document
document.Save("Sample.pdf");
document.Close(true);
A complete working sample can be downloaded from the HTMLtoPDF_Sample.zip.
Screenshot for print media type
Screenshot for screen media type
Take a moment to peruse the documentation for Converting HTML to PDF, where you will find instructions for converting HTML pages to PDF documents along with the respective customization options and features.
Refer here to explore the rich set of Syncfusion Essential® PDF features.
An online sample link for Converting HTML to PDF.
Starting with v16.2.0.x, if you reference Syncfusion® assemblies from the trial setup or the NuGet feed, include a license key in your projects. Refer to the link to learn about generating and registering the Syncfusion® license key in your application to use the components without a trial message.