❶ 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不存在或者連接超時");
}
}
}