Tuesday, 04 March 2014 01:29

Convert HTML file to PDF and XPS in C#

HTML file is widely used to collect data and for the security reason, we need to convert it to printable documents in PDF and XPS formats. You can easily convert HTML page in URL to PDF in high quality by using Spire.PDF. This article will focus on demonstrate how to convert HTML files into the printable document, both in PDF and XPS by the help of Spire.Doc.

First, check the html file that will be converted to PDF and XPS.

Convert HTML file to PDF and XPS

Secondly, download Spire.Doc and install on your system. The Spire.Doc installation is clean, professional and wrapped up in a MSI installer.

Then adds Spire.Doc.dll as reference in the downloaded Bin folder though the below path: "..\Spire.Doc\Bin\NET4.0\ Spire.Doc.dll".

Now it comes to the steps of how to convert HTML into PDF and XPS.

Step 1: Load a HTML file from the file.

Document document = new Document();
document.LoadFromFile("Good.htm", FileFormat.Html, XHTMLValidationType.None) ;

Step 2: Save HTML to the file format in PDF and XPS.

//Save html to PDF.
document.SaveToFile("Sample.pdf", FileFormat.PDF);

//Save html to XPS.
document.SaveToFile("Sample.xps", FileFormat.XPS);

After debugging, please check the following PDF and XPS file as the result.

Convert HTML file to PDF and XPS

Convert HTML file to PDF and XPS

Full codes:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Spire.Doc;
using Spire.Doc.Documents;
using Spire.Doc.Fields;

namespace HTML2PDFXPS
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();

        }

        private void button1_Click(object sender, EventArgs e)
        {
            Document document = new Document();
            document.LoadFromFile("Good.htm", FileFormat.Html, XHTMLValidationType.None);

            //Save html to PDF.
            document.SaveToFile("Sample.pdf", FileFormat.PDF);
        }

        private void button2_Click(object sender, EventArgs e)
        {
            Document document = new Document();
            document.LoadFromFile("Good.htm", FileFormat.Html, XHTMLValidationType.None);

            //Save html to PDF.
            document.SaveToFile("Sample.xps", FileFormat.XPS);
         }
    }
}
Published in Conversion
Wednesday, 22 June 2011 08:45

How to convert HTML into PDF in C#, VB.NET

Converting HTML to PDF almost relates to every individual or group as PDF is the best format for safely distribution or sharing. Then how should we convert html to PDF easily and safely becomes very important. Online html to PDF converter is truly convenient but may lead to reveal of privacy, which could be fatal, especially for cooperation, company and organization. A sound solution is that we can customize html to PDF by ourselves. This section shows a direct example for you to manage html to PDF by yourself in C# and VB.NET.

By using Spire.PDF, a professional PDF component, you can convert files of HTML to PDF format under .NET, WPF and Silverlight platforms. You can realize your requirement of converting HTML to PDF in several lines of code.

Here comes to the details:

  • Download Spire.PDF for .NET (or Spire.Office for .NET) and install it on your system.
  • Add Spire.PDF.dll as reference in the downloaded Bin folder thought the below path: "..\Spire.PDF\Bin\NET4.0\ Spire.PDF.dll".
  • Check the codes as below in C# and VB.NET:
[C#]
static void Main(string[] args)
{
 //Create a pdf document.
 PdfDocument doc = new PdfDocument();
 String url = "http://www.london2012.com/news/articles/paralympic-torch-relay-route-revealed-1258473.html";
 Thread thread = new Thread(() =>
 { doc.LoadFromHTML(url, false, true, true);});
 thread.SetApartmentState(ApartmentState.STA);
 thread.Start();
 thread.Join();
 //Save pdf file.
 doc.SaveToFile("sample.pdf");
 doc.Close();
 //Launching the Pdf file.
 System.Diagnostics.Process.Start("sample.pdf");
}
[VB.NET]
 Sub Main()

        Dim thread As Thread = New Thread(AddressOf LoadHtml)
        thread.SetApartmentState(ApartmentState.STA)
        thread.Start()
        thread.Join()

    End Sub
    Private Sub LoadHtml()
        Dim path As String = "sample.pdf"
        Dim pdfDoc As PdfDocument = New PdfDocument()
        Dim url As String = "http://www.london2012.com/news/articles/paralympic-torch-relay-route-revealed-1258473.html"
        pdfDoc.LoadFromHTML(url, False, True, True)
        pdfDoc.SaveToFile(path, FileFormat.PDF)
        pdfDoc.Close()
        pdfDoc.Dispose()
    End Sub

Effective Screenshot:

HTML TO PDF c#/vb.net

Published in Conversion
Monday, 30 May 2011 06:49

How to Convert Image to PDF

Spire.PDFConverter is a powerful PDF converting tool which enables to convert files from Doc to PDF, HTML to PDF, Image to PDF, Excel to PDF and Text to PDF. This article will show you how to use Spire PDF Converter to convert file from Image to PDF.

How to Convert Image to PDF?

Download Free Spire.PDFConverter, Only a few simple steps, you can finish a whole process of Image to PDF conversion by using Spire PDF Converter.

Step 1: Install and Run Spire PDF Converter

Step 2: Choose Image files

Click "Add File" button to select Image files which you want to convert to PDF format.

Step 3: Choose output file folder

Click "browse" button to select file folder where you want to place the output PDF files. Spire PDF Converter supports encrypt out PDF files and enables add watermark (Text/Image watermark).

Step 4: Run Image to PDF conversion

Click "Convert" button to start converting process.

Published in Spire.PDFConverter