有时我们需要对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 2011-08-28 15:53  PetterLiu  阅读(6721)  评论(3编辑  收藏  举报