Comparing text or rendered pages can be restricted to regions of one or more pages. Such an area is defined by four values: the x/y values of the upper left corner, the width and the height:
// Instantiating a page region public PageRegion(int leftX, int upperY, int width, int height) public PageRegion(int leftX, int upperY, int width, int height, FormatUnit init)
Page regions can be defined using millimeters or points. Further information to the units are described in chapter 13.8: “Format Units - Points and Millimeters”.
Here's an example:
@Test public void hasTextOnFirstPage_InPageRegion() throws Exception { String filename = "documentUnderTest.pdf"; int leftX = 17; // in millimeter int upperY = 45; int width = 60; int height = 9; PageRegion pageRegion = new PageRegion(leftX, upperY, width, height); AssertThat.document(filename) .restrictedTo(FIRST_PAGE) .restrictedTo(pageRegion) .hasText() .containing("on first") ; }
It's easy to use a region of a page in a test. But it might be more difficult to find the
right values for the region you need. PDFUnit provides the utility RenderPdfPageRegionToImage
to extract a page section into an image file (PNG). You can use that program using
mm
:
:: :: Render a part of a PDF page into an image file. :: @echo off setlocal set CLASSPATH=./lib/aspectj-1.8.7/*;%CLASSPATH% set CLASSPATH=./lib/commons-logging-1.2/*;%CLASSPATH% set CLASSPATH=./lib/pdfbox-2.0.0/*;%CLASSPATH% set CLASSPATH=./lib/pdfunit-2016.05/*;%CLASSPATH% set CLASSPATH=./build_eclipse;%CLASSPATH% set TOOL=com.pdfunit.tools.RenderPdfPageRegionToImage set PAGENUMBER=1 set OUT_DIR=./tmp set IN_FILE=./content/documentForTextClipping.pdf set PASSWD= :: All values must be millimeter: set UPPERLEFTX=135 set UPPERLEFTY=30 set WIDTH=70 set HEIGHT=30 set PAGEHEIGHT=297 java %TOOL% %IN_FILE% %PAGENUMBER% %OUT_DIR% %UPPERLEFTX% %UPPERLEFTY% %WIDTH% %HEIGHT% %PAGEHEIGHT% %PASSWD% endlocal
The generated image needs to be checked. Does it contain the section you want? If not, change the parameters until they are right. Then you can copy the four values into your test.