java使用pdfbox操作pdf文件示例
還有一個用于創(chuàng)建PDF文件的項目----iText。
PDFBox下面有兩個子項目:FontBox是一個處理PDF字體的java類庫;JempBox是一個處理XMP元數(shù)據(jù)的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();
}
}
}
相關(guān)文章
Spring事件監(jiān)聽器@EventListener與publishEvent的使用
Spring可以通過事件監(jiān)聽器機制來處理應(yīng)用程序中的事件,本文主要介紹了Spring事件監(jiān)聽器@EventListener與publishEvent的使用,具有一定的參考價值,感興趣的可以了解一下2024-06-06Spring中的注解@Value("#{}")與@Value("${}")的區(qū)別
這篇文章主要介紹了Spring中的注解@Value(“#{}“)與@Value(“${}“)的區(qū)別到底是什么,本文給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-06-06Java如何使用ReentrantLock實現(xiàn)長輪詢
這篇文章主要介紹了如何使用ReentrantLock實現(xiàn)長輪詢,對ReentrantLock感興趣的同學(xué),可以參考下2021-04-04SpringCloud Edgware.SR3版本中Ribbon的timeout設(shè)置方法
今天小編就為大家分享一篇關(guān)于SpringCloud Edgware.SR3版本中Ribbon的timeout設(shè)置方法,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧2018-12-12