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();
}
}
}
相关文章
springboot使用AOP+反射实现Excel数据的读取
本文主要介绍了springboot使用AOP+反射实现Excel数据的读取,文中通过示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下2022-01-01使用mybatis-plus报错Invalid bound statement (not found)错误
这篇文章主要介绍了使用mybatis-plus报错Invalid bound statement (not found)错误,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧2020-09-09SpringCloud 客户端Ribbon负载均衡的实现方法
Ribbon 是 Netflix 提供的一个基于 Http 和 TCP 的客户端负载均衡工具,且已集成在 Eureka 依赖中,这篇文章主要介绍了SpringCloud 客户端Ribbon负载均衡的实现方法,需要的朋友可以参考下2022-06-06
最新评论