3.11.  Form Fields - Text Overflow


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 tests:

<!-- Tags to check field overflow: -->

<!-- Check text overflow for one text field: -->
<hasField ... >
  <withoutTextOverflow />
</hasField ... >

<!-- Check text overflow for all text fields: -->
<hasFields ... >
  <allWithoutTextOverflow />
</hasFields ... >

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:

<testcase name="hasField_WithoutTextOverflow_Fieldname"
  <assertThat testDocument="acrofields/fieldSizeAndText.pdf">
    <hasField withName="Textfield, too much text, multiline:">
      <withoutTextOverflow />

If you didn't expect an exception (by including errorExpected="YES"), this message would appear: Content of field 'Textfield, too much text, multiline:' of 'C:\...\fieldSizeAndText.pdf' does not fit in the available space.

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 tag:

<testcase name="hasFields_AllWithoutTextOverflow">
  <assertThat testDocument="acrofields/javaScriptForFields.pdf">
      <allWithoutTextOverflow />

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

Technical constraint

Due to technical reasons the content of a field is not detectable by the test tag <containing>..</containing>. If you anyhow want to verify it, the PDF document must first be flattened.

Therfore, the following test fails:

  Text from AcroFields (type PdfName.ANNOTS) is not detectable.
  Flatten it first.
<testcase name="hasTextOnFirstPage_DocumentWithFields">
  <assertThat testDocument="&testfile;">
    <hasText on="FIRST_PAGE">
      <inClippingArea upperLeftX="0" upperLeftY="0" width="595" height="842" unit="POINTS">
        <containing whitespaces="NORMALIZE">