有時我們需要對PDF檔案進行一些處理,提取文本、合并等。以前我們使用A-PDF Text Extractor免費工具,為什麼不自己寫一個呢?
現在我們可以使用PDFBox-0.7.3這個開源類庫. 下載下傳解包後引用:
複制代碼 代碼如下:
PDFBox-0.7.3.dll
IKVM.GNU.Classpath.dll
建立一個項目,代碼很簡單:
複制代碼 代碼如下:
public static string ParseToTxtStringUsingPDFBox(string filename){
PDDocument doc = PDDocument.load(filename);
PDFTextStripper stripper = new PDFTextStripper();
return stripper.getText(doc);
}
獲得這個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();
}
}
其它的功能您可以自行發揮了. 這個類庫目前支援:
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
時間: 2014-01-21