PDFBox API 简介

PDFBox 设计时采用面向对象的方式来描述PDF文件。PDF文件的数据时一系列基本对象的集合:数组,布尔型,字典,数字,字符串和二进制流。PDFBox在org.pdfbox.cos包(COS模型)中定义这些基本对象类型,我们可以使用这些对象与PDF文档进行任何交互,但是首先应该对PDF文档内部结构以及高层概念做一些深入的了解。例如,页面和字体都是带有特殊属性的字典对象。

       新浪下载地址:http://ishare.iask.sina.com.cn/f/15276873.html

      .NET下使用C#读取PDF步骤:

       解压缩下载的PDFBox,找到其中的Bin目录,需要在项目中添加引用的dll文件有:
        IKVM.GNU.Classpath.dll
        PDFBox-0.7.3.dll
        FontBox-0.1.0-dev.dll
        IKVM.Runtime.dll
        将以上4个文件引用到项目中,在文件中需要引入以下2个命名空间:
        using org.pdfbox.pdmodel;
        using org.pdfbox.util;
      以下为一个非常简单的读取PDF文件的例子:

     void ReadPdf()
     {
       FileInfo file = new FileInfo(Server.MapPath("./pdf/Silverlight.pdf"));
       FileInfo txtfile = new FileInfo(Server.MapPath("./pdf/moon.txt"));
       pdf2txt(file, txtfile);
     }
    public void pdf2txt(FileInfo file, FileInfo txtfile)
    {
         PDDocument doc = PDDocument.load(file.FullName);
         PDFTextStripper pdfStripper = new PDFTextStripper();
        string text = pdfStripper.getText(doc);
        StreamWriter swPdfChange = new StreamWriter(txtfile .FullName ,false,System.Text.Encoding.GetEncoding("gb2312"));
        swPdfChange.Write(text);
        swPdfChange.Close();
      }

如此便可以将PDF中的内容读取到txt文件中。

原文地址:https://www.cnblogs.com/xuezhi/p/2788999.html