public List<HashMap<String, Object>> readPdfString(File file ,int pageNum) { List<HashMap<String, Object>> result = Collections.synchronizedList(new ArrayList<HashMap<String,Object>>()); PDDocument doc = null; PDDocument originalDocument=null; try{ // 创建新的 PDF 文档 originalDocument=PDDocument.load(file); doc = new PDDocument(); // 遍历原始文档的页面并复制到新文档 for (PDPage page : originalDocument.getPages()) { doc.addPage(page); } doc.save(file); doc=PDDocument.load(file); /** * 为0表示读全部页的数据,大于0表示读取指定页码的数据 */ pageNum = pageNum == 0 ? doc.getNumberOfPages() : pageNum;
CountDownLatch latch = new CountDownLatch(pageNum); for(int i=1;i<=pageNum;i++) { int finalI = i; PDDocument finalDoc = doc; executorService.execute(() ->{ int attempts = 0; try { HashMap<String, Object> map = new HashMap<>(); PDFTextStripper textStripper =new PDFTextStripper(); textStripper.setSortByPosition(true); // 设置是否按文本位置排序 textStripper.setStartPage(finalI); // 设置开始页数 textStripper.setEndPage(finalI); // 设置结束页数 // 从 PDF 文档提取文本 String text = textStripper.getText(finalDoc); int maxAttempts = 5; // 设置最大尝试次数 while (attempts < maxAttempts&&!text.contains("兹证明")&&finalI==1) { text = textStripper.getText(finalDoc); if (text.contains("兹证明")) { break; } attempts++; } map.put("id",finalI); map.put("data",text); result.add(map); }catch (Exception e){ log.error("读取pdf内容失败",e); attempts++; }finally { latch.countDown(); } }); } latch.await(); }catch (Exception e){ log.error(e.getMessage(),e); }finally { IoUtil.close(doc); IoUtil.close(originalDocument); } return result; } IDEA报错 org.apache.fontbox.ttf.TTFParser 151 parse 然后中文是乱码的