最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Java基于PDFbox實(shí)現(xiàn)讀取處理PDF文件

 更新時(shí)間:2022年02月09日 10:06:15   作者:小阿杰  
PDFbox是一個(gè)開源的、基于Java的、支持PDF文檔生成的工具庫(kù),它可以用于創(chuàng)建新的PDF文檔,修改現(xiàn)有的PDF文檔,還可以從PDF文檔中提取所需的內(nèi)容。本文將具體介紹一下PDFbox讀取處理PDF文件的示例代碼,感興趣的可以學(xué)習(xí)一下

前言

嗨,大家好,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í)例

    這篇文章主要介紹了 Java異常處理運(yùn)行時(shí)異常(RuntimeException)詳解及實(shí)例的相關(guān)資料,需要的朋友可以參考下http://time.qq.com/?pgv_ref=aiotime
    2017-05-05
  • Java實(shí)現(xiàn)圖片切割功能

    Java實(shí)現(xiàn)圖片切割功能

    這篇文章主要為大家詳細(xì)介紹了Java實(shí)現(xiàn)圖片切割功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-01-01
  • Java中實(shí)現(xiàn)可拖放圖片剪裁入門教程

    Java中實(shí)現(xiàn)可拖放圖片剪裁入門教程

    這篇文章主要介紹了Java中實(shí)現(xiàn)可拖放圖片剪裁入門教程,本文寫給新手,分步驟講解如何實(shí)現(xiàn)圖片裁剪,并對(duì)每步的代碼作注釋,需要的朋友可以參考下
    2015-01-01
  • Java使用elasticsearch基礎(chǔ)API使用案例講解

    Java使用elasticsearch基礎(chǔ)API使用案例講解

    這篇文章主要介紹了Java使用elasticsearch基礎(chǔ)API使用案例講解,本篇文章通過(guò)簡(jiǎn)要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下
    2021-08-08
  • java事務(wù)的概念淺析

    java事務(wù)的概念淺析

    在本篇內(nèi)容里小編給大家整理的是一篇關(guān)于java事務(wù)的概念淺析,有興趣的朋友們可以參考學(xué)習(xí)下。
    2020-12-12
  • 解決springboot 多線程使用MultipartFile讀取excel文件內(nèi)容報(bào)錯(cuò)問(wèn)題

    解決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
  • Java+MySQL前后端連接新手小白教程

    Java+MySQL前后端連接新手小白教程

    Java作為一種廣泛使用的編程語(yǔ)言之一,在開發(fā)Web應(yīng)用程序時(shí)經(jīng)常需要連接MySQL數(shù)據(jù)庫(kù)進(jìn)行數(shù)據(jù)操作,下面這篇文章主要給大家介紹了關(guān)于Java+MySQL前后端連接的相關(guān)資料,需要的朋友可以參考下
    2024-03-03
  • springboot如何實(shí)現(xiàn)導(dǎo)入其他配置類

    springboot如何實(shí)現(xiàn)導(dǎo)入其他配置類

    這篇文章主要介紹了springboot如何實(shí)現(xiàn)導(dǎo)入其他配置類問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-11-11
  • Hyperlane 文件分塊上傳服務(wù)端的解決方案

    Hyperlane 文件分塊上傳服務(wù)端的解決方案

    在現(xiàn)代Web應(yīng)用中,文件上傳是一個(gè)核心功能,尤其是對(duì)于大文件,傳統(tǒng)的上傳方式常常因網(wǎng)絡(luò)中斷或超時(shí)而失敗,為了解決這一痛點(diǎn),我們推出了基于 Hyperlane 的文件分塊上傳服務(wù)端代碼,為開發(fā)者提供了一個(gè)高效、可靠的大文件上傳解決方案,感興趣的朋友一起看看吧
    2025-04-04
  • springboot后臺(tái)session的存儲(chǔ)與取出方式

    springboot后臺(tái)session的存儲(chǔ)與取出方式

    這篇文章主要介紹了springboot后臺(tái)session的存儲(chǔ)與取出方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-06-06

最新評(píng)論

正定县| 永兴县| 建始县| 镇原县| 沧州市| 翼城县| 萍乡市| 宁国市| 峡江县| 焦作市| 聂荣县| 昆明市| 马山县| 泸溪县| 嘉定区| 涞水县| 隆林| 兰西县| 新郑市| 巧家县| 定日县| 全州县| 霍邱县| 华亭县| 台中市| 高要市| 宣汉县| 汕头市| 镇雄县| 东阿县| 进贤县| 金平| 连城县| 清河县| 南澳县| 稷山县| 岳池县| 抚顺县| 绵竹市| 石楼县| 恩平市|