C#讀取PDF ——PDFBox使用

一、下載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 System.IO;

using System.Text;

using org.pdfbox.pdmodel;

using org.pdfbox.util;

 

namespace PDFReader

{

    class Program

    {

 

        public static void pdf2txt(FileInfo pdffile, FileInfo txtfile)

        {

 

            PDDocument doc = PDDocument.load(pdffile.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();

 

        }

 

        static void Main(string[] args)

        {

            pdf2txt(new FileInfo(@"C:/Users/Susan/Desktop/完整稿__匆匆那年_九夜茴.pdf"), new FileInfo(@"C:/Users/Susan/Desktop/完整稿__匆匆那年_九夜茴.txt"));

        }

    }

}

轉化中文是沒有問題的,原因你應該知道。
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章