Click here to Skip to main content
Click here to Skip to main content

Free powerful API to process Word and PDF file-Spire.Doc and Spire.PDF

, 2 Sep 2014 CPOL
E-iceblue Co., Ltd. is a vendor of .NET, Silverlight and WPF development components. The goal of e-iceblue is always to offer high-quality components for reading and writing different formats of office files.

Editorial Note

This article is in the Product Showcase section for our sponsors at CodeProject. These reviews are intended to provide you with information on products and services that we consider useful and of value to developers.

Introduction

Developers need to work with Word files and PDF files often and it is time consuming to deal with these operation tasks one by one. Free Spire.Doc and Free Spire.PDF, two powerful .NET components, are used to operate word documents and PDF files in C# and VB.NET. It is easy to integrate with your applications and they work well with any other third party components. In this article, I will present a demo by using free Spire.Doc and Spire.PDF DLLs together. This demo will show you how to create beautiful and multi-element word documents using the mail merge function, then we will convert this word document to a PDF file. Finally, we will protect the target PDF document by creating a visible digital signature in PDF.

Application Overview

Here is what this application fulfills:

  • Sign the PDF
  • Mail Merge
  • Convert to PDF document
  • Load Word template

Mail merge is a powerful feature and it has been widely used for developers to create multiple similar documents. By using it, developers can fill the word template with customized data. Free Spire.Doc provides many ways to do a mail merge; check the main methods as below:

public void Execute(DataTable table);
public void Execute(OleDbDataReader dataReader);
public void Execute(string[] fieldNames, string[] fieldValues);
public void ExecuteWidthNestedRegion(DbConnection conn, List<DictionaryEntry> commands)

... and so on.

We use the method public void Execute(string[] fieldNames, string[] fieldValues) to create a word document in this application. Using Spire.Doc, data can be used from a variety of sources in formats supported by .NET. It can be DataTable, DataView, DataSet, IDataReader or an array of values. If you have your data in XML format, then you can load it into a DataSet and merge with the DataSet. You can see more details on mail merge using Spire.Doc in E-iceblue’s tutorials.

After we create the word document successfully, then we will convert it to a PDF file. Converting word to PDF is one of the most important features offered by Free Spire.Doc. It only needs one core line to convert word documents to a PDF file.

When we get the resulting PDF file, we will then use Free Spire.PDF to sign the PDF document to protect it. Free Spire.PDF supports creating, writing, editing, handling and reading PDF files. It also provides security settings including digital signature, PDF text/attachment/image extract, PDF merge/split, metadata update, section and paragraph optimizing, graph/image drawing and inserting, table creation and processing, and importing data.

Check the Sample Code as below:

  • Load Word template

This application uses a Word template to create multi-element document. Here is the Word template used for mail merge.

Load the template that is used in mail merge.

Document document = new Document();
document.LoadFromFile("template.docx");

The merge data is stored in string []. There are three paragraphs in the template file. The data to fulfill these paragraphs is stored in txt file content.txt. Get the text form content.txt and assign them to "Paras." You can change the data to other customized data to create the Word document that you want.

string[] Paras = File.ReadAllLines("content.txt");
string[] filedValues = new string[] { ".NET", "WPF", "Silverlight", "Spire.Office", "Spire.Doc", "Spire.XLS", "Spire.PDF", "Version Available", "Yes", Paras[0], Paras[1], Paras[2] };
  • Mail merge

In this application, the merge data is acquired through two methods: one is from String Array, the other (to fulfill these paragraph fields) is from a TXT file. For this, you need to get the text from the TXT file and assign them to an array. You may change according to your needs as well. The code is as follows:

string[] Paras = File.ReadAllLines("content.txt");
string[] filedValues = new string[] { ".NET", "WPF", "Silverlight", "Spire.Office", "Spire.Doc", "Spire.XLS","Spire.PDF", "Version Available", "Yes", Paras[0], Paras[1], Paras[2] };

Merge the template word document with customized data.

string[] Paras = File.ReadAllLines("content.txt");
string[] filedNames = new string[] { "netField", "wpfField", "silverlightField", "officeField", "docField", "xlsField","pdfField", "ver", "exist", "Para1", "Para2", "Para3" };
string[] filedValues = new string[] { ".NET", "WPF", "Silverlight", "Spire.Office", "Spire.Doc", "Spire.XLS","Spire.PDF", "Version Available", "Yes", Paras[0], Paras[1], Paras[2] };
document.MailMerge.Execute(filedNames, filedValues);

After the mail merge, here is the generated Word file:

  • Convert to PDF document

Spire.Doc supports many conversions, such as Word Doc/Docx to stream, save as web response, and convert Word Doc/Docx to PDF, Image, XML, RTF, EMF, TXT, XPS, EPUB, HTML and vice versa. Spire.Doc also supports the ability to convert HTML to an image. In this application, we use the method SaveToFile to convert a word document to PDF file directly.

document.SaveToFile("result_1.pdf",Spire.Doc.FileFormat.PDF);
  • Sign the PDF

