Java基于PDFbox實(shí)現(xiàn)讀取處理PDF文件
前言
嗨,大家好,2022年春節(jié)已經(jīng)接近尾聲,各地都陸陸續(xù)續(xù)開工了。近期有朋友做一個(gè)小項(xiàng)目正好使用Java讀取PDF文件信息。因此記錄一下相關(guān)過(guò)程。
pdfbox介紹
PDFbox是一個(gè)開源的、基于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)制流。
開發(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>快速開始
本示例是將指定目錄下的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)文章
Java異常處理運(yùn)行時(shí)異常(RuntimeException)詳解及實(shí)例
這篇文章主要介紹了 Java異常處理運(yùn)行時(shí)異常(RuntimeException)詳解及實(shí)例的相關(guān)資料,需要的朋友可以參考下http://time.qq.com/?pgv_ref=aiotime2017-05-05
Java使用elasticsearch基礎(chǔ)API使用案例講解
這篇文章主要介紹了Java使用elasticsearch基礎(chǔ)API使用案例講解,本篇文章通過(guò)簡(jiǎn)要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-08-08
解決springboot 多線程使用MultipartFile讀取excel文件內(nèi)容報(bào)錯(cuò)問(wèn)題
這篇文章主要介紹了解決springboot 多線程使用MultipartFile讀取excel文件內(nèi)容報(bào)錯(cuò)問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-09-09
springboot如何實(shí)現(xiàn)導(dǎo)入其他配置類
這篇文章主要介紹了springboot如何實(shí)現(xiàn)導(dǎo)入其他配置類問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-11-11
springboot后臺(tái)session的存儲(chǔ)與取出方式
這篇文章主要介紹了springboot后臺(tái)session的存儲(chǔ)與取出方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-06-06

