Java基于PDFbox實現讀取處理PDF文件
前言
嗨,大家好,2022年春節(jié)已經接近尾聲,各地都陸陸續(xù)續(xù)開工了。近期有朋友做一個小項目正好使用Java讀取PDF文件信息。因此記錄一下相關過程。
pdfbox介紹
PDFbox是一個開源的、基于Java的、支持PDF文檔生成的工具庫,它可以用于創(chuàng)建新的PDF文檔,修改現有的PDF文檔,還可以從PDF文檔中提取所需的內容。Apache PDFBox還包含了數個命令行工具。
PDF文件的數據時一系列基本對象的集合:數組,布爾型,字典,數字,字符串和二進制流。
開發(fā)環(huán)境
本次Java基于PDFbox讀取處理PDF文件的版本信息如下:
JDK1.8
SpringBoot 2.3.0.RELEASE
PDFbox 1.8.13
PDFbox依賴
在初次使用PDFbox的時候需要引入PDFbox依賴。本次使用的依賴包如下:
<dependency> <groupId>org.apache.pdfbox</groupId> <artifactId>pdfbox</artifactId> <version>1.8.13</version> </dependency>
快速開始
本示例是將指定目錄下的PDF文件中的信息讀取出來,存儲到新的指定路徑的txt文本文件當中。
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來存儲每行讀取到的字符串 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("網", ""); } }
結語
好了,以上就是Java中繼承相關概念介紹,感謝您的閱讀,希望您喜歡,如有不足之處,歡迎評論指正。
到此這篇關于Java基于PDFbox實現讀取處理PDF文件的文章就介紹到這了,更多相關Java讀取處理PDF內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
SpringBoot?SPI?機制和實現自定義?starter
這篇文章主要介紹了SpringBoot?SPI機制和實現自定義?starter,全稱是Service?Provider?Interface。簡單翻譯的話,就是服務提供者接口,是一種尋找服務實現的機制2022-08-08servlet監(jiān)聽實現統(tǒng)計在線人數功能 附源碼下載
這篇文章主要為大家詳細介紹了servlet監(jiān)聽統(tǒng)計在線人數的實現方法,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-04-04springboot+redis過期事件監(jiān)聽實現過程解析
這篇文章主要介紹了springboot+redis過期事件監(jiān)聽實現過程解析,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2020-03-03