PDFUnit in .Net - Some Examples with C# (.Net)
PDFUnit can test visible and invisible parts of a PDF document. Text from a PDF page can be processed as text or as a rendered image.
Furthermore, it is possible to compare many properties of a PDF document against a reference document.
The following examples show a small part of the capabilities.
Contents in a page region of a given page
[TestMethod]
public void hasTextOnFirstPage()
{
String filename = "document-under-test.pdf";
int upperLeftX = 17;
int upperLeftY = 45;
int width = 60;
int height = 9;
PageRegion region = new PageRegion(upperLeftX, upperLeftY, width, height);
AssertThat.document(filename)
.restrictedTo(Constants.FIRST_PAGE)
.restrictedTo(region)
.hasText()
.containing("on first")
;
}
Compare page regions of two documents as text
The following example compares the text of the given region of a PDF under test with the text of the same region of a reference document. The documents should have the same text.
[TestMethod]
public void compareText_InClippingArea()
{
String documentUnderTest = "document-under-test.pdf";
String reference = "reference.pdf";
int upperLeftX = 0;
int upperLeftY = 0;
int width = 210;
int height = 50;
PageRegion headerRegion = new PageRegion(upperLeftX, upperLeftY, width, height);
AssertThat.document(documentUnderTest)
.and(reference)
.restrictedTo(Constants.EVERY_PAGE)
.restrictedTo(headerRegion)
.haveSameText()
;
}
}
Compare page regions of two documents as rendered images
PDFUnit can compare both the text, as well as a rendered page with a reference document. The following example shows such a comparison which is also limited to the header.
[TestMethod]
public void haveSameAppearanceInRegion()
{
String documentUnderTest = "document-under-test.pdf";
String reference = "reference.pdf";
int upperLeftX = 0;
int upperLeftY = 0;
int width = 210;
int height = 50;
PageRegion headerRegion = new PageRegion(upperLeftX, upperLeftY, width, height);
AssertThat.document(documentUnderTest)
.and(reference)
.restrictedTo(Constants.EVERY_PAGE)
.restrictedTo(headerRegion)
.haveSameAppearance()
;
}
Content of a QR code
QR codes are more and more part of documents. For the validation of its content PDFUnit provides appropriate methods.
[TestMethod]
public void validateTextInQRCode()
{
String pdfUnderTest = PATH + "qrcode/qrCode-createdByZXing.pdf";
String expectedText = "hello, world";
int upperLeftX = 10;
int upperLeftY = 65;
int width = 40;
int height = 40;
PageRegion pageRegion = new PageRegion(upperLeftX, upperLeftY, width, height);
AssertThat.document(pdfUnderTest)
.restrictedTo(Constants.FIRST_PAGE)
.restrictedTo(pageRegion)
.hasImage()
.withQRCode()
.containing(expectedText)
;
}
Contents in ZUGFeRD data
Often, the invisible ZUGFeRD data and the visible data of a PDF document should be the same. PDFUnit provides appropriate test methods for this requirement. The next example validates that the International Bank Account Number (IBAN) in the ZUGFeRD data is the same as the IBAN in a given region of the first page of a document:
[TestMethod]
public void validateIBANInZugferdData()
{
String filename = PATH + "zugferd10/ZUGFeRD_1p0_BASIC_Einfach.pdf";
String expectedIBAN = "DE08700901001234567890";
XMLNode nodeIBAN = new XMLNode("ram:IBANID", expectedIBAN);
int ibanUpperLeftX = 80; // in millimeter
int ibanUpperLeftY = 175;
int ibanWidth = 60;
int ibanHeight = 9;
PageRegion regionIBAN = new PageRegion(ibanUpperLeftX, ibanUpperLeftY, ibanWidth, ibanHeight);
AssertThat.document(filename)
.hasZugferdData()
.withNode(nodeIBAN)
;
AssertThat.document(filename)
.restrictedTo(Constants.FIRST_PAGE)
.restrictedTo(regionIBAN)
.hasText()
.containing(expectedIBAN, WhitespaceProcessing.IGNORE)
;
}
More examples
All features of PDFUnit can be found in the manual of PDFUnit-Java. The manual of PDFUnit-.NET describes a lot of examples, but not all. The reason for that is, to reduce the time for documentation. The names of the methods in Java are the same as in .NET and the Syntax of C# is mostly identical to Java.
Both manuals can be downloaded as PDF.