Prev |  | Next

## 3.15. Format

### Overview

You need to check that the intended format was created successfully: A4-landscape, Letter-portrait or a special format for a poster? You think testing such simple thing is a waste of time? But have you ever printed a “LETTER”-formatted document on an “A4”-printer? It is possible, but... So, PDFUnit provides the following test method:

Deshalb stellt PDFUnit eine Testmethode für Formattests zur Verfügung:

PDFUnit provides one flexible method to test document formats:

    // Simple tests for page formats:
    .hasFormat(..)

### Documents with Same Page Format

You can use predefined constants to verify the format of conventional PDF documents, each page having the same size:

    import static com.pdfunit.Constants.*;
    ...
    @Test
    public void hasFormat_A4Landscape() throws Exception {
      String filename = "documentUnderTest.pdf";

      AssertThat.document(filename)
                .hasFormat(A4_LANDSCAPE)
      ;
    }

    import static com.pdfunit.Constants.*;
    ...
    @Test
    public void hasFormat_LetterPortrait() throws Exception {
      String filename = "documentUnderTest.pdf";

      AssertThat.document(filename)
                .hasFormat(LETTER_PORTRAIT)
      ;
    }

| Many popular formats are defined in `com.pdfunit.Constants`

You can also verify individual formats:

    @Test
    public void hasFormat_FreeFormat_1117x836_mm() throws Exception {
      String filename = "documentUnderTest.pdf";
      int heightMM = 1117;
      int widthMM = 863;
      DocumentFormat formatMM = new DocumentFormatMillis(widthMM, heightMM);

      AssertThat.document(filename)
                .hasFormat(formatMM)
      ;
    }

It is recommended to use the unit millimeters for tests with document formats, although a class DocumentFormatPoints exists. When the unit points is used, the real size of a page depends on the resolution. Two documents with the same number of points can have different page sizes if one document has a resolution of 72 DPI (72 dots per inch) and the other has a resolution of 150 DPI.

All size values are of type integer. If you want to check formats having tenths of a millimeter, round the values. Please note that PDFUnit uses the tolerance defined in DIN 476 when comparing two size values.

Information about paper formats can be found at http://en.wikipedia.org/wiki/Paper_size. PDFUnit uses the stricter tolerances of DIN 476 when comparing page sizes, although the ISO 216 standard defines larger tolerances.

### Formats on Single Pages

A document with different page sizes can also be checked. The next example checks the format of page 3 only:

    @Test
    public void hasFormatOnPage3() throws Exception {
      String filename = "documentUnderTest.pdf";
      PagesToUse page3 = PagesToUse.getPage(3);

      AssertThat.document(filename)
                .restrictedTo(page3)
                .hasFormat(A5_PORTRAIT)
      ;
    }

The restriction of tests to individual pages or page ranges is described in chapter 13.2: “Page Selection”:

Prev | Up | Next
3.14. Form Fields - Text Overflow | Home | 3.16. Images in PDF Documents