News Category

Convert Webpage to PDF in WPF

2012-08-08 01:06:49 Written by  support iceblue
Rate this item
(0 votes)

HTML to PDF solution can be quite a few when people search on google. However, most solutions are not proper in use since what you view on webpage is not always completely same with what you get on your PDF. Some information such as dynamic image and anchor text in HTML are easily lost. This section will introduce a simple method to clearly convert HTML to PDF without any cut on WPF platform.

Spire.PDF for WPF, as a professional PDF component, allows you to clearly convert HTML to PDF only by three lines of key code on WPF platform. In the solution, you only need to load HTML file with four parameters:

  • URL, the url of the webpage which will be converted to PDF file.
  • Enable JavaScrpit, indicates whether enables the JavaScript of the webpage.
  • Enable hyperlink, indicates whether enables the hyperlink of the webpage.
  • Auto detect page break, indicates whether splits auto the web page in the result PDF file.

Then, save your html as PDF to file by calling the method PdfDocument.LoadFromHTM. Please preview the result screenshot of the whole project first as prove:

Webpage to PDF

Before demonstrating the code, please feel free to download Spire.PDF and install it on system. Now, you can view the full code below:

[C#]
using Spire.Pdf;

namespace WPFhtmltopdf
{
    /// 
    /// Interaction logic for MainWindow.xaml
    /// 
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }
        [STAThread]
        private void button1_Click(object sender, RoutedEventArgs e)
        {

            //Create a new pdf document.
            PdfDocument doc = new PdfDocument();
            //load the webpage 
            String url = "http://msdn.microsoft.com/";
            doc.LoadFromHTML(url, false, true, true);
            //Save html webpage as pdf file.
            doc.SaveToFile("sample.pdf");
            doc.Close();
        }
    }
}
[VB.NET]
Imports Spire.Pdf

Namespace WPFhtmltopdf
	''' 
	''' Interaction logic for MainWindow.xaml
	''' 
	Public Partial Class MainWindow
		Inherits Window
		Public Sub New()
			InitializeComponent()
		End Sub
		 _
		Private Sub button1_Click(sender As Object, e As RoutedEventArgs)

			'Create a new pdf document.
			Dim doc As New PdfDocument()
			'load the webpage 
			Dim url As  = "http://msdn.microsoft.com/"
			doc.LoadFromHTML(url, False, True, True)
			'Save html webpage as pdf file.
			doc.SaveToFile("sample.pdf")
			doc.Close()
		End Sub
	End Class
End Namespace

Obviously, using this WPF PDF component, the webpage is easily converted to PDF. Both text and images are clearly shown in PDF file. Besides, Spire.PDF for WPF is a 100% secure PDF component software. No Malware, No Spyware and No Virus.

Additional Info

  • tutorial_title: Convert Webpage to PDF in WPF
Last modified on Friday, 24 September 2021 09:55