3.22.  Number of PDF Elements

Overview

Not only the number of pages can be a test goal, also any kind of countable items in a PDF document, e.g. form fields and bookmarks. The following list shows the items that are countable and therefore testable:

// Test counting parts of a PDF:
.hasNumberOfActions(..)             
.hasNumberOfBookmarks(..)          
.hasNumberOfDifferentImages(..)    1
.hasNumberOfEmbeddedFiles(..) 
.hasNumberOfFields(..)                  
.hasNumberOfFonts(..) 
.hasNumberOfLayers(..) 
.hasNumberOfOCGs(..) 
.hasNumberOfPages(..)              2
.hasNumberOfSignatures(..) 
.hasNumberOfVisibleImages(..)      3

1 3

Tests for the number of images are described in chapter 3.16: “Images in PDF Documents”.

2

Tests for the number of pages are described in chapter 3.23: “Page Numbers as Objectives”.

Examples

Validating the number of items in PDF documents works identically for all items. So, only two of them are shown as examples:

@Test
public void hasNumberOfFields() throws Exception {
  String filename = "documentUnderTest.pdf";
  
  AssertThat.document(filename)
            .hasNumberOfFields(4)
  ;
}
@Test 
public void hasNumberOfBookmarks() throws Exception {
  String filename = "documentUnderTest.pdf";
  
  AssertThat.document(filename)
            .hasNumberOfBookmarks(19)
  ; 
}

All tests can be concatenated:

@Test
public void testHugeDocument_MultipleInvocation() throws Exception {
  String filename = "documentUnderTest.pdf";
  
  AssertThat.document(filename)
            .hasNumberOfPages(1370)
            .hasNumberOfBookmarks(565)
            .hasNumberOfActions(4814)
            .hasNumberOfEmbeddedFiles(0)
  ;
}

Fortunately, this test takes only 2.5 seconds for a document with 1370 pages on a contemporary notebook.