try {
// PDDocument document = PDDocument.load(new File("C:\\Users\\ThinkPad\\Desktop\\0e510c96-6d60-4002-9fec-a8396951d09b\\OA202307051874700\\aabbccddeefghijklm.pdf"));
PDDocument document = PDDocument.load(new File("C:\\Users\\ThinkPad\\Desktop\\0e510c96-6d60-4002-9fec-a8396951d09b\\OA202307051874700\\main.pdf"));
PDFTextStripper stripper = new PDFTextStripper() {
private StringBuilder tempBuffer = new StringBuilder();
@Override
protected void writeString(String text, List<TextPosition> textPositions) throws IOException {
if (!isWatermark(textPositions,"D:\\Rong\\ideaProject\\KMshy\\out\\artifacts\\kmirmsshy\\")) {
// super.writeString(text, textPositions);
tempBuffer.append(text); // 将符合条件的文本添加到临时缓冲区
}
}
// @Override
// public void writePage() throws IOException {
// super.writeString(tempBuffer.toString()); // 将临时缓冲区的内容写入输出流
// tempBuffer.setLength(0); // 清空缓冲区
// super.writePage(); // 调用基类的方法继续处理页面
// }
@Override
protected void writePageStart() throws IOException {
super.writePageStart();
// 在页面开始时清空缓冲区
tempBuffer.setLength(0);
}
@Override
protected void writePageEnd() throws IOException {
super.writeString(tempBuffer.toString()); // 将已过滤的文本写入输出流
super.writePageEnd();
}
};
stripper.getText(document);
stripper.getOutput();
document.save("C:\\Users\\ThinkPad\\Desktop\\AAPDF\\main.pdf");
document.close();
} catch (IOException e) {
e.printStackTrace();
}
}
已经通过isWatermark方法把正确的文本筛选出来了 但是在document.save()的时候保存的文件和以前一样 为什么呢