3.20.  Layout - Entire PDF Pages

Overview

Text in a PDF document has properties such as font size, font color and lines which should be correct before sending it to the customer. Also, paragraphs, alignment of text, images and image descriptions are important parts of the layout. PDFUnit tests these aspects, first rendering the document page by page and then comparing each page:

The following test methods are provided:

// Compare rendered page(s) with a given image. 
// The left-upper corner is 0/0:
.asRenderedPage(..).isEqualToImage(BufferedImage)
.asRenderedPage(..).isEqualToImage(File)
.asRenderedPage(..).isEqualToImage(String fileName)
.asRenderedPage(..).isEqualToImages(BufferedImage[] images)
.asRenderedPage(..).isEqualToImages(File[] imageFiles)
.asRenderedPage(..).isEqualToImages(String[] fileNames)

// Compare rendered page(s) with a given image. 
// The left-upper corner is given:  3.21: “Layout - in Page Regions” 
.asRenderedPage(..).isEqualToImage(upperLeftX, upperLeftY, FormatUnit, BufferedImage)
.asRenderedPage(..).isEqualToImage(upperLeftX, upperLeftY, FormatUnit, File)
.asRenderedPage(..).isEqualToImage(upperLeftX, upperLeftY, FormatUnit, imageFileName)
.asRenderedPage(..).isEqualToImages(upperLeftX, upperLeftY, FormatUnit, BufferedImage)
.asRenderedPage(..).isEqualToImages(upperLeftX, upperLeftY, FormatUnit, File)
.asRenderedPage(..).isEqualToImages(upperLeftX, upperLeftY, FormatUnit, imageFileName)

You can choose a page to be compared. This is described in chapter 13.2: “Page Selection”. Also, a comparison can be limited to a region of a page. That is described in chapter 3.21: “Layout - in Page Regions”.

Example - Compare Preselected Pages as Rendered Images

The following example checks that the pages 1, 3 and 4 look the same as referenced image files:

@Test
public void compareAsRenderedPage_MultipleImages() throws Exception {
  String filename = "documentUnderTest.pdf";
  
  String imagePage1 = "images/documentUnderTest_page1.png";
  String imagePage3 = "images/documentUnderTest_page3.png";
  String imagePage4 = "images/documentUnderTest_page4.png";
  PagesToUse pages134 = PagesToUse.getPages(1, 3, 4);
  
  AssertThat.document(filename)
            .restrictedTo(pages134)
            .asRenderedPage()
            .isEqualToImages(imagePage1, imagePage3, imagePage4)
  ;
}

All image formats which are supported by java.awt.image.BufferedImage can be used, i.e. GIF, PNG, JPEG, BMP and WBMP.