Finally, this application uses Spire.PDF to sign the PDF document with visible digital signature. Spire.PDF also supports the encryption and decryption of PDF documents, or allows PDF documents to be set as read-only.

PdfCertificate cert = new PdfCertificate("Demo.pfx", "e-iceblue");
var signature = new PdfSignature(pdfDocument, pdfDocument.Pages[0], cert, "Requestd1");
signature.Bounds = new RectangleF(new PointF(250, 600), new SizeF(260, 90));
signature.IsTag = true;
signature.DigitalSignerLable = "Digitally signed by ";
signature.DigitalSigner = "Harry Hu";
signature.DistinguishedName = "DN:";
signature.LocationInfoLabel = "Location:";
signature.LocationInfo = "London";
signature.ReasonLabel = "Reason: ";
signature.Reason = "Le document est certifie";
signature.DateLabel = "Date: ";
signature.Date = DateTime.Now;
signature.ContactInfoLabel = "Contact: ";
signature.ContactInfo = "123456789";
signature.Certificated = false;
signature.ConfigGraphicType = ConfiguerGraphicType.Picture;
signature.ConfiguerGraphicPath = "signature.png";
signature.DocumentPermissions = PdfCertificationFlags.ForbidChanges;

The final PDF document:

Complete Application Code

Below is the complete code of the application, but before the code can run, please download the doc DLL here and PDF DLL here, and add references to Spire.Doc.dll and Spire.Pdf.dll. As you can, the DLLS do a lot of work.

// Part A

Document document = new Document();
document.LoadFromFile("template.docx");

string[] Paras = File.ReadAllLines("content.txt");
string[] filedNames = new string[] { "netField", "wpfField", "silverlightField", "officeField", "docField", "xlsField", "pdfField", "ver", "exist", "Para1", "Para2", "Para3" };
string[] filedValues = new string[] { ".NET", "WPF", "Silverlight", "Spire.Office", "Spire.Doc", "Spire.XLS", "Spire.PDF", "Version Available", "Yes", Paras[0], Paras[1], Paras[2] };
document.MailMerge.Execute(filedNames, filedValues);

document.SaveToFile("sample.docx", Spire.Doc.FileFormat.Docx2010);
document.SaveToFile("result_1.pdf", Spire.Doc.FileFormat.PDF);
document.Close();


// Part B

PdfDocument pdfDocument = new PdfDocument();
pdfDocument.LoadFromFile("result_1.pdf");

PdfCertificate cert = new PdfCertificate("Demo.pfx", "e-iceblue");
var signature = new PdfSignature(pdfDocument, pdfDocument.Pages[0], cert, "Requestd1");
signature.Bounds = new RectangleF(new PointF(250, 600), new SizeF(260, 90));
signature.IsTag = true;
signature.DigitalSignerLable = "Digitally signed by ";
signature.DigitalSigner = "Harry Hu";
signature.DistinguishedName = "DN:";
signature.LocationInfoLabel = "Location:";
signature.LocationInfo = "London";
signature.ReasonLabel = "Reason: ";
signature.Reason = "Le document est certifie";
signature.DateLabel = "Date: ";
signature.Date = DateTime.Now;
signature.ContactInfoLabel = "Contact: ";
signature.ContactInfo = "123456789";
signature.Certificated = false;
signature.ConfigGraphicType = ConfiguerGraphicType.Picture;
signature.ConfiguerGraphicPath = "signature.png";
signature.DocumentPermissions = PdfCertificationFlags.ForbidChanges;

pdfDocument.SaveToFile("result_2.pdf");
pdfDocument.Close();

Summary

In the article, we use the FREE versions of Spire.Doc and Spire.PDF to create a word document, convert to a PDF document, and sign the PDF document with a visible digital signature. The commercial editions are much more powerful and can meet your needs in every aspect of processing Word documents and PDF documents. You are free to evaluate the commercial editions.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

E-iceblue

United States United States
E-iceblue Co., Ltd. is a vendor of .NET, Silverlight and WPF development components. The goal of e-iceblue is always to offer high-quality components for reading and writing different formats of office files.
 
Our components have been widely-used by most of the Fortune 500 corporations. The key developers of e-iceblue have over 10 years of combined experience developing high-performance, high-quality .NET, Silverlight and WPF component technology.
 
Everyday, e-iceblue products help a large number of developers from large/small companies in more than sixty countries to easier, better, faster and to be more productive develop and deliver reliable applications to their customers.

Comments and Discussions

 
GeneralThanks Pinmemberjimbradyis3-Sep-14 7:52 
General[My vote of 1] This is an advertisement, not an article PinmemberLLKrisJ2-Sep-14 12:08 
GeneralRe: [My vote of 1] This is an advertisement, not an article PinmemberE-iceblue2-Sep-14 16:39 
GeneralRe: [My vote of 1] This is an advertisement, not an article PinmemberMember 105397421-Oct-14 3:21 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.141216.1 | Last Updated 2 Sep 2014
Article Copyright 2014 by E-iceblue
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid