Chapter 12. PDFUnit for non-XML Systems

12.1.  A quick Look at PDFUnit-Java

The first implementation of PDFUnit was PDFUnit-Java. It is the reference for implementations in other programming languages. Whenever it is possible, the keywords in all implementations are chosen to be the same as in PDFUnit-Java.

The following examples shows that the API follows the Fluent Interface (http://de.wikipedia.org/wiki/Fluent_Interface):

@Test
public void hasTextOnFirstPageInClippingArea() throws Exception {
  String filename = PATH + "content/documentForTextClipping.pdf";
  
  int upperLeftX  =  50;
  int upperLeftY  = 130;
  int width       = 170;
  int height      =  25; 
  ClippingArea inClippingArea = new ClippingArea(upperLeftX, upperLeftY, width, height);
  
  AssertThat.document(filename)
            .hasText(ON_FIRST_PAGE, inClippingArea)
            .containing("Content on first page") 
  ;
}
@Test
public void compareFields() throws Exception {
  String filenameTest = PATH + "acrofields/test.pdf";
  String filenameMaster = PATH + "acrofields/master.pdf";
  
  AssertThat.document(filenameTest)
            .and(filenameMaster)
            .haveSameFieldsByName()
            .haveSameFieldsByProperties()
            .haveSameFieldsByValue()
  ;
}
@Test
public void hasSignature() throws Exception {
  String filename = PATH + "signed/sampleSignedPDFDocument.pdf";
  String xpath = "//signature[@name='Signature2']";
  
  XPathExpression expression = new XPathExpression(xpath);
  AssertThat.document(filename)
            .hasSignatures()
            .matchingXPath(expression)
  ;
}

A detailed documentation of PDFUnit-Java is available from http://www.pdfunit.com/en/documentation/java/index.html.