我需要从pdf中提取文本以验证某些内容并使用java计算pdf文档中的图像数量。我可以使用下面的getText函数获取文本内容没有问题但是找不到计算图像对象的方法。我已经能够使用下面的代码计算所有对象,但无法找到关于如何仅计算图像的任何doco。任何想法将非常感激。感谢
static String getText(File pdfFile) throws IOException {
PDDocument doc = PDDocument.load(pdfFile);
return new PDFTextStripper().getText(doc);
}
static void countImages(File pdfFile) throws IOException{
PDDocument doc = PDDocument.load(pdfFile);
List myObjects = doc.getDocument().getObjects();
System.out.println("Count: " + myObjects.size());
doc.close();
}
答案 0 :(得分:1)
快速而肮脏的解决方案可能如下所示:
static void countImages(File pdfFile) throws IOException{
PDDocument doc = PDDocument.load(pdfFile);
PDResources res = doc.getDocumentCatalog().getPages().getResources();
int numImg = 0;
for (PDXObject xobject : res.getXObjects().values()) {
if (xobject instanceof PDXObjectImage) {
numImg++;
}
}
System.out.println("Count: " + numImg);
doc.close();
}
答案 1 :(得分:1)
这应该可以解决问题:
public static void main(String[] args) throws IOException {
PDDocument document = PDDocument.load(new File(""));
int numImages = 0;
for (int i = 0; i < document.getNumberOfPages(); i++)
{
PDPage page = document.getPage(i);
CountImages countImages = new CountImages(page);
countImages.processPage(page);
numImages += countImages.numImages;
}
System.out.println(numImages);
}
static class CountImages extends PDFGraphicsStreamEngine {
public int numImages = 0;
private final Set<COSStream> duplicates = new HashSet<>();
protected CountImages(PDPage page) throws IOException
{
super(page);
}
@Override
public void appendRectangle(Point2D pd, Point2D pd1, Point2D pd2, Point2D pd3) throws IOException {
}
@Override
public void drawImage(PDImage pdImage) throws IOException {
if (pdImage instanceof PDImageXObject) {
PDImageXObject xobject = (PDImageXObject)pdImage;
if (duplicates.contains(xobject.getCOSObject()) == false) {
numImages++;
duplicates.add(xobject.getCOSObject());
}
} else {
numImages++; //means its an inline image
}
}
@Override
public void clip(int i) throws IOException {
}
@Override
public void moveTo(float f, float f1) throws IOException {
}
@Override
public void lineTo(float f, float f1) throws IOException {
}
@Override
public void curveTo(float f, float f1, float f2, float f3, float f4, float f5) throws IOException {
}
@Override
public Point2D getCurrentPoint() throws IOException {
return new Point2D.Float(0, 0);
}
@Override
public void closePath() throws IOException {
}
@Override
public void endPath() throws IOException {
}
@Override
public void strokePath() throws IOException {
}
@Override
public void fillPath(int i) throws IOException {
}
@Override
public void fillAndStrokePath(int i) throws IOException {
}
@Override
public void shadingFill(COSName cosn) throws IOException {
}
}