java打開PDF需要藉助其他的jar包的,如果我沒記錯的話,然後查看相應的API介面。應該是以流的形式進行讀取,之前寫過一點是往裡面寫數據的,生成html文然後寫入PDF文件 工具有: ItextPdf、FlyingAndItext、pd4ml 這些是將html文轉換為PDF文件的...
❷ 如何用java讀取pdf文檔的部分內容
你需要用到PDFbox api
https://pdfbox.apache.org/1.8/cookbook/textextraction.html
例子如下
importjava.io.File;
importjava.io.IOException;
importorg.apache.pdfbox.pdmodel.PDDocument;
importorg.apache.pdfbox.text.PDFTextStripper;
importorg.apache.pdfbox.text.PDFTextStripperByArea;
try{
PDDocumentdocument=null;
document=PDDocument.load(newFile("test.pdf"));
document.getClass();
if(!document.isEncrypted()){
PDFTextStripperByAreastripper=newPDFTextStripperByArea();
stripper.setSortByPosition(true);
PDFTextStripperTstripper=newPDFTextStripper();
Stringst=Tstripper.getText(document);
System.out.println("Text:"+st);
}
}catch(Exceptione){
e.printStackTrace();
}
❸ 怎樣用JAVA編程實現讀取PDF文件中的文字或英文保存到TXT文檔中,不使用第三方jar包。
1、創建一個路徑為要讀取的txt文件的file對象rFile。2、創建一個路徑為要寫入的txt文件的file對象wFile。3、創建一個FileReader對象,傳入rFile到構造器。4、准備一個char數組,FileReader類有一個繼承自java.io.Reader的read(char[]cbuf)方法,將字元讀入數組。5、創建一個FileWriter對象,傳入wFile到構造器。6、FileWriter類有一個繼承自java.io.Writer的write(char[]cbuf)方法,可以寫入字元數組。7、最後別忘了關閉流。
❹ java 如何讀取PDF文件內容
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.net.MalformedURLException;
import java.net.URL;
import org.pdfbox.pdmodel.PDDocument;
import org.pdfbox.util.PDFTextStripper;
public class PdfReader {
public void readFdf(String file) throws Exception {
// 是否排序
boolean sort = false;
// pdf文件名
String pdfFile = file;
// 輸入文本文件名稱
String textFile = null;
// 編碼方式
String encoding = "UTF-8";
// 開始提取頁數
int startPage = 1;
// 結束提取頁數
int endPage = Integer.MAX_VALUE;
// 文件輸入流,生成文本文件
Writer output = null;
// 內存中存儲的PDF Document
PDDocument document = null;
try {
try {
// 首先當作一個URL來裝載文件,如果得到異常再從本地文件系統//去裝載文件
URL url = new URL(pdfFile);
//注意參數已不是以前版本中的URL.而是File。
document = PDDocument.load(pdfFile);
// 獲取PDF的文件名
String fileName = url.getFile();
// 以原來PDF的名稱來命名新產生的txt文件
if (fileName.length() > 4) {
File outputFile = new File(fileName.substring(0, fileName
.length() - 4)
+ ".txt");
textFile = outputFile.getName();
}
} catch (MalformedURLException e) {
// 如果作為URL裝載得到異常則從文件系統裝載
//注意參數已不是以前版本中的URL.而是File。
document = PDDocument.load(pdfFile);
if (pdfFile.length() > 4) {
textFile = pdfFile.substring(0, pdfFile.length() - 4)
+ ".txt";
}
}
// 文件輸入流,寫入文件倒textFile
output = new OutputStreamWriter(new FileOutputStream(textFile),
encoding);
// PDFTextStripper來提取文本
PDFTextStripper stripper = null;
stripper = new PDFTextStripper();
// 設置是否排序
stripper.setSortByPosition(sort);
// 設置起始頁
stripper.setStartPage(startPage);
// 設置結束頁
stripper.setEndPage(endPage);
// 調用PDFTextStripper的writeText提取並輸出文本
stripper.writeText(document, output);
} finally {
if (output != null) {
// 關閉輸出流
output.close();
}
if (document != null) {
// 關閉PDF Document
document.close();
}
}
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
PdfReader pdfReader = new PdfReader();
try {
// 取得E盤下的SpringGuide.pdf的內容
pdfReader.readFdf("E://SpringGuide.pdf");
} catch (Exception e) {
e.printStackTrace();
}
}
}
❺ 如何使用java讀取PDF文件
可以用itext包,這個功能很全,讀、寫、合並全有
❻ 怎麼用java讀取pdf文件內容
你可以把pdf轉成word在進行讀取
推薦使用轉轉大師pdf轉word轉換器,免費的在線工具
網路搜索下,在線免費轉換就行了,不用下載注冊,很方便
❼ 求助:java讀取pdf文件問題.我用下面代碼讀取本地的pdf文件沒問題但是讀取遠程的pdf文件就報異常
用UrlConnetion類, 讀取http:\www.dt158.com\pdf\8928991747799539758.pdf ,
UrlConnection 的 openStream() 方法獲得一個讀取流,就可以讀取了
❽ 用java讀取pdf
可以使用PDFBOX0.7.3控制項:
import java.io.InputStream;import java.io.IOException;
import org.apache.lucene.document.Document;import org.pdfbox.cos.COSDocument;
import org.pdfbox.pdfparser.PDFParser;import org.pdfbox.pdmodel.PDDocument;
import org.pdfbox.pdmodel.PDDocumentInformation;import org.pdfbox.util.PDFTextStripper;
import com.search.code.Index;
public Document getDocument(Index index, String url, String title, InputStream is)throws DocCenterException {COSDocument cosDoc = null;<br/> try {cosDoc = parseDocument(is);<br/> } catch (IOException e) {
closeCOSDocument(cosDoc);
throw new DocCenterException("無法處理該PDF文檔", e);
}
if (cosDoc.isEncrypted()) {
if (cosDoc != null)
closeCOSDocument(cosDoc);
throw new DocCenterException("該PDF文檔是加密文檔,無法處理");
}
String docText = null;
try {
PDFTextStripper stripper = new PDFTextStripper();
docText = stripper.getText(new PDDocument(cosDoc));
} catch (IOException e) {
closeCOSDocument(cosDoc);
throw new DocCenterException("無法處理該PDF文檔", e);
}
PDDocument pdDoc = null;
try {pdDoc = new PDDocument(cosDoc);<br/> PDDocumentInformation docInfo = pdDoc.getDocumentInformation();<br/> if(docInfo.getTitle()!=null && !docInfo.getTitle().equals("")){<br/> title = docInfo.getTitle();}
} catch (Exception e) {
closeCOSDocument(cosDoc);
closePDDocument(pdDoc);
System.err.println("無法取得該PDF文檔的元數據" + e.getMessage());
} finally {
closeCOSDocument(cosDoc);
closePDDocument(pdDoc);
}
return null;
}
private static COSDocument parseDocument(InputStream is) throws IOException {
PDFParser parser = new PDFParser(is);parser.parse();return parser.getDocument();
}
private void closeCOSDocument(COSDocument cosDoc) {
if (cosDoc != null) {try {cosDoc.close();} catch (IOException e) {}
}}
private void closePDDocument(PDDocument pdDoc) {
if (pdDoc != null) {
try { pdDoc.close();
} catch (IOException e) {
}}}
❾ java讀取pdf文件的時候出現異常
當時想把pdf中的文字讀取成txt文件,方便我的手機閱讀,寫了以下代碼,解決了這個小問題
所需要的包:PDFBox-0.7.3.zip 解壓文件夾下:external下的全部,lib文件下的PDFBox-0.7.3.jar
注意:只能讀取文本格式的pdf,如果pdf為圖片格式的,本程序不能使用!
程序介紹:本程序將文本格式pdf中的文字讀取出來,存入與pdf文件同文件名的TXT文本文檔。
支持中文,但是在某些文字上會出現亂碼.
package com.small;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.net.MalformedURLException;
import java.net.URL;
import org.pdfbox.pdmodel.PDDocument;
import org.pdfbox.util.PDFTextStripper;
public class Pdfreader {
public void readFdf(String file) throws Exception {
// 是否排序
boolean sort = false;
// pdf文件名
String pdfFile = file;
// 輸入文本文件名稱
String textFile = null;
// 編碼方式
String encoding = "GB2312";
// 開始提取頁數
int startPage = 1;
// 結束提取頁數
int endPage = Integer.MAX_VALUE;
// 文件輸入流,生成文本文件
Writer output = null;
// 內存中存儲的PDF Document
PDDocument document = null;
try {
try {
// 首先當作一個URL來裝載文件,如果得到異常再從本地文件系統//去裝載文件
URL url = new URL(pdfFile); //注意參數已不是以前版本中的URL.而是File。
document = PDDocument.load(pdfFile);
// 獲取PDF的文件名
String fileName = url.getFile();
// 以原來PDF的名稱來命名新產生的txt文件
if (fileName.length() > 4) {
File outputFile = new File(fileName.substring(0, fileName
.length() - 4)
+ ".txt");
textFile = outputFile.getName();
}
} catch (MalformedURLException e) {
// 如果作為URL裝載得到異常則從文件系統裝載 //注意參數已不是以前版本中的URL.而是File。
document = PDDocument.load(pdfFile);
if (pdfFile.length() > 4) { textFile = pdfFile.substring(0, pdfFile.length() - 4)
+ ".txt";
}
}
// 文件輸入流,寫入文件倒textFile
output = new OutputStreamWriter(new FileOutputStream(textFile),
encoding);
// PDFTextStripper來提取文本
PDFTextStripper stripper = null;
stripper = new PDFTextStripper();
// 設置是否排序
stripper.setSortByPosition(sort);
// 設置起始頁
stripper.setStartPage(startPage);
// 設置結束頁
System.out.print(stripper.getText(document));
stripper.setEndPage(endPage);
// 調用PDFTextStripper的writeText提取並輸出文本
stripper.writeText(document, output);
} finally {
if (output != null) {
// 關閉輸出流
output.close();
}
if (document != null) {
// 關閉PDF Document
document.close();
}
}
} /**
* @param args
*/
public static void main(String[] args) {
Pdfreader pdfReader = new Pdfreader();
try {
// 讀取pdf文件
pdfReader.readFdf("d:\\2\\D.pdf");
} catch (Exception e) {
e.printStackTrace();
}
}
}
http://hi..com/websays/blog/item/bb5a9daa950dc1baca130c7f.html
❿ java如何讀取某個網址下的pdf文件
讀取http文件的文件應該用HttpURLConnection 獲取inputstrem
然後在PDFParser p = new PDFParser(inputstrem);這樣