3.3.  Attachments

Overview

Files that are embedded in PDF documents, often play an important role in post-processing steps. Therefore PDFUnit provides test methods for these attachments:

// Simple tests:
.hasEmbeddedFile()
.hasNumberOfEmbeddedFiles(..) 

// More detailed tests:
.hasEmbeddedFile().withContent(..) 
.hasEmbeddedFile().withName(..)

The following tests are using umsatzsteuervoranmeldung-2010.pdf, a PDF form for the German sales tax return of 2010. It contains a file named PrePress-Qualität.joboptions.

Existence of Attachments

A very simple test is to check whether an embedded file exists:

@Test
public void hasEmbeddedFile() throws Exception {
  String filename = "umsatzsteuervoranmeldung-2010.pdf";
  
  AssertThat.document(filename)
            .hasEmbeddedFile()
  ;
}

Number of Attachments

The next test verifies the expected number of embedded files:

@Test
public void hasNumberOfEmbeddedFiles() throws Exception {
  String filename = "documentUnderTest.pdf";
  
  AssertThat.document(filename)
            .hasNumberOfEmbeddedFiles(1)
  ;
}

Filename

Also, the names of embedded files can be tested:

@Test
public void hasEmbeddedFile_WithName() throws Exception {
  String filename = "documentUnderTest.pdf";
  
  AssertThat.document(filename)
            .hasEmbeddedFile().withName("PrePress-Qualität.joboptions")
  ;
}

Content

And finally, the content of an embedded file can be compared with the content of an external file:

@Test
public void hasEmbeddedFile_WithContent() throws Exception {
  String filename = "documentUnderTest.pdf";
  String embeddedFileName = "embeddedfiles/PrePress-Qualität.joboptions";
  
  AssertThat.document(filename)
            .hasEmbeddedFile().withContent(embeddedFileName)
  ;
}

The comparison is carried out byte-wise. The parameter can be a filename or an instance of java.util.File.

Multiple Attachments

The next example refers to the file kubrick_dvds.pdf, an iText sample. Adobe Reader® shows the attachments:

You can check for multiple attachments in one test. But select a better name for such a test than the name of the test in the next example:

@Test
public void hasEmbeddedFile_MultipleInvocation() throws Exception {
  String filename = "kubrick_dvds.pdf";
  String embeddedFileName1 = "0048254.jpg";
  String embeddedFileName2 = "0049406.jpg";
  String embeddedFileName3 = "0050825.jpg";
  
  AssertThat.document(filename)
            .hasEmbeddedFile().withName(embeddedFileName1)
            .hasEmbeddedFile().withName(embeddedFileName2)
            .hasEmbeddedFile().withName(embeddedFileName3)
  ;
}

If embedded files are not available as separate files, they can be extracted from an existing PDF with the utility ExtractEmbeddedFiles. This program is described in detail in chapter 9.4: “Extract Attachments”: