现在的位置: 首页 > 综合 > 正文

.net C#读取PDF ——PDFBox使用下载

2013年10月31日 ⁄ 综合 ⁄ 共 1798字 ⁄ 字号 评论关闭

 

一、下载PDFBox

访问网址http://sourceforge.net/projects/pdfbox/
二、引用动态链接库

    解压缩下载的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();

    }
-----------------------关于最新版本另一种说法
需要复制并加载如下5个DLL文件到bin目录下面
# IKVM.GNU.Classpath.dll
# PDFBox-0.7.3.dll
# FontBox-0.1.0-dev.dll
# IKVM.Runtime.dll
# bcprov-jdk14-132.dll

实例代码:
using System;

using org.pdfbox.pdmodel;

using org.pdfbox.util;

namespace PDFReader

{

    class Program

    {

        static void Main(string[] args)

        {

            PDDocument doc = PDDocument.load("lopreacamasa.pdf");

            PDFTextStripper pdfStripper = new PDFTextStripper();

            Console.Write(pdfStripper.getText(doc));

        }

    }

}
====================
using org.pdfbox.pdmodel;
using org.pdfbox.util;
using org.pdfbox;

//读取本地PDF
    public string PdfReader(string filename)
    {
        string fullname = DocPath + filename;
        PDDocument doc = PDDocument.load(fullname);
        PDFTextStripper stripper = new PDFTextStripper();
        string pdoc = stripper.getText(doc);
        return pdoc;
    }

//读取网络PDF

    public string PdfReader(string fileuri)
    {
        java.net.URL url = new URL(fileuri);
        PDDocument doc = PDDocument.load(url);
        PDFTextStripper stripper = new PDFTextStripper();
        string pdoc = stripper.getText(doc);
        return pdoc;
    }

抱歉!评论已关闭.