当前位置: 移动技术网 > IT编程>开发语言>.net > 使用pdfbox实现pdf文本提取和合并功能示例

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

2017年12月12日  | 移动技术网IT编程  | 我要评论

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

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网