.net如何读取PDF文档的内容

来源:互联网 发布:excel03版数据分析表 编辑:程序博客网 时间:2024/05/18 02:12

一、下载PDFBox

访问网址http://sourceforge.net/projects/pdfbox/,进入如图7-1所示的下载界面。

 

PDFBox是一个开源的Java PDF库,这个库允许你访问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;

三、API的使用方法

请见以下示例:

using org.pdfbox.pdmodel;
using org.pdfbox.util;

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, Encoding.GetEncoding("gb2312"));

        swPdfChange.Write(text);

        swPdfChange.Close();

    }