pdf文档操作第三方库合集

1 篇文章 0 订阅

前几天需要操作pdf文件,由于pdf文件含有不同的版本,产生pdf文件的方式也有很多种,对pdf版本兼容也很头痛,下面是从网上找了几个比较好用的库

1.mupdf 比较好用,完全开源,支持不同语言,不过代码看起来有点复杂,兼容的版本比较多。

2.PkPDFConverter 只是单纯的exe文件,支持命令行。

3.podofo开源的,需要安装cmake,cmake生成的时候依赖很多库,支持的pdf版本也不全。

4.pdflib 不开源,商业版收费,库使用很简单。

5.Adobe PDF SDK和Foxit PDF SDK,功能肯定支持的很全,但是收费。


通过比较测试个人觉得第一个比较好用

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 下面是在 .NET 中将 Word 文件转换为 PDF 的代码示例,使用的是 Spire.Doc 第三方库: ``` using Spire.Doc; using Spire.Doc.Documents; namespace WordToPdf { class Program { static void Main(string[] args) { // Load a Word document Document document = new Document(); document.LoadFromFile("input.docx"); // Save the Word document to PDF file document.SaveToFile("output.pdf", FileFormat.PDF); } } } ``` 请注意,需要在项目中添加对 Spire.Doc 的引用。如果没有安装 Spire.Doc,可以在 NuGet 上下载它。 希望这能帮到你! ### 回答2: 要将Word文件转换为PDF文件,可以使用第三方库iTextSharp来实现。以下是使用C#语言编写的示例代码: ```csharp using iTextSharp.text; using iTextSharp.text.pdf; using System.IO; public class WordToPdfConverter { public void ConvertToPdf(string wordFilePath, string pdfFilePath) { // 创建文档对象 Document document = new Document(); // 创建PDF写入器 PdfWriter writer = PdfWriter.GetInstance(document, new FileStream(pdfFilePath, FileMode.Create)); // 打开文档 document.Open(); // 读取Word文件内容 using (StreamReader reader = new StreamReader(wordFilePath)) { string line; // 逐行读取Word文件内容并写入PDF文件中 while ((line = reader.ReadLine()) != null) { document.Add(new Paragraph(line)); } } // 关闭文档 document.Close(); } } ``` 在上述示例代码中,我们首先创建了Document对象,然后创建了PdfWriter对象来指定生成的PDF文件路径。接下来使用StreamReader读取Word文件的内容,然后逐行写入到PDF文件中。最后关闭文档。 你可以创建一个WordToPdfConverter的实例并调用ConvertToPdf方法,将要转换的Word文件路径和生成的PDF文件路径作为参数传入,即可完成将Word文件转换为PDF文件的操作。 需要注意的是,使用该方法转换Word文件至PDF可能会遇到格式不完全一致的情况,具体效果可能会因文档复杂程度而异。如果需要更准确的转换效果,可以考虑使用其他专业的PDF转换工具。 ### 回答3: 要将word文件转为pdf,可以使用第三方库iTextSharp来完成。iTextSharp是一个基于.NET平台的开源PDF库,可以实现PDF文档的创建、读取、修改等操作。 首先,需要在项目中引入iTextSharp库。可以通过NuGet管理解决方案,搜索并安装iTextSharp。 接下来,我们需要编写代码以实现将word文件转为pdf的功能。以下是一个示例代码: ```csharp using iTextSharp.text; using iTextSharp.text.pdf; using System.IO; using Word = Microsoft.Office.Interop.Word; public class WordToPdfConverter { public static void Convert(string wordFilePath, string pdfFilePath) { // 使用iTextSharp创建PDF文档 Document document = new Document(); // 创建PdfWriter对象,将PDF文档写入到指定的文件 PdfWriter writer = PdfWriter.GetInstance(document, new FileStream(pdfFilePath, FileMode.Create)); // 打开文档 document.Open(); // 使用Microsoft.Office.Interop.Word打开Word文档 Word.Application wordApplication = new Word.Application(); object missing = System.Reflection.Missing.Value; Word.Document wordDocument = wordApplication.Documents.Open(wordFilePath, ReadOnly: true, Visible: false); // 将Word文档内容逐页写入到PDF文档中 foreach (Word.Range range in wordDocument.StoryRanges) { string storyType = range.StoryType.ToString(); if (storyType.Equals("wdMainTextStory")) { for (int p = 1; p <= range.Cells.Count; p++) { // 创建段落对象 Paragraph paragraph = new Paragraph(); // 读取段落文本内容 string paragraphText = range.Cells[p].Range.Text.Trim(); // 添加段落到PDF文档中 paragraph.Add(new Phrase(paragraphText)); // 将段落写入到PDF文档中 document.Add(paragraph); } } } // 关闭Word文档 wordDocument.Close(Word.WdSaveOptions.wdDoNotSaveChanges); // 关闭Word应用程序 wordApplication.Quit(); // 关闭PDF文档 document.Close(); } } ``` 在调用Convert方法时,需要传入word文件路径和pdf保存路径,示例代码会将word文件内容逐页写入pdf文件中,并保存为指定路径的pdf文件。 需要注意的是,该示例代码使用了Microsoft.Office.Interop.Word来读取Word文档的内容,所以需要在项目中引用Microsoft.Office.Interop.Word库。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值