Winnovative Software Logo

 HTML to PDF Converter - Excel Library - ASP.NET Charts - RTF to PDF Converter
 PDF Merge and Split - PDF Security - PDF Viewers - PDF to Text - Images Extractor

 
Skip Navigation Links
 

Getting Started with the Winnovative HTML to PDF Converter for .NET

 

Overview

 
The HTML to PDF Converter for .NET can be used in any type of application to generate printable PDF documents and images from any web page. To see the capabilities of this product take a look at the quick drive test from home page.

THe HTML to PDF Converter for .NET is a .NET library 'wnvhtmlconvert.dll' that can be used in any type of .NET application (e.g web sites, windows forms applications, console applications).

The HTML to PDF COnverter library defines two classes PdfConverter and ImgConverter that can be used to render the web page from a given URL either as a PDF document or as an image.
 

Using the Winnovative HTML to PDF Converter Library for .NET

 
To use the HTML to PDF Converter in Windows Forms and Console applications, all you have to do is to add a reference the wnvhtmlconvert.dll assembly and call the render methods from your code after you have set the render properties.

Below is a code example of setting the converter library properties and then calling the render methods to generate either an image or a PDF document. The code is taken from the windows forms application sample provided with the HTML to PDF Converter. In the event handler of a button, the application creates either a PdfConverter or an ImgConverter object and calls
        private PdfConverter GetPDFConverter()
        {
            int pageWidth = 0;
            int pageHeight = 0;

            // set common properties
            if (!radioButtonAutodetect.Checked)
            {
                pageWidth = int.Parse(textBoxWebPageWidth.Text.Trim());
                pageHeight = int.Parse(textBoxWebPageHeight.Text.Trim());
            }

            // create the PDF converter
            PdfConverter pdfConverter = new PdfConverter(pageWidth, pageHeight);

            // set PDF options
            pdfConverter.PdfDocumentOptions.PdfPageSize = 
               (PdfPageSize)Enum.Parse(typeof(PdfPageSize),
               ddlPageFormat.SelectedItem.ToString());
            pdfConverter.PdfDocumentOptions.PdfCompressionLevel = (PdfCompressionLevel)
               Enum.Parse(typeof(PdfCompressionLevel),
               ddlCompression.SelectedItem.ToString());
            pdfConverter.PdfDocumentOptions.ShowHeader = cbShowHeader.Checked;
            pdfConverter.PdfDocumentOptions.ShowFooter = cbShowFooter.Checked;
            pdfConverter.PdfDocumentOptions.LeftMargin = int.Parse(
                           textBoxLeftMargin.Text.Trim());
            pdfConverter.PdfDocumentOptions.RightMargin = int.Parse(
                           textBoxRightMargin.Text.Trim());
            pdfConverter.PdfDocumentOptions.TopMargin = int.Parse(
                           textBoxTopMargin.Text.Trim());
            pdfConverter.PdfDocumentOptions.BottomMargin = int.Parse(
                           textBoxBottomMargin.Text.Trim());
            pdfConverter.PdfDocumentOptions.GenerateSelectablePdf = cbUseMetafileFormat.Checked;

            pdfConverter.PdfHeaderOptions.HeaderText = textBoxHeaderTitle.Text;
            pdfConverter.PdfHeaderOptions.HeaderTextColor = Color.FromKnownColor(
               (KnownColor)Enum.Parse(typeof(KnownColor), 
               ddlHeaderTextColor.SelectedItem.ToString()));
            pdfConverter.PdfHeaderOptions.HeaderSubtitleText = textBoxHeadeSubtitle.Text;
            pdfConverter.PdfHeaderOptions.DrawHeaderLine = cbDrawHeaderLine.Checked;

            pdfConverter.PdfFooterOptions.FooterText = textBoxFooterText.Text;
            pdfConverter.PdfFooterOptions.FooterTextColor = Color.FromKnownColor(
               (KnownColor)Enum.Parse(typeof(KnownColor), 
               ddlFooterColor.SelectedItem.ToString()));
            pdfConverter.PdfFooterOptions.DrawFooterLine = cbDrawFooterLine.Checked;
            pdfConverter.PdfFooterOptions.PageNumberText = textBoxPageNumberText.Text;
            pdfConverter.PdfFooterOptions.ShowPageNumber = cbShowPageNumber.Checked;

            return pdfConverter;
        }

        private ImgConverter GetImgConverter()
        {
            int pageWidth = 0;
            int pageHeight = 0;

            // set common properties
            if (!radioButtonAutodetect.Checked)
            {
                pageWidth = int.Parse(textBoxWebPageWidth.Text.Trim());
                pageHeight = int.Parse(textBoxWebPageHeight.Text.Trim());
            }

            // create the PDF converter
            ImgConverter imgConverter = new ImgConverter(pageWidth, pageHeight);

            return imgConverter;
        }

        private void btnConvert_Click(object sender, EventArgs e)
        {
            if (!PageIsValid())
                return;

            string outFile = null;
            if (radioConvertToPDF.Checked)
            {
                PdfConverter pdfConverter = GetPDFConverter();
                outFile = Path.Combine(Application.StartupPath, "RenderedPage.pdf");
                try
                {
                    pdfConverter.SavePdfFromUrlToFile(textBoxWebPageURL.Text, outFile);
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                    return;
                }
            }
            else
            {
                ImgConverter imgConverter = GetImgConverter();
                outFile = Path.Combine(Application.StartupPath, String.Format(
                          "RenderedPage.{0}", ddlImageFormat.SelectedItem.ToString()));
                try
                {
                    RenderImageFormat renderImageFormat = (RenderImageFormat)Enum.Parse
                              (typeof(RenderImageFormat), 
                               ddlImageFormat.SelectedItem.ToString());
                    imgConverter.SaveImageFromUrlToFile(
                            textBoxWebPageURL.Text,
                            RenderImageFormatConverter.GetImageFormat(renderImageFormat), 
                            outFile
                         );
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                    return;
                }
            }

            DialogResult dr = MessageBox.Show("Open the rendered file in an external
                viewer?", "Open Rendered File", MessageBoxButtons.YesNo);
            if (dr == DialogResult.Yes)
            {
                try
                {
                    System.Diagnostics.Process.Start(outFile);
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                    return;
                }
            }
        }

 

Licensing

 
After purchase you will receive by a license key string that you'll use to set the LicenseKey property of the PdfConverter object.