1200字范文,内容丰富有趣,写作的好帮手!
1200字范文 > C# 创建 Word 并另存为PDF格式

C# 创建 Word 并另存为PDF格式

时间:2022-11-04 09:50:08

相关推荐

C# 创建 Word 并另存为PDF格式

简介

软件开发人员在开发应用程序时,常需要操作Word和PDF文档。在本文中,我将从免费Spire.Office里添加Spire.doc.dll和Spire.PDF.dll来演示如何使用邮件合并创建漂亮的Word文档,然后将这个Word文档另存为PDF文件格式,并为生成的PDF文件添加可见的数字签名来达到保护文档的目的。

步骤:创建word模板---邮件合并文本和图片---word保存为PDF--添加PDF数字签名

创建Word邮件合并模板

首先我们需要制作一个主文档,主文档是指在Word的邮件合并功能中,所含文本和图片对合并文档的每个版本都相同的文档,例如,信息库中的姓名,电话等。

//创建Document实例Document document = new Document();//添加节Section section = document.AddSection();//添加段落Paragraph paragraph = section.AddParagraph();//添加文本paragraph.AppendText("\n姓名:");//添加合并域“Name”paragraph.AppendField("Name",FieldType.FieldMergeField);//添加文本paragraph.AppendText("\n电话:");//添加合并域"Phone"paragraph.AppendField("Phone",FieldType.FieldMergeField);//添加文本paragraph.AppendText("\n部门:");//添加合并域"Department"paragraph.AppendField("Department",FieldType.FieldMergeField);//添加文本paragraph.AppendText("\n照片:");//添加合并域"Image:Photo"paragraph.AppendField("Image:Photo",FieldType.FieldMergeField);//保存模板文档document.SaveToFile("Template.docx",FileFormat.Docx);

合并文本和图片到模板。

Word的邮件合并是一个非常强大的功能,常被应用于制作大量相似的文档。通过使用它,开发人员可以用定制的数据填充Word

static void Main(string[] args){//载入模板文档 Document doc = new Document();doc.LoadFromFile(@"Template.docx");var textFieldNames = new string[] { "Name", "Phone", "Department" };var textFieldValues = new string[] { "李 梓", "15581678920", "技 术 支 持" };var imageFieldNames = new string[] { "Photo" };var imageFieldValues = new string[] { "image.jpg" };//合并文本到模板doc.MailMerge.Execute(textFieldNames, textFieldValues);//创建合并图片自定义事件doc.MailMerge.MergeImageField += new MergeImageFieldEventHandler(MailMerge_MergeImageField);//合并图片到模板doc.MailMerge.Execute(imageFieldNames, imageFieldValues);//保存文档doc.SaveToFile("result.docx", FileFormat.Docx);}//载入图片static void MailMerge_MergeImageField(object sender, MergeImageFieldEventArgs field){string filePath = field.FieldValue as string;if (!string.IsNullOrEmpty(filePath)){field.Image = Image.FromFile(filePath);}}

将word文档保存为PDF

转换功能作为Spire.Doc产品的一个主要亮点,Spire.Doc支持将 Word文件转换为PDF、HTML、XPS、SVG、Text、图片和XML文件(Word文件的格式可以是.doc,也可以是.docx)。这里我们将直接调用SaveToFile方法将word另存为PDF.

static void Main(string[] args){//载入Sample文档 Document doc = new Document();doc.LoadFromFile("result.docx");doc.SaveToFile("WordtoPDF.PDF", FileFormat.PDF);}

添加PDF数字签名

为防止文件被恶意篡改,并确保PDF文档的权威性,这里我们需要对PDF进行数字签名。当我们用PFX加密证书添加签名时,请务必保证PFX证书的有效性。

//初始化一个PdfDocument实例PdfDocument doc = new PdfDocument();//加载PDF文档doc.LoadFromFile("WordtoPDF.PDF");//根据证书实例化一个PdfCertificate对象PdfCertificate cert = new PdfCertificate("Demo.pfx", "eiceblue");//在指定页面添加数字签名PdfSignature signature = new PdfSignature(doc, doc.Pages[0], cert, "Signature1");//设置签名的位置和大小signature.Bounds = new RectangleF(new PointF(200, 300), new SizeF(180, 90));//设置签名内容signature.IsTag = true;signature.DigitalSignerLable = "Digitally signed by: ";signature.DigitalSigner = "Gary";signature.LocationInfoLabel = "Location:";signature.LocationInfo = "CN";signature.ReasonLabel = "Reason: ";signature.Reason = "Ensure authenticity";signature.DateLabel = "Date: ";signature.Date = DateTime.Now;signature.ContactInfoLabel = "Contact Number: ";signature.ContactInfo = "028-81705109";//设置被签名文档的编辑权限signature.DocumentPermissions = PdfCertificationFlags.AllowFormFill | PdfCertificationFlags.ForbidChanges;//保存文档doc.SaveToFile("数字签名.pdf");

至止,该项目已全部完成。在这个项目中,我们先用Spire.Doc从头开始创建了一个新的word文档模板,并通过Mailmerge 功能,合并了文本和图片到文档并将生成的word文档转换给PDF格式。为了保证数据安全性,我又调用Spire.PDF为PDF结果文档添加了可视化数字签名。全程使用纯代码操作,无需安装任何第三方插件及Microsoftword或AdobeAcrobat.

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。