Files that are embedded in PDF documents, often play an important role in post-processing steps. Therefore PDFUnit provides test for these attachments:
<!-- Tags to test embedded files: --> <hasNumberOfEmbeddedFiles /> <hasEmbeddedFile name=".." (one of the two content=".." attributes is required) /> <hasEmbeddedFileContent />
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”.
A very simple test is to check whether an embedded file exists:
<testcase name="hasEmbeddedFile"> <assertThat testDocument="embeddedfiles/umsatzsteuervoranmeldung-2010.pdf"> <hasEmbeddedFile /> </assertThat> </testcase>
The next test verifies the expected number of embedded files:
<testcase name="hasNumberOfEmbeddedFiles"> <assertThat testDocument="embeddedfiles/umsatzsteuervoranmeldung-2010.pdf"> <hasNumberOfEmbeddedFiles>1</hasNumberOfEmbeddedFiles> </assertThat> </testcase>
Also the names of embedded files can be tested:
<testcase name="hasEmbeddedFile_WithName"> <assertThat testDocument="embeddedfiles/umsatzsteuervoranmeldung-2010.pdf"> <hasEmbeddedFile name="PrePress-Qualität.joboptions" /> </assertThat> </testcase>
And finally, the content of an embedded file can be compared with the content of an external file:
<testcase name="hasEmbeddedFile_WithContent"> <assertThat testDocument="embeddedfiles/umsatzsteuervoranmeldung-2010.pdf"> <hasEmbeddedFile content="embeddedfiles/PrePress-Qualität.joboptions" /> </assertThat> </testcase>
The comparison is carried out byte-wise.
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”:
Multiple filenames can be tested with one test.
<testcase name="hasEmbeddedFile_MultipleInvocation"> <assertThat testDocument="embeddedfiles/kubrick_dvds.pdf"> <hasEmbeddedFile name="0048254.jpg" /> <hasEmbeddedFile name="0049406.jpg" /> <hasEmbeddedFile name="0050825.jpg" /> </assertThat> </testcase>
The next example refers to the file “kubrick_dvds.pdf”, an iText sample. Adobe Reader® shows the attachments: