3.14.  Form Fields - Text Overflow

Overview

Some projects create PDF documents with empty fields as placeholders which are later filled with text. But if the text is longer than the available size of the field, the excessive text is placed outside the field and can't be seen on the page. Decreasing the font size is rarely an acceptable solution for that problem.

Verifying if text fits in a field is not easy because the required space depends not only on the font size, but also on the length of words at the end of each line and whether hyphenation is used. Following requests by various users, PDFUnit now provides these two methods:

// Fit(nes)-test for one field:
.hasField(..).withoutTextOverflow()

// Verifying that all fields are big enough:
.hasFields().allWithoutTextOverflow()

Important: only text fields are checked. Buttons, lists, combo boxes, signature fields and password fields are not validated.

Correct Size of a Field

The screenshot shows the form fields of a PDF document and their content used in the next example. It is easy to see that the text in the last three fields does not fit:

And this is the test for the last field of the document from the previous picture:

@Test(expected=PDFUnitValidationException.class)
public void hasField_WithoutTextOverflow_Fieldname() throws Exception {
  String filename = "documentUnderTest.pdf";
  String fieldname = "Textfield, too much text, multiline:";

  AssertThat.document(filename)
            .hasField(fieldname)
            .withoutTextOverflow()
  ;
}

If you do not declare the expected exception, the following message appears: Content of field 'Textfield, too much text, multiline:' of 'C:\...\fieldSizeAndText.pdf' does not fit in the available space.

When the method hasField(..) is used for non-text fields, no exception is thrown.

Correct Size of all Fields

If a document has many fields, it would be time-consuming to write a test for every single field. Therefore, all fields can be checked for text overflow using one method:

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

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

Also for this test: only textfields are checked, but not button fields, lists, combo-boxes, signature- or password fields.