Codecs: How to convert a HTML file to a PDF file?
In This Topic
Here is C#/VB.NET code that shows how to convert a HTML file to a PDF file using
ImageCollection and
PdfEncoder classes:
/// <summary>
/// Converts HTML document to PDF document using ImageCollection and PdfEncoder classes.
/// </summary>
public static void ConvertHtmlToPdf_ImageCollection(string htmlFileName, string pdfFileName)
{
// create image collection
using (Vintasoft.Imaging.ImageCollection images =
new Vintasoft.Imaging.ImageCollection())
{
// layout HTML document on page area: width - 210mm, height - infinite
Vintasoft.Imaging.Codecs.Decoders.HtmlLayoutSettings layoutSettings =
new Vintasoft.Imaging.Codecs.Decoders.HtmlLayoutSettings();
layoutSettings.PageLayoutSettings.PageSize =
Vintasoft.Imaging.ImageSize.FromMillimeters(210, 0, new Vintasoft.Imaging.Resolution(96));
images.LayoutSettings.SetSettings(layoutSettings);
// add HTML document to collection
images.Add(htmlFileName);
// create PdfEncoder
using (Vintasoft.Imaging.Codecs.Encoders.PdfEncoder encoder =
new Vintasoft.Imaging.Codecs.Encoders.PdfEncoder(true))
{
// set PDF resources compression to Jpeg
encoder.Settings.Compression = Vintasoft.Imaging.Codecs.Encoders.PdfImageCompression.Jpeg;
// save images of image collection to PDF document using PdfEncoder
images.SaveSync(pdfFileName, encoder);
}
// dispose images
images.ClearAndDisposeItems();
}
}
''' <summary>
''' Converts HTML document to PDF document using ImageCollection and PdfEncoder classes.
''' </summary>
Public Shared Sub ConvertHtmlToPdf_ImageCollection(htmlFileName As String, pdfFileName As String)
' create image collection
Using images As New Vintasoft.Imaging.ImageCollection()
' layout HTML document on page area: width - 210mm, height - infinite
Dim layoutSettings As New Vintasoft.Imaging.Codecs.Decoders.HtmlLayoutSettings()
layoutSettings.PageLayoutSettings.PageSize = Vintasoft.Imaging.ImageSize.FromMillimeters(210, 0, New Vintasoft.Imaging.Resolution(96))
images.LayoutSettings.SetSettings(layoutSettings)
' add HTML document to collection
images.Add(htmlFileName)
' create PdfEncoder
Using encoder As New Vintasoft.Imaging.Codecs.Encoders.PdfEncoder(True)
' set PDF resources compression to Jpeg
encoder.Settings.Compression = Vintasoft.Imaging.Codecs.Encoders.PdfImageCompression.Jpeg
' save images of image collection to PDF document using PdfEncoder
images.SaveSync(pdfFileName, encoder)
End Using
' dispose images
images.ClearAndDisposeItems()
End Using
End Sub
Here is C#/VB.NET code that shows how to convert a HTML file to a PDF file using
DocumentConverter class:
/// <summary>
/// Converts HTML document to PDF document using Vintasoft.Imaging.DocumentConverter class.
/// </summary>
public static void ConvertHtmlToPdf_DocumentConverter(string htmlFileName, string pdfFileName)
{
// create the document convertor
Vintasoft.Imaging.DocumentConverter converter = new Vintasoft.Imaging.DocumentConverter();
// layout HTML document on page area: width - 210mm, height - infinite
Vintasoft.Imaging.Codecs.Decoders.HtmlLayoutSettings layoutSettings =
new Vintasoft.Imaging.Codecs.Decoders.HtmlLayoutSettings();
layoutSettings.PageLayoutSettings.PageSize =
Vintasoft.Imaging.ImageSize.FromMillimeters(210, 0, new Vintasoft.Imaging.Resolution(96));
converter.Images.LayoutSettings.SetSettings(layoutSettings);
// add HTML pages to the document convertor
converter.Images.Add(htmlFileName);
// convert HTML pages to TIFF images
converter.Convert(pdfFileName);
}
''' <summary>
''' Converts HTML document to PDF document using Vintasoft.Imaging.DocumentConverter class.
''' </summary>
Public Shared Sub ConvertHtmlToPdf_DocumentConverter(htmlFileName As String, pdfFileName As String)
' create the document convertor
Dim converter As New Vintasoft.Imaging.DocumentConverter()
' layout HTML document on page area: width - 210mm, height - infinite
Dim layoutSettings As New Vintasoft.Imaging.Codecs.Decoders.HtmlLayoutSettings()
layoutSettings.PageLayoutSettings.PageSize = Vintasoft.Imaging.ImageSize.FromMillimeters(210, 0, New Vintasoft.Imaging.Resolution(96))
converter.Images.LayoutSettings.SetSettings(layoutSettings)
' add HTML pages to the document convertor
converter.Images.Add(htmlFileName)
' convert HTML pages to TIFF images
converter.Convert(pdfFileName)
End Sub