这是我目前面临的挑战。
我有很多 PDF,我必须删除其中的空白页面并仅显示包含内容(文本或图像)的页面。
问题是那些 pdf 是扫描文档。
所以空白页上有一些扫描仪留下的脏东西。
最佳答案
我做了一些研究并最终得到了这段代码,它检查页面的 99% 是白色还是浅灰色。 我需要灰色因素,因为扫描的文档有时不是纯白色的。
private static Boolean isBlank(PDPage pdfPage) throws IOException {
BufferedImage bufferedImage = pdfPage.convertToImage();
long count = 0;
int height = bufferedImage.getHeight();
int width = bufferedImage.getWidth();
Double areaFactor = (width * height) * 0.99;
for (int x = 0; x < width ; x++) {
for (int y = 0; y < height ; y++) {
Color c = new Color(bufferedImage.getRGB(x, y));
// verify light gray and white
if (c.getRed() == c.getGreen() && c.getRed() == c.getBlue()
&& c.getRed() >= 248) {
count++;
}
}
}
if (count >= areaFactor) {
return true;
}
return false;
}
关于java - 如何使用 PDFBox 在 PDF 中查找空白页?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23738652/