PDFBox: Counting the number of images in a document


/ Published in: Java
Save to your folder(s)

Opens a PDF document and counts the number of images used on all pages.


Copy this code and paste it in your HTML
  1. /**
  2.  * @param args the command line arguments
  3.  * @throws java.io.IOException
  4.  */
  5. public static void main(String[] args) throws IOException {
  6. PDDocument document = PDDocument.load(new File(""));
  7.  
  8. int numImages = 0;
  9. for (int i = 0; i < document.getNumberOfPages(); i++)
  10. {
  11. PDPage page = document.getPage(i);
  12.  
  13. CountImages countImages = new CountImages(page);
  14. countImages.processPage(page);
  15.  
  16. numImages += countImages.numImages;
  17. }
  18.  
  19. System.out.println(numImages);
  20. }
  21.  
  22. static class CountImages extends PDFGraphicsStreamEngine {
  23. public int numImages = 0;
  24. private final Set<COSStream> duplicates = new HashSet<>();
  25.  
  26. protected CountImages(PDPage page) throws IOException
  27. {
  28. super(page);
  29. }
  30.  
  31. @Override
  32. public void appendRectangle(Point2D pd, Point2D pd1, Point2D pd2, Point2D pd3) throws IOException {
  33. }
  34.  
  35. @Override
  36. public void drawImage(PDImage pdImage) throws IOException {
  37. if (pdImage instanceof PDImageXObject) {
  38. PDImageXObject xobject = (PDImageXObject)pdImage;
  39.  
  40. if (duplicates.contains(xobject.getCOSObject()) == false) {
  41. numImages++;
  42. duplicates.add(xobject.getCOSObject());
  43. }
  44. } else {
  45. numImages++; //means its an inline image
  46. }
  47. }
  48.  
  49. @Override
  50. public void clip(int i) throws IOException {
  51. }
  52.  
  53. @Override
  54. public void moveTo(float f, float f1) throws IOException {
  55. }
  56.  
  57. @Override
  58. public void lineTo(float f, float f1) throws IOException {
  59. }
  60.  
  61. @Override
  62. public void curveTo(float f, float f1, float f2, float f3, float f4, float f5) throws IOException {
  63. }
  64.  
  65. @Override
  66. public Point2D getCurrentPoint() throws IOException {
  67. return new Point2D.Float(0, 0);
  68. }
  69.  
  70. @Override
  71. public void closePath() throws IOException {
  72. }
  73.  
  74. @Override
  75. public void endPath() throws IOException {
  76. }
  77.  
  78. @Override
  79. public void strokePath() throws IOException {
  80. }
  81.  
  82. @Override
  83. public void fillPath(int i) throws IOException {
  84. }
  85.  
  86. @Override
  87. public void fillAndStrokePath(int i) throws IOException {
  88. }
  89.  
  90. @Override
  91. public void shadingFill(COSName cosn) throws IOException {
  92. }
  93. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.