使用pdfbox实现pdf文本提取和合并功能示例

穷则独善其身,达则兼善天下。没有梦想的人到达不了成功的彼岸,也就因此而看不到成功的辉煌。没有梦想的人生是失败的,因为他们根本看不到生命的意义。

有时我们需要对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

本文使用pdfbox实现pdf文本提取和合并功能示例到此结束。脆弱的人,只懂得怜悯自已,而不知道反省自已。小编再次感谢大家对我们的支持!

您可能有感兴趣的文章
.NET使用DinkToPdf将HTML转成PDF的示例代码

C#实现pdf导出 .Net导出pdf文件

pdf转换成jpg示例分享

C# 中使用iTextSharp组件创建PDF的简单方法

C# 生转换网页为pdf