How to Convert HTML(Webpage) to PDF with C#/VB.NET

Introduction

Because the wonderful reading effect of PDF documents, people would like to convert their electronic documents to PDF, especially articles and papers. Besides it, the other advantage of PDF document is that it can include lots of contents which can make our document be more appealed to readers, for example, comic images, audio, even video. In this post, I will introduce a method to convert HTML to PDF by using C# and VB.NET. It doesn’t means to convert a HTML file to PDF, but the contents in one webpage to a PDF document.

Background

Sometimes, we want to introduce a website to others, such as own homepage or blog in PDF. Just with a hyperlink, people will have no interest with it, while, if we present the contents in this website, people may feel better. At that time, we need to convert this website to PDF.

Following, the method I will give is very simple because I use a .NET PDF component, Spire.PDF for .NET. Therefore, before starting with coding, I add its dll file as reference in my project. After everything is prepared, we can go to steps.

Steps

  1. Create a blank PDF document
  2. Add website address and load this address in PDF document. In this step, we need to send four parameters to LoadFromHTML method, url string, bool value to enable JavaScript or not, bool value to enable hyperlinks or not, bool value to auto detect page break.
  3. Save and launch file.

Main Coding (C#):

using System;
using Spire.Pdf;

namespace HTMLtoPDF
{
    class fromhtml
    {
        [STAThread]
        static void Main(string[] args)
        {
            //Create PDF
            PdfDocument doc = new PdfDocument();

            //Load Website
            String url = http://www.google.com/”;
            doc.LoadFromHTML(url, false, true, true);

            //Save PDF
            doc.SaveToFile(“FromHTML.pdf”);
            doc.Close();

            //Launch PDF
            System.Diagnostics.Process.Start(“FromHTML.pdf”);
        }
    }
}

VB.BET:

Imports System
Imports Spire.Pdf

Namespace HTMLtoPDF
       Friend Class fromhtml
              <STAThread>
              Shared Sub Main(ByVal args() As String)

                     ‘Create PDF Document
                     Dim doc As New PdfDocument()
                     Dim page As PdfPageBase = doc.Pages.Add()
                     ‘Load Website
                     Dim url As String = http://www.google.com/&#8221;
                     doc.LoadFromHTML(url, False, True, True)

                     ‘Save PDF
                     doc.SaveToFile(“FromHTML.pdf”)
                     doc.Close()

                     ‘Launch Pdf
                     System.Diagnostics.Process.Start(“FromHTML.pdf”)
              End Sub
       End Class
End Namespace

Result Shown by Following:

Download Spire.PDF for .NET Here

Advertisements

Author: janewdaisy

.NET Program Beginner. Share methods about how to use C#/VB.NET to export data, operate Word, Excel, PDF and other useful skills.

13 thoughts on “How to Convert HTML(Webpage) to PDF with C#/VB.NET”

  1. I usually do not create many remarks, but after reading some of the remarks on How to Convert HTML(Webpage)
    to PDF with C#/VB.NET | janewdaisy. I do have 2 questions for you if it’s allright. Is it just me or does it appear like some of these remarks appear like they are coming from brain dead people? 😛 And, if you are posting on other social sites, I would like to follow everything new you have to post. Could you post a list of every one of all your shared sites like your Facebook page, twitter feed, or linkedin profile?

  2. Good day I am so grateful I found your blog, I really
    found you by mistake, while I was researching on Google for something else, Anyhow I am here now and would just like to say many
    thanks for a fantastic post and a all round thrilling blog (I also
    love the theme/design), I don’t have time to read it all at the moment but I have saved it and also included your RSS feeds, so when I have time I will be back to read more, Please do keep up the superb job.

  3. You actually make it appear really easy with your presentation however I find this topic to be really
    one thing that I believe I’d by no means understand. It seems too complex and very broad for me. I am looking forward to your subsequent put up, I will try to get the hang of it!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s