posts - 930,  comments - 588,  views - 402万
< 2025年2月 >
26 27 28 29 30 31 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 1
2 3 4 5 6 7 8

        有时我们需要对PDF文件进行一些处理, 提取文本,合并等. 以前我们使用A-PDF Text Extractor免费工具,为什么不自己写一个呢?
现在我们可以使用PDFBox-0.7.3这个开源类库. 下载解包后引用:

   PDFBox-0.7.3.dll
   IKVM.GNU.Classpath.dll

        新建一个项目,代码很简单:

   1:          public static string ParseToTxtStringUsingPDFBox(string filename)
   2:          {
   3:              PDDocument doc = PDDocument.load(filename);
   4:              PDFTextStripper stripper = new PDFTextStripper();
   5:              return stripper.getText(doc);
   6:          }

        获得这个textString,再把它们写成磁盘文件就可以了, 像这样的方法:

   1:          public static void WriteToTextFile(string str,string txtpath)
   2:          {
   3:              if (string.IsNullOrEmpty(txtpath))
   4:                  throw new ArgumentNullException("Output file path should not be Null");
   5:   
   6:              using (var txtWriter = new StreamWriter(txtpath))
   7:              {
   8:                  txtWriter.Write(str);
   9:                  txtWriter.Close();
  10:              }
  11:          }

         其它的功能您可以自行发挥了. 这个类库目前支持:

  • PDF to text extraction
  • Merge PDF Documents
  • PDF Document Encryption/Decryption
  • Lucene Search Engine Integration
  • Fill in form data FDF and XFDF
  • Create a PDF from a text file
  • Create images from PDF pages
  • Print a PDF

 

  希望对您开发有帮助.


作者:Petter Liu
出处:http://www.cnblogs.com/wintersun/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
该文章也同时发布在我的独立博客中-Petter Liu Blog

posted on   PetterLiu  阅读(6769)  评论(3编辑  收藏  举报
编辑推荐:
· [.NET]调用本地 Deepseek 模型
· 一个费力不讨好的项目,让我损失了近一半的绩效!
· .NET Core 托管堆内存泄露/CPU异常的常见思路
· PostgreSQL 和 SQL Server 在统计信息维护中的关键差异
· C++代码改造为UTF-8编码问题的总结
阅读排行:
· 实操Deepseek接入个人知识库
· CSnakes vs Python.NET:高效嵌入与灵活互通的跨语言方案对比
· 【.NET】调用本地 Deepseek 模型
· Plotly.NET 一个为 .NET 打造的强大开源交互式图表库
· 上周热点回顾(2.17-2.23)
历史上的今天:
2009-08-28 使Dictionary泛型查询简单化
点击右上角即可分享
微信分享提示