Wednesday, 18 May 2011 09:59

How to Convert HTML to PDF

Why Convert HTML to PDF?

PDF (Portable Document Format), which often used for printing document is an open standard for creating and sharing document. It is one of the most popular file formats because it retains the same format no matter what browser is used to view it and without any loss when transfer from one system to another. So, we usually convert different format files such as Doc, Text, CSV, XML and HTML to PDF. Here is one of the easiest and fastest solutions.

How to Convert HTML to PDF?

Only 3 simple steps, you can finish a whole process of HTML to PDF conversion by using Spire.PDFConverter.

Step 1: Install and Run Spire PDF Converter

Step 2: Add HTML files

Spire.PDFConverter enables user add single HTML file or multiple HTML files. And support add file folder.

Step 3: Choose output file folder and Run conversion process

Spire.PDFConverter is one of the fastest HTML to PDF converting solutions. By using Spire PDF Converter, you can encrypt or add watermark to output PDF files.

Monday, 16 May 2011 10:04

How to Convert Doc to PDF

Why Convert Doc to PDF?

PDF documents are not good for designing and editing content but without any information and appearance loss, they are very safe and easy to transfer from one system to another. On the other hand, Microsoft Word document (.doc file) is the most suitable document for designing and editing. So, the most popular way to create PDF documents is to design and create content in MS Word document and then convert the file from Doc to PDF.

How to Convert Doc to PDF?

Only 3 simple steps, you can finish a whole process of Doc to PDF conversion by using Spire.PDFConverter. Download Free Spire.PDFConverter Here

Step 1: Install and Run Spire.PDFConverter

Step 2: Choose Doc file

Click button "Add files" to select Doc files which you want to convert. Spire.PDFConverter supports batch conversion which means you can add multiple Doc files.

Step 3: Start Converting

Choose output file folder and click button "Convert"

Friday, 13 May 2011 10:24

How to Use Spire.PDFConverter

Spire.PDFConverter is a PDF conversion program which can convert office files such as Doc, Excel, HTML, Text and XML to PDF format. Below is a simple instruction of how to use Spire.PDFConverter.

Step 1: Download Spire.PDFConverter here and install.

Step 2: Run Spire.PDFConverter.

Step 3: Add Files to Converter.

Spire.PDFConverter allows user to add single file, multiple files, whole file folder into conversion list. Spire PDF Converter offers powerful functions which enable uses to add watermark and encrypt output PDF document.

Step 4: Start conversion process.

Spire.PDFConverter enables user to set output address for output files. And once the conversion process finished, user can click "Open Folder" button to check the status.

Wednesday, 13 April 2011 03:21

How to Use Mail Merge to Create Report

Introduction

Mail merge is often used to print reports in bulk, such as financial statement, payroll or transcript. And the merged documents can be sent by E-mail.

In this article, I will show one way to generate mail merged reports via Spire.Doc.

Report Overview

This report includes multiple invoices, and each invoice starts in a new page. Invoice logo and supplier information will present in the header of every page.

Order, shipment, customer, order details and total price make up a completed invoice.

The following pictures show the appearance of invoice:

Mail Merge Report

Content details in each invoice are shown as following:

Mail Merge Report

Order Data Overview

All data in this example is from Northwind database, which is a sample database provided by Microsoft Access 2003.

We will export data from table Orders, Shippers, Customers, Employees, [Order Details] and Products to generate our report. The following picture shows the relationship between the 6 tables.

Mail Merge Report

Steps

We need to finish the following 3 steps to generate our report.

  • Create a mail merge template.
  • Load data from database.
  • Merge data into template and save.

Every step includes several sub-steps and in #2 and #3 we need to write some code.

Create mail merge template

A template is a reusable document. It renders the pattern of our report. We could modify it to change our report without any modification of code.

Note: in this section, all tables mean DataTable instance, not physical table in database.

We can create the template in MS Word or by other program. Please see the following picture. It is the template we need to create. Data will be filled in the red party.

Mail Merge Report

Insert mail-merge-field as placeholder into the red-block. There are three types of mail-merge-field which will be used in this example:

GeneralField is a general Word mail-merge-field. It is real data field and our data will be filled in it during merge process. We need to insert a GeneralField to every red-block and name these fields with the corresponding data name. After inserting GeneralFields, our template will looks like:

