当前位置: 首页 > 工具软件 > Reader > 使用案例 >

pdfReader 解析pdf(ItextPdf)

颛孙镜
2023-12-01
public static String getInfo(String pdfFilePath) {
    HashMap<String, String> info = new HashMap<>();
    PdfDocument document = null;
    StringBuffer sb = new StringBuffer();
    try (FileInputStream is = new FileInputStream(pdfFilePath)) {
        PdfReader reader = new PdfReader(is);
        int pages = reader.getNumberOfPages();
        for (int i = 1; i <= pages; i++) {
            String textFromPage = PdfTextExtractor.getTextFromPage(reader, i);
            sb.append(textFromPage);
            sb.append("\n");//读取第i页的文档内容
        }
        String s = sb.toString();
        reader.close();
        return s.replaceAll(" ","");
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return StringUtils.EMPTY;
}

 

 类似资料: