Java基于PDFbox實(shí)現(xiàn)讀取處理PDF文件
前言
嗨,大家好,2022年春節(jié)已經(jīng)接近尾聲,各地都陸陸續(xù)續(xù)開(kāi)工了。近期有朋友做一個(gè)小項(xiàng)目正好使用Java讀取PDF文件信息。因此記錄一下相關(guān)過(guò)程。
pdfbox介紹
PDFbox是一個(gè)開(kāi)源的、基于Java的、支持PDF文檔生成的工具庫(kù),它可以用于創(chuàng)建新的PDF文檔,修改現(xiàn)有的PDF文檔,還可以從PDF文檔中提取所需的內(nèi)容。Apache PDFBox還包含了數(shù)個(gè)命令行工具。
PDF文件的數(shù)據(jù)時(shí)一系列基本對(duì)象的集合:數(shù)組,布爾型,字典,數(shù)字,字符串和二進(jìn)制流。
開(kāi)發(fā)環(huán)境
本次Java基于PDFbox讀取處理PDF文件的版本信息如下:
JDK1.8
SpringBoot 2.3.0.RELEASE
PDFbox 1.8.13
PDFbox依賴
在初次使用PDFbox的時(shí)候需要引入PDFbox依賴。本次使用的依賴包如下:
<dependency>
<groupId>org.apache.pdfbox</groupId>
<artifactId>pdfbox</artifactId>
<version>1.8.13</version>
</dependency>快速開(kāi)始
本示例是將指定目錄下的PDF文件中的信息讀取出來(lái),存儲(chǔ)到新的指定路徑的txt文本文件當(dāng)中。
class PdfTest {
public static void main(String[] args) throws Exception {
String filePath ="C:\\Users\\Admin\\Desktop\\cxy1.pdf";
List<String> list = getFiles(basePath);
for (String filePath : list) {
long ltime = System.currentTimeMillis();
String substring = filePath.substring(filePath.lastIndexOf("\\") + 1, filePath.lastIndexOf("."));
String project = "(juejin.cn)";
String textFromPdf = getTextFromPdf(filePath);
String s = writterTxt(textFromPdf, substring + "--", ltime, basePath);
StringBuffer stringBuffer = readerText(s, project);
writterTxt(stringBuffer.toString(), substring + "-", ltime, basePath);
}
System.out.println("******************** end ************************");
}
public static List<String> getFiles(String path) {
List<String> files = new ArrayList<String>();
File file = new File(path);
File[] tempList = file.listFiles();
for (int i = 0; i < tempList.length; i++) {
if (tempList[i].isFile()) {
if (tempList[i].toString().contains(".pdf") || tempList[i].toString().contains(".PDF")) {
files.add(tempList[i].toString());
}
//文件名,不包含路徑
//String fileName = tempList[i].getName();
}
if (tempList[i].isDirectory()) {
//這里就不遞歸了,
}
}
return files;
}
public static String getTextFromPdf(String filePath) throws Exception {
String result = null;
FileInputStream is = null;
PDDocument document = null;
try {
is = new FileInputStream(filePath);
PDFParser parser = new PDFParser(is);
parser.parse();
document = parser.getPDDocument();
PDFTextStripper stripper = new PDFTextStripper();
result = stripper.getText(document);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (is != null) {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (document != null) {
try {
document.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
Map<String, String> map = new HashMap<String, String>();
return result;
}
public static String writterTxt(String data, String text, long l, String basePath) {
String fileName = null;
try {
if (text == null) {
fileName = basePath + "javaio-" + l + ".txt";
} else {
fileName = basePath + text + l + ".txt";
}
File file = new File(fileName);
//if file doesnt exists, then create it
if (!file.exists()) {
file.createNewFile();
}
//true = append file
OutputStream outputStream = new FileOutputStream(file);
// FileWriter fileWritter = new FileWriter(file.getName(), true);
// fileWritter.write(data);
// fileWritter.close();
OutputStreamWriter outputStreamWriter = new OutputStreamWriter(outputStream);
outputStreamWriter.write(data);
outputStreamWriter.close();
outputStream.close();
System.out.println("Done");
} catch (IOException e) {
e.printStackTrace();
}
return fileName;
}
public static StringBuffer readerText(String name, String project) {
// 使用ArrayList來(lái)存儲(chǔ)每行讀取到的字符串
StringBuffer stringBuffer = new StringBuffer();
try {
FileReader fr = new FileReader(name);
BufferedReader bf = new BufferedReader(fr);
String str;
// 按行讀取字符串
while ((str = bf.readLine()) != null) {
str = replaceAll(str);
if (str.contains("D、") || str.contains("D.")) {
stringBuffer.append(str);
stringBuffer.append("\n");
stringBuffer.append("參考: \n");
stringBuffer.append("參考: \n");
stringBuffer.append("\n\n\n\n");
} else if (str.contains("A、") || str.contains("A.")) {
stringBuffer.deleteCharAt(stringBuffer.length() - 1);
stringBuffer.append("。" + project + "\n");
stringBuffer.append(str + "\n");
} else if (str.contains("B、") || str.contains("C、") || str.contains("B.") || str.contains("C.")) {
stringBuffer.append(str + "\n");
} else {
stringBuffer.append(str);
}
}
bf.close();
fr.close();
} catch (IOException e) {
e.printStackTrace();
}
return stringBuffer;
}
public static String replaceAll(String str) {
return str.replaceAll("網(wǎng)", "");
}
}結(jié)語(yǔ)
好了,以上就是Java中繼承相關(guān)概念介紹,感謝您的閱讀,希望您喜歡,如有不足之處,歡迎評(píng)論指正。
到此這篇關(guān)于Java基于PDFbox實(shí)現(xiàn)讀取處理PDF文件的文章就介紹到這了,更多相關(guān)Java讀取處理PDF內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
設(shè)計(jì)模式之模版方法模式_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理
這篇文章主要介紹了設(shè)計(jì)模式之模版方法模式,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-08-08
使用profiles進(jìn)行多環(huán)境配置的代碼實(shí)現(xiàn)
在項(xiàng)目開(kāi)發(fā)的過(guò)程中會(huì)用到多個(gè)環(huán)境,為了便于開(kāi)發(fā)使用,通常需要使用profiles進(jìn)行多環(huán)境配置,所以本文給大家介紹了使用profiles進(jìn)行多環(huán)境配置的代碼實(shí)現(xiàn),需要的朋友可以參考下2024-02-02
SpringBoot?SPI?機(jī)制和實(shí)現(xiàn)自定義?starter
這篇文章主要介紹了SpringBoot?SPI機(jī)制和實(shí)現(xiàn)自定義?starter,全稱是Service?Provider?Interface。簡(jiǎn)單翻譯的話,就是服務(wù)提供者接口,是一種尋找服務(wù)實(shí)現(xiàn)的機(jī)制2022-08-08
servlet監(jiān)聽(tīng)實(shí)現(xiàn)統(tǒng)計(jì)在線人數(shù)功能 附源碼下載
這篇文章主要為大家詳細(xì)介紹了servlet監(jiān)聽(tīng)統(tǒng)計(jì)在線人數(shù)的實(shí)現(xiàn)方法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-04-04
玩轉(zhuǎn)SpringBoot中的那些連接池(小結(jié))
這篇文章主要介紹了玩轉(zhuǎn)SpringBoot中的那些連接池(小結(jié)),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-12-12
java方法實(shí)現(xiàn)簡(jiǎn)易ATM功能
這篇文章主要為大家詳細(xì)介紹了用java方法實(shí)現(xiàn)簡(jiǎn)易ATM功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-04-04
springboot+redis過(guò)期事件監(jiān)聽(tīng)實(shí)現(xiàn)過(guò)程解析
這篇文章主要介紹了springboot+redis過(guò)期事件監(jiān)聽(tīng)實(shí)現(xiàn)過(guò)程解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-03-03

