免费强大的API来处理Word和PDF文件 - Spire.Doc和Spire.PDF
E-iceblue Co., Ltd. 是一家提供.NET, Silverlight和WPF开发组件的供应商。E-iceblue 的目标始终是提供高质量的组件来读取和写入不同格式的Office文件。
引言
开发人员经常需要处理Word文件和PDF文件,逐个处理这些操作任务非常耗时。免费的Spire.Doc和Spire.PDF是两个强大的.NET组件,用于在C#和VB.NET中操作Word文档和PDF文件。它们易于集成到您的应用程序中,并且与其他第三方组件配合良好。在本文中,我将演示如何一起使用免费的Spire.Doc和Spire.PDF DLL。此演示将展示如何使用邮件合并功能创建精美且多元素的Word文档,然后我们将把该Word文档转换为PDF文件。最后,我们将通过创建可见的数字签名来保护目标PDF文档。
应用程序概述
该应用程序实现的功能如下:
- 签名PDF
- 邮件合并
- 转换为PDF文档
- 加载Word模板
邮件合并是一项强大的功能,已广泛用于开发人员创建多个相似文档。使用它,开发人员可以用自定义数据填充Word模板。免费的Spire.Doc提供了多种邮件合并方式;请查看下方的主要方法:
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)
...等等。
我们在应用程序中使用方法public void Execute(string[] fieldNames, string[] fieldValues)
来创建Word文档。使用Spire.Doc,数据可以来自.NET支持的各种格式。它可以是DataTable
, DataView
, DataSet
, IDataReader
或值数组。如果您的数据是XML格式,则可以将其加载到DataSet
中并与DataSet
合并。您可以在E-iceblue的教程中找到更多关于使用Spire.Doc进行 邮件合并的详细信息。
在成功创建Word文档后,我们将将其转换为PDF文件。将Word转换为PDF是Free Spire.Doc提供的最重要的功能之一。只需一行核心代码即可将Word文档转换为PDF文件。
获取结果PDF文件后,我们将使用Free Spire.PDF 对PDF文档进行签名以保护它。Free Spire.PDF支持创建、写入、编辑、处理和读取PDF文件。它还提供安全设置,包括数字签名、PDF文本/附件/图像提取、PDF合并/拆分、元数据更新、章节和段落优化、图形/图像绘制和插入、表格创建和处理以及导入数据。
请查看下面的示例代码
- 加载Word模板
此应用程序使用Word模板创建多元素文档。以下是用于邮件合并的Word模板。
加载用于邮件合并的模板。
Document document = new Document(); document.LoadFromFile("template.docx");
合并数据存储在string []
中。模板文件中包含三个段落。用于填充这些段落的数据存储在文本文件content.txt中。从content.txt获取文本并将其分配给“Paras
”。您可以更改数据以使用其他自定义数据来创建您想要的Word文档。
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] };
- 邮件合并
在此应用程序中,合并数据通过两种方法获取:一种来自字符串数组,另一种(用于填充段落字段)来自TXT文件。为此,您需要从TXT文件中获取文本并将其分配给一个数组。您也可以根据需要进行更改。代码如下:
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] };
将模板Word文档与自定义数据合并。
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);
邮件合并后,这是生成的Word文件
- 转换为PDF文档
Spire.Doc支持许多转换,例如将Word Doc/Docx转换为流、另存为Web响应,以及将Word Doc/Docx转换为PDF、Image、XML、RTF、EMF、TXT、XPS、EPUB、HTML,反之亦然。Spire.Doc还支持将HTML转换为图像的功能。在此应用程序中,我们使用SaveToFile
方法直接将Word文档转换为PDF文件。
document.SaveToFile("result_1.pdf",Spire.Doc.FileFormat.PDF);
- 签名PDF
最后,此应用程序使用Spire.PDF通过可见的数字签名来签名PDF文档。Spire.PDF还支持PDF文档的加密和解密,或者允许将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;
最终的PDF文档
完整的应用程序代码
以下是应用程序的完整代码,但在代码运行之前,请在此处下载doc DLL和在此处下载PDF DLL,并将引用添加到Spire.Doc.dll和Spire.Pdf.dll。正如您所见,DLL完成了大量工作。
// 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();
摘要
在本文中,我们使用Spire.Doc和Spire.PDF的免费版本来创建Word文档,将其转换为PDF文档,并使用可见的数字签名对PDF文档进行签名。商业版本功能更强大,可以满足您在处理Word文档和PDF文档的各个方面的需求。您可以自由评估商业版本。