.NET Framework 4.6.2下用iTextSharp v4.1.6将Word文档追加到PDF
实现Word文档追加到PDF的可行方案
由于iTextSharp v4.1.6.0不支持直接解析Word文档,核心思路是先将Word转换为PDF格式,再复用你现有代码中追加PDF的逻辑完成合并。以下是两种适配.NET Framework 4.6.2的可行方案:
方案一:利用Office Interop转换Word为PDF(需服务器安装Office)
该方案依赖Microsoft Office的COM组件,需确保服务器已安装对应版本的Office,且应用程序池权限足够访问Office组件。
转换代码示例
先添加对Microsoft.Office.Interop.Word的引用(可通过NuGet安装同名包):
using Microsoft.Office.Interop.Word; using System.IO; // 将Word文档转换为临时PDF文件 private string ConvertWordToPdf(string wordFilePath) { var tempPdfPath = Path.Combine(Path.GetTempPath(), Guid.NewGuid() + ".pdf"); Application wordApp = new Application(); Document wordDoc = null; try { wordDoc = wordApp.Documents.Open(wordFilePath); wordDoc.ExportAsFixedFormat(tempPdfPath, WdExportFormat.wdExportFormatPDF); } finally { wordDoc?.Close(); wordApp.Quit(); // 释放COM资源,避免内存泄漏 System.Runtime.InteropServices.Marshal.ReleaseComObject(wordDoc); System.Runtime.InteropServices.Marshal.ReleaseComObject(wordApp); } return tempPdfPath; }
整合到现有代码
在你的foreach循环中添加Word文件的处理分支:
foreach (additionalFiles a in additionalFiles) { document.NewPage(); // plain text if (a.MimeType == "text/plain") { // 原有代码不变 StreamReader str = new StreamReader(System.Web.HttpContext.Current.Server.MapPath(a.FilePath)); document.Add(new Paragraph(str.ReadToEnd())); str.Close(); } // images else if (a.MimeType == "image/png" || a.MimeType == "image/jpeg" || a.MimeType == "image/gif") { // 原有代码不变 var aImage = Image.GetInstance(System.Web.HttpContext.Current.Server.MapPath(a.FilePath)); document.Add(aImage); } // PDF's else if (a.MimeType == "application/pdf") { // 原有代码不变 PdfReader reader = new PdfReader(System.Web.HttpContext.Current.Server.MapPath(a.FilePath), null); var pageCount = reader.NumberOfPages; for (int i = 1; i <= pageCount; i++) { PdfImportedPage page = writer.GetImportedPage(reader, i); PdfContentByte cb = writer.DirectContent; cb.AddTemplate(page, 0, 0); if (i != pageCount) { document.NewPage(); } } reader.Close(); } // Word文档(.doc和.docx) else if (a.MimeType == "application/msword" || a.MimeType == "application/vnd.openxmlformats-officedocument.wordprocessingml.document") { string wordPath = System.Web.HttpContext.Current.Server.MapPath(a.FilePath); string tempPdfPath = ConvertWordToPdf(wordPath); // 复用现有PDF追加逻辑 PdfReader reader = new PdfReader(tempPdfPath, null); var pageCount = reader.NumberOfPages; for (int i = 1; i <= pageCount; i++) { PdfImportedPage page = writer.GetImportedPage(reader, i); PdfContentByte cb = writer.DirectContent; cb.AddTemplate(page, 0, 0); if (i != pageCount) { document.NewPage(); } } reader.Close(); // 删除临时PDF文件 File.Delete(tempPdfPath); } //...Additional Code }
方案二:使用FreeSpire.Doc免费版转换Word为PDF(无需安装Office)
该方案无需依赖Office,适合服务器环境,FreeSpire.Doc免费版支持转换最多5页的Word文档,若需处理更长文档可考虑付费版。
转换代码示例
先通过NuGet安装FreeSpire.Doc包:
using Spire.Doc; using System.IO; // 将Word文档转换为临时PDF文件 private string ConvertWordToPdf(string wordFilePath) { var tempPdfPath = Path.Combine(Path.GetTempPath(), Guid.NewGuid() + ".pdf"); Document doc = new Document(); doc.LoadFromFile(wordFilePath); doc.SaveToFile(tempPdfPath, FileFormat.PDF); doc.Close(); return tempPdfPath; }
整合到现有代码
和方案一的整合逻辑完全一致,替换ConvertWordToPdf方法即可,无需修改循环中的分支代码。
内容的提问来源于stack exchange,提问作者shadonar
相关产品推荐
相关产品推荐

