導航:首頁 > 文檔加密 > java處理pdf

java處理pdf

發布時間:2025-01-26 03:57:52

1. java處理pdf文件

FileInputStream 讀取文件流就OK 至於在頁面顯示

1、客戶機上要有PDF2、URL url =new URL("file:///"+ 你的文件路徑);response.setContentType(url.openConnection().getContentType());response.setHeader("Content-Disposition", "inline; filename="+ 文件名);或在jsp頁面中加入 <% response.setHeader("Content-disposition", "attachment; filename=*.pdf"); %> 以上會提示下載、保存 <% response.setHeader("Content-disposition", "filename=*.pdf"); %> 不要attachment; 就會直接打開,顯示pdf了

2. java解析pdf文件,求大神提供代碼,請注意是java語言的

給你提供一個參考例子,你可以在這個例子上試試,修改修改。也是解析PDF的。

importjava.io.File;
importjava.io.FileOutputStream;
importjava.io.OutputStreamWriter;
importjava.io.Writer;
importjava.net.MalformedURLException;
importjava.net.URL;
importorg.apache.pdfbox.pdmodel.PDDocument;
importorg.apache.pdfbox.util.PDFTextStripper;
publicclassPdfReader{
publicvoidreadFdf(Stringfile)throwsException{
//是否排序
booleansort=false;
//pdf文件名
StringpdfFile=file;
//輸入文本文件名稱
StringtextFile=null;
//編碼方式
Stringencoding="UTF-8";
//開始提取頁數
intstartPage=1;
//結束提取頁數
intendPage=Integer.MAX_VALUE;
//文件輸入流,生成文本文件
Writeroutput=null;
//內存中存儲的PDFDocument
PDDocumentdocument=null;
try{
try{
//首先當作一個URL來裝載文件,如果得到異常再從本地文件系統//去裝載文件
URLurl=newURL(pdfFile);
//注意參數已不是以前版本中的URL.而是File。
document=PDDocument.load(pdfFile);
//獲取PDF的文件名
StringfileName=url.getFile();
//以原來PDF的名稱來命名新產生的txt文件
if(fileName.length()>4){
FileoutputFile=newFile(fileName.substring(0,fileName
.length()-4)
+".txt");
textFile=outputFile.getName();
}
}catch(MalformedURLExceptione){
//如果作為URL裝載得到異常則從文件系統裝載
//注意參數已不是以前版本中的URL.而是File。
document=PDDocument.load(pdfFile);
if(pdfFile.length()>4){
textFile=pdfFile.substring(0,pdfFile.length()-4)
+".txt";
}
}
//文件輸入流,寫入文件倒textFile
output=newOutputStreamWriter(newFileOutputStream(textFile),
encoding);
//PDFTextStripper來提取文本
PDFTextStripperstripper=null;
stripper=newPDFTextStripper();
//設置是否排序
stripper.setSortByPosition(sort);
//設置起始頁
stripper.setStartPage(startPage);
//設置結束頁
stripper.setEndPage(endPage);
//調用PDFTextStripper的writeText提取並輸出文本
stripper.writeText(document,output);
}finally{
if(output!=null){
//關閉輸出流
output.close();
}
if(document!=null){
//關閉PDFDocument
document.close();
}
}
}
/**
*@paramargs
*/
publicstaticvoidmain(String[]args){
//TODOAuto-generatedmethodstub
PdfReaderpdfReader=newPdfReader();
try{
//取得E盤下的SpringGuide.pdf的內容
pdfReader.readFdf("d:\b.pdf");
}catch(Exceptione){
e.printStackTrace();
}
}
}
閱讀全文

與java處理pdf相關的資料

熱點內容
伺服器地址缺少埠號什麼意思 瀏覽:526
下載需要解壓的小說用哪個軟體 瀏覽:530
廣東分布式伺服器雲主機 瀏覽:579
伺服器忙打不開怎麼辦 瀏覽:11
tif壓縮軟體 瀏覽:409
程序員那麼可愛陸漓上班第1天 瀏覽:950
macbookair自帶什麼app 瀏覽:697
如何關了加密的軟體 瀏覽:578
程序員p2p待遇 瀏覽:920
ipd編譯要求 瀏覽:934
壓縮解壓王怎麼用 瀏覽:31
伺服器共享文件如何備份 瀏覽:755
買安卓手機怎麼在官網買 瀏覽:124
詩詞入門PDF 瀏覽:362
毒app是什麼單位 瀏覽:65
如何自己編譯android系統 瀏覽:793
phpmysqlpdomysqli 瀏覽:809
php修改sql語句 瀏覽:721
android有道api 瀏覽:389
撓耳營水表用的什麼app 瀏覽:56