Mail Merge Report

TableField is assistant mail-merge-field and used as a container of multiple related GeneralFields and other TableFields. So it is not data placeholder and no data will be filled in. It is composed of two special mail-merge-fields:

TableStart:TableName and TableEnd:TableName. During merge process, the data of related GeneralFields contained by one same TableField will be from one same data table. For example, fields in Customer information block will be filled with data from data table Customer, so we need to put them in TableField Customer.

Insert a mail-merge-field with field name TableStart:Customer immediately before the first CompanyName field and insert another mail-merge-field with field name TableEnd:Customer immediately after the field Country. And then our fields in Customer information block looks like:

Mail Merge Report

During the merge process, data in column CompanyName of table Customer will be filled in the field CompanyName, Customer.Address to field Address, Customer.City to field City and so on.

Data of fields in column Salesperson in Order information table is from table Employee

Mail Merge Report

Data of fields in column Ship Via in Order information table is from table Shipper

Mail Merge Report

Data of fields in Order details table is from table Detail, except field ProductName. Data of field ProductName is from table Product. Data of field InvoiceSubtotal and InvoiceTotal in Invoice total information is from table Total (virtual table)

Mail Merge Report

GroupField is assistant mail-merge-field too. It can contain multiple related GeneralFields and TableFields. It is composed of two special mail-merge-fields: GroupStart:GroupName and GroupEnd:GroupName. During merge process, all Word document elements included in a GroupField will be copied. One row in data table has one copy and data in the row will be filled into the fields in the copy.

If the row has sub data table, the data in sub data table will be filled into the fields included in the corresponding TableField. If the sub data table has multiple data rows, the corresponding TableField will be copied and filled too.

We need to insert a mail-merge-field named GroupStart:Order in the top of the template body and insert a mail-merge-field named GroupEnd:Order in the bottom of the template body. After this, our template looks like:

Mail Merge Report

You could find the complete template named InvoiceTemplate.doc in the attached source package.

Load Data from Database

Spire.Doc provides merge data from DataSet. So we will use DataAdapter to fill data table from NorthWind database to a DataSet and merge it into our template. Difference from DataRelation of DataSet, Spire.Doc has owned table relation functionality. So we don't need to create DataRelation instance for the DataSet object. The code below just shows load Order data. Please see the attached source package for other code.

[C#]
String connectionString
    = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=Northwind.mdb";
DataSet dataSet = new DataSet();
using(OleDbConnection conn = new OleDbConnection(connectionString))
{
    //load December 1997 orders
    String sql
        = " SELECT * "
        + " FROM   Orders "
        + " WHERE  ShippedDate Between #12/1/1997# And #12/31/1997# ";
    using (OleDbDataAdapter dataAdapter = new OleDbDataAdapter(sql, conn))
    {
        dataAdapter.Fill(dataSet, "Order");
    }
}

Merge data into template and save

In this section, we need to write some code to call Spire.Doc to merge our data table and template.

Create Spire.Doc.Document object and load template.

[C#]
Document document = new Document();
document.LoadFromFile("InvoiceTemplate.doc", FileFormat.Doc);

Establish relationship between data tables.

[C#]
List<DictionaryEntry> list = new List<DictionaryEntry>
{
    new DictionaryEntry("Order", String.Empty),
    new DictionaryEntry("Shipper", "ShipperID = %Order.ShipVia%"),
    new DictionaryEntry("Customer", "CustomerID = %Order.CustomerID%"),
    new DictionaryEntry("Employee", "EmployeeID = %Order.EmployeeID%"),
    new DictionaryEntry("Detail", "OrderID = %Order.OrderID%"),
    new DictionaryEntry("Product", "ProductID = %Detail.ProductID%"),
    new DictionaryEntry("Total", "OrderID = %Order.OrderID%")
};

Merge data set into template and save document to file.

[C#]
//clear empty value fields during merge process
document.MailMerge.ClearFields = true;

//clear empty paragraphs if it has only empty value fields.
document.MailMerge.RemoveEmptyParagraphs = true;

//merge
document.MailMerge.ExecuteWidthNestedRegion(dataSet, list);

//set word view type.
document.ViewSetup.DocumentViewType = DocumentViewType.PrintLayout;
document.SaveToFile("Invoice.doc");

In order to start each invoice in a new page, we insert a page-break-symbol immediately before the first paragraph when a new order row will be merged. To do this, we need to handle the event MergeField which is fired before a field merged.

[C#]
//index of row of merged order data
int mergedRowIndex = 0;
document.MailMerge.MergeField += delegate(object sender, MergeFieldEventArgs e)
{
    if (e.TableName == "Order")
    {
        if (e.RowIndex > mergedRowIndex)
        {
            mergedRowIndex = e.RowIndex;
            
            //insert page break symbol before the paragraph of current field
            InsertPageBreak(e.CurrentMergeField);
        }
    }
};

Code of method InsertPageBreak

[C#]
private static void InsertPageBreak(IMergeField field)
{
    //append a page break symbol
    Break pageBreak = field.OwnerParagraph.AppendBreak(BreakType.PageBreak);

    //move to the start of the paragraph
    field.OwnerParagraph.Items.Insert(0, pageBreak);
}

Download Mail_Merge_Report_Src.zip

Download Mail_Merge_Report_Bin.zip

Test Scenario

  • Time cost when Export an Word/Excel file
  • Time cost when batch Export Word/Excel files
  • Output File Size

Test Environment

  • O/S: Microsoft Windows XP Service Pack 3
  • CPU: Intel Celeron E3300
  • Mainboard: ECS G31T-M9
  • RAM: Geil CL5-5-5 DDR2 800 2GB
  • Graphics: Intel Chipset – Integrated Graphics 0[ECS EliteGroup Computer]
  • HDD: Seagate ST3500418AS
  • Office: Office Automation via Office 2003 and Spire.Office for .NET
  • Test Data Source Files Info:
    Rows Columns
    File 1 20 5
    File 2 55 15
    File 3 43 7
    File 4 60 8
    File 5 5000 7

Test Result

Export Data to Word

When export a single Word file document, Office OLE Automation is much slower than Spire.Doc. From the result above we can see that Spire.Doc only need about 20ms and Office need at least add 1000ms.

Batch Export Word Files

When batch export 10 Word file documents, Spire.Doc need some more time but it is still much faster.

Export Single Word File Output Size

The exported output Word document file size created by Spire.Doc is much smaller than which created by Office OLE Automation.

Export Data to Excel

Export an Excel File

When export a single Excel file, Spire.XLS is faster than Office OLE Automation. We tested 9 times and the average time (Spire.XLS,1233ms; Office,1633ms) shows Spire.XLS is about 1/3 faster.

Batch Export Excel Files (10 Files)

Batch export 10 Excel files and from the Chart above we can see Spire.XLS and Office OLE Automation spend almost the same time.

PS: Office OLE Automation Excel will ask you whether overlap the same name files or not, which may reduce export speed. Spire.XLS for .NET enables you set up to ignore this hint.

Export Single Excel File Output Size

After exporting data to Excel, we can see the output file size from the above chart for both Office and Spire.XLS. The result shows it's really a small difference between them.

PS: When we test large content data on exporting through Office OLE automation, the test PC would stop working. It took all of the CPU usage and need more than 10 minutes to return normal. Furthermore, it stored the Excel/Word process in RAM which needs manual operation or program to finish. That's really a trouble for non-professional.

Download Comparion Details.xls

Download Source Code

Wednesday, 06 April 2011 09:53

PDF Split in C#, VB.NET

The sample demonstrates how to split one PDF document to multiple PDF documents.

(NO screenshot)

Wednesday, 06 April 2011 09:50

PDF Merge in C#, VB.NET

The sample demonstrates how to merge multiple PDF documents to one PDF document.

(NO screenshot)

Wednesday, 06 April 2011 09:47

PDF Extract in C#, VB.NET

The sample demonstrates how to extract images and text from PDF document.

(NO screenshot)

Wednesday, 06 April 2011 09:44

PDF Booklet in C#, VB.NET

The sample demonstrates how to convert a PDF document to a booklet.

Download Booklet.pdf

Wednesday, 06 April 2011 09:41

PDF ViewPreference in C#, VB.NET

The sample demonstrates how to set PDF document view preference.

Download ViewerPreference.pdf

Page 6 of 22