導航:首頁 > 文檔加密 > java二級PDF

java二級PDF

發布時間:2025-02-10 17:16:56

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的幾個問題,是高手的進。

1、我不清楚,沒做過相關東西
2、通過js可以實現禁止右鍵行為
3、頁面無法列印好像實現不了,因為瀏覽器有列印功能會將頁面列印出來

❸ Java如何使用Java創建一個空的PDF文檔

package com.yii;import java.io.IOException;import org.apache.pdfbox.pdmodel.PDDocument;import org.apache.pdfbox.pdmodel.PDPage;// 需要下 apache pdfbox包和apache.commons.loggin烏,下載地址:http://pdfbox.apache.org/download.cgi 和 http://commons.apache.org/proper/commons-logging/download_logging.cgi// 在本示例中下載使用的是:pdfbox-2.0.7.jar // 將下載的pdfbox-2.0.7.jar添加到Eclipse項目依懶庫中。// 右鍵點擊:"java_apache_pdf_box"->"Bulid Path"->"Add External Artchives...",然後選篤下載的"pdfbox-2.0.7.jar"和"commons-logging-1.2.jar"文件 public class CreatingEmptyPdf {
public static void main(String args[]) throws IOException {

// Creating PDF document object
PDDocument document = new PDDocument();

// Add an empty page to it
document.addPage(new PDPage());

// Saving the document
document.save("F:/worksp/javaexamples/java_apache_pdf_box/BlankPdf.pdf");
System.out.println("PDF created");

// Closing the document
document.close();
}}

❹ java程序下載pdf文件

主要是 URL 和 HttpURLConnection 類的運用,看代碼:


importjava.io.DataInputStream;
importjava.io.FileOutputStream;
importjava.io.IOException;
importjava.net.HttpURLConnection;
importjava.net.URL;

publicclassHttpDownloader{

_FILE_URL="http://211.103.156.163/u/cms/www/201511/25051940i6ou.pdf";
privatestaticfinalStringLOCAL_FILE_PATH="D:/some.pdf";//改成你保存文件的路徑

publicstaticvoidmain(String[]args){
newHttpDownloader(REMOTE_FILE_URL,LOCAL_FILE_PATH).download();
}

privateStringremoteFileUrl;
privateStringlocalFilePath;

publicHttpDownloader(StringremoteFileUrl,StringlocalFilePath){
this.remoteFileUrl=remoteFileUrl;
this.localFilePath=localFilePath;
}

publicvoiddownload(){
try{
URLurl=newURL(remoteFileUrl);

=(HttpURLConnection)url.openConnection();
httpURLConnection.setConnectTimeout(5*1000);//5000毫秒內沒有連接上則放棄連接
httpURLConnection.connect();//連接
System.out.println("連接URL成功~");

intfileLenght=httpURLConnection.getContentLength();
System.out.println("文件大小:"+(fileLenght/1024.0)+"KB");

System.out.println("開始下載...");
try(DataInputStreamdis=newDataInputStream(httpURLConnection.getInputStream());
FileOutputStreamfos=newFileOutputStream(localFilePath)){
byte[]buf=newbyte[10240];//根據實際情況可以增大buf大小
for(intreadSize;(readSize=dis.read(buf))>0;){
fos.write(buf,0,readSize);
}
System.out.println("下載完畢~");
}catch(IOExceptionex){
System.out.println("下載時出錯");
}

httpURLConnection.disconnect();
}catch(IOExceptionex){
System.out.println("URL不存在或者連接超時");
}
}
}
閱讀全文

與java二級PDF相關的資料

熱點內容
女程序員熬夜體檢 瀏覽:715
解壓故事校園戀愛又煩惱 瀏覽:555
冰箱壓縮機放多久能啟動 瀏覽:171
軟體演算法發明專利 瀏覽:987
旁氏演算法 瀏覽:765
程序員那麼可愛電視劇免費觀看極速版 瀏覽:747
程序員那麼可愛陸漓孩子保住了嗎 瀏覽:564
如何獲取伺服器dns秒解 瀏覽:663
如何破解大黃蜂的加密文件 瀏覽:11
新概念英語第三冊pdf 瀏覽:401
分項詳細估演算法步驟 瀏覽:436
ipad桌面文件夾放大 瀏覽:891
我的世界基岩版怎麼進國際伺服器 瀏覽:516
福州醫院有沒有解壓艙 瀏覽:470
帶pwm的51單片機 瀏覽:917
ace程序員指南源碼 瀏覽:414
哪個app可以分36期還款 瀏覽:244
為什麼安卓軟體平台那麼多 瀏覽:288
如果把伺服器切斷了怎麼辦 瀏覽:19
app建立網站需要什麼技術 瀏覽:691