zoukankan      html  css  js  c++  java
  • 使用PDFBox提取PDF文件中文本

            有时我们需要对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

  • 相关阅读:
    eslint自动格式化
    焕肤功能
    Web Components
    Webpack 中的 sideEffects
    andriod 新建Activity_ Form
    那么唯美
    C# PDF添加水印
    停止触发器
    sp_sys_ERPTrigger_base
    sql语句返回主键SCOPE_IDENTITY()
  • 原文地址:https://www.cnblogs.com/wintersun/p/2156226.html
Copyright © 2011-2022 走看看