6.2.  Does Content Fit in Predefined Form Fields?

Initial Situation

A PDF document is created using a document template with empty fields as place holders for text, e.g. the address of a customer in an advertising letter. At runtime the fields are filled with text.

Problem

The text could be larger than the available size of the field.

Solution Approach

PDFUnit provides methods to detect text overflow.

Solution

@Test
public void noTextOverflow_AllFields() throws Exception {
  String filename = "documentUnderTest.pdf";

  AssertThat.document(filename)
            .hasFields()
            .allWithoutTextOverflow()
  ;
}

A similar test can be done with one field:

@Test
public void noTextOverflow_OneField() throws Exception {
  String filename = "documentUnderTest.pdf";
  String fieldname = "Textfield, text inside, align left:";
  
  AssertThat.document(filename)
            .hasField(fieldname)
            .withoutTextOverflow()
  ;
}

Chapter 3.14: “Form Fields - Text Overflow” describes this subject in detail.