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, 06 April 2011 09:33

PDF FromHTML in C#, VB.NET

The sample demonstrates how to convert HTML to PDF document.

Download FromHTML.pdf

Published in Conversion
Tuesday, 18 January 2011 02:22

How to Convert Word to PDF

This is an all-in-one solution to convert Word to PDF. Within this solution, you can either create a word from scratch or load a word template from folder, and then edit the document as you like; finally you can effortlessly convert word to PDF in C#, VB.NET. The conversion can be doc to PDF or docx to PDF.

Spire.Doc for .NET is a professional .NET library which enables users to directly manage word editing without Microsoft Word being installed, after the editing, you can convert the document to PDF in 2 steps.

Below is the an effective screenshot of the document which will be converted to PDF, at the end Of this post, you can find the target PDF.

Word to PDF

Now feel free to download Spire.Doc for .NET and convert word to PDF for personal use or evaluation. The whole process of converting word to PDF in C#, VB.NET can be described as 2 simple steps.

1. Declare a document and load document which you prepare by using ocument.LoadFromFile() method. The parameter passed to this method is file name string.

2. Save Word document as PDF by using document.SaveToFile() method. Parameters passed to this method is file name string and file format. The file format must be PDF.

[C#]
using System;
using Spire.Doc;
using Spire.Doc.Documents;

namespace DoctoPDF
{
    class toPDF
    {
        static void Main(string[] args)
        {
            //Load Document
            Document document = new Document();
            document.LoadFromFile(@"E:\work\documents\TestSample.docx");

            //Convert Word to PDF
            document.SaveToFile("toPDF.PDF", FileFormat.PDF);

            //Launch Document
            System.Diagnostics.Process.Start("toPDF.PDF");
        }
    }
}
[VB.NET]
Imports System
Imports Spire.Doc
Imports Spire.Doc.Documents

Namespace DoctoPDF
    Friend Class toPDF
        Shared Sub Main(ByVal args() As String)
            'Load Document
            Dim document As New Document()
            document.LoadFromFile("E:\work\documents\TestSample.docx")

            'Convert Word to PDF
            document.SaveToFile("toPDF.PDF", FileFormat.PDF)

            'Launch Document
            System.Diagnostics.Process.Start("toPDF.PDF")
        End Sub
    End Class
End Namespace

After running the demo, you may find a PDF document launched on your computer:

Word to PDF

Spire.Doc is an MS Word component which enables user to perform a wide range of Word document processing tasks directly, such as generate, read, write and modify Word document for .NET and Silverlight.

More about This .NET Word component

Published in Conversion

Basic Knowledge about Office OpenXML

When talking about Office OpenXML, we may think of HTML. Actually, Office OpenXML is similar to HTML both are tag-based languages. The difference between Office OpenXML and HTML is that the tags which Office OpenXML uses are not predefined. If we want to create own tags within Office OpenXML, we need to follow a few rules.

Firstly, only one root element is contained in Office OpenXML document. The root element is often taken as document element and appears after the prolog section. Besides, all the Office OpenXML elements should contain end tags. Both start and end tag should be identical. Also, the elements can’t overlap. What’s more, all attribute values must use quotation marks and we can’t use some special characters within the text. After following the rules, the Office OpenXML document will be well formatted.

Use C# and VB.NET Convert Doc to Office OpenXML via Spire.Doc

Spire.Doc (Spire.Office) presents you an easy way to convert Doc to Office OpenXML. In this way, we can convert an exist Word doc file to Office OpenXML format with a few clicks. Now, just follow the simple steps.

Step 1: Create Project

Download Spire.Doc and install on system. Create a project through Visual Studio and add Spire.Doc DLL as reference.

Note: Please make sure Spire.Doc and Visual Studio are correctly installed on system

Step 2: Load Word Doc File

Load local Word doc file which we need to convert to Office OpenXML format. The following code can help us load it.

Document document = new Document();
document.LoadFromFile(@"D:\Sample.doc");

Step 3: Convert Doc to Office OpenXML

Spire.Doc supports convert Word Doc files to most of popular file formats such as PDF, HTML, Office OpenXML, EPub, RTF, Dot, Text, etc. Now, use the code below to convert Word to Office OpenXML.

document.SaveToFile("Sample.xml", FileFormat.Xml);

Step 4: Full Code

Now, write the full code into your project and press F5 to start the program.

[C#]
using System;
using System.Windows.Forms;
using Spire.Doc;
using Spire.Doc.Documents;

namespace to XML
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            //Create word document
            Document document = new Document();
            document.LoadFromFile(@"D:\Sample.doc");

            //Save doc file.
            document.SaveToFile("Sample.xml", FileFormat.Xml);

            //Launching the MS Word file.
            WordDocViewer("Sample.xml");
        }

        private void WordDocViewer(string fileName)
        {
            try
            {
                System.Diagnostics.Process.Start(fileName);
            }
            catch { }
        }

    }
}
[VB.NET]
Imports System
Imports System.Windows.Forms
Imports Spire.Doc
Imports Spire.Doc.Documents

Namespace to XML
	Partial Public Class Form1
		Inherits Form
		Public Sub New()
			InitializeComponent()
		End Sub

		Private Sub button1_Click(ByVal sender As Object, ByVal e As EventArgs)
			'Create word document
			Dim document As New Document()
			document.LoadFromFile("D:\Sample.doc")

			'Save doc file.
			document.SaveToFile("Sample.xml", FileFormat.Xml);

			'Launching the MS Word file.
			WordDocViewer("Sample.xml")
		End Sub

		Private Sub WordDocViewer(ByVal fileName As String)
			Try
				System.Diagnostics.Process.Start(fileName)
			Catch
			End Try
		End Sub

	End Class
End Namespace

After running the demo, you may find an Office OpenXML document launched on your browser:

Spire.Doc is an MS Word component which enables user to perform a wide range of Word document processing tasks directly, such as generate, read, write and modify Word document for .NET and Silverlight. Click to Learn more...

Published in Conversion