有時(shí)我們需要對(duì)PDF文件進(jìn)行一些處理,提取文本、合并等。以前我們使用A-PDF Text Extractor免費(fèi)工具,為什么不自己寫一個(gè)呢?
現(xiàn)在我們可以使用PDFBox-0.7.3這個(gè)開源類庫(kù). 下載解包后引用:
代碼如下:
PDFBox-0.7.3.dll
IKVM.GNU.Classpath.dll
新建一個(gè)項(xiàng)目,代碼很簡(jiǎn)單:
代碼如下:
public static string ParseToTxtStringUsingPDFBox(string filename){
PDDocument doc = PDDocument.load(filename);
PDFTextStripper stripper = new PDFTextStripper();
return stripper.getText(doc);
}
獲得這個(gè)textString,再把它們寫成磁盤文件就可以了, 像這樣的方法:
代碼如下:
public static void WriteToTextFile(string str,string txtpath)
{
if (string.IsNullOrEmpty(txtpath))
throw new ArgumentNullException("Output file path should not be Null");
using (var txtWriter = new StreamWriter(txtpath))
{
txtWriter.Write(str);
txtWriter.Close();
}
}
其它的功能您可以自行發(fā)揮了. 這個(gè)類庫(kù)目前支持:
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
聲明:本網(wǎng)頁(yè)內(nèi)容旨在傳播知識(shí),若有侵權(quán)等問(wèn)題請(qǐng)及時(shí)與本網(wǎng)聯(lián)系,我們將在第一時(shí)間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com