java使用pdfbox操作pdf文件示例
还有一个用于创建PDF文件的项目----iText。
PDFBox下面有两个子项目:FontBox是一个处理PDF字体的java类库;JempBox是一个处理XMP元数据的java类库。
一个简单示例:
要引入pdfbox-app-1.6.0.jar这个包。
package pdf;
import java.io.File;
import java.net.MalformedURLException;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.util.PDFTextStripper;
public class StripPDFContent {
public static String getText(File file)throws Exception{
boolean sort=false;
int startPage=1;
int endPage=10;
PDDocument document=null;
try{
try{
document=PDDocument.load(file);
}catch(MalformedURLException e){
}
PDFTextStripper stripper=new PDFTextStripper();
stripper.setSortByPosition(sort);
stripper.setStartPage(startPage);
stripper.setEndPage(endPage);
return stripper.getText(document);
}catch(Exception e){
e.printStackTrace();
return "";
}finally{
if(document!=null){
document.close();
}
}
}
public static void main(String[] args){
File file=new File("/home/orisun/123.pdf");
try{
String cont=getText(file);
System.out.println(cont);
}catch(Exception e){
System.out.println("Strip failed.");
e.printStackTrace();
}
}
}
相关文章
Spring事件监听器@EventListener与publishEvent的使用
Spring可以通过事件监听器机制来处理应用程序中的事件,本文主要介绍了Spring事件监听器@EventListener与publishEvent的使用,具有一定的参考价值,感兴趣的可以了解一下2024-06-06
Spring中的注解@Value("#{}")与@Value("${}")的区别
这篇文章主要介绍了Spring中的注解@Value(“#{}“)与@Value(“${}“)的区别到底是什么,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下2023-06-06
SpringCloud Edgware.SR3版本中Ribbon的timeout设置方法
今天小编就为大家分享一篇关于SpringCloud Edgware.SR3版本中Ribbon的timeout设置方法,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧2018-12-12


最新评论