FilenameFooterWriter { public void addFooter(PDPage page, String fileName, long pageNumber) throws TaskException { if (addFooter) { try { String truncatedFilename = truncateIfRequired(fileName, maxWidth(page, pageNumber)); writer.write(page, HorizontalAlign.LEFT, VerticalAlign.BOTTOM, truncatedFilename, FONT, FONT_SIZE, Color.BLACK); writer.write(page, HorizontalAlign.RIGHT, VerticalAlign.BOTTOM, Long.toString(pageNumber), FONT, FONT_SIZE, Color.BLACK); } catch (TaskIOException | IOException e) { throw new TaskException("Unable to write the page footer", e); } } } FilenameFooterWriter(boolean addFooter, PDDocument document); void addFooter(PDPage page, String fileName, long pageNumber);  }
@Test public void write() throws TaskException { PDDocument doc = new PDDocument(); PDPage page = new PDPage(); doc.addPage(page); new FilenameFooterWriter(true, doc).addFooter(page, "My Footer", 20); assertThat(new PdfTextExtractorByArea().extractFooterText(page).trim(), is("My Footer 20")); }
@Test public void write_long_filename_that_needs_truncation() throws TaskException { PDDocument doc = new PDDocument(); PDPage page = new PDPage(); doc.addPage(page); new FilenameFooterWriter(true, doc).addFooter(page, "My very long title that will not fit on the page and needs to be truncated so that it will not overflow and cover the page number and generally look not so nice", 20); assertPageFooterText(page,"My very long title that will not fit on the page and needs to be truncated so that it will not overflow and cover the page number a20"); }
@Test public void dontWrite() throws TaskException { PDDocument doc = new PDDocument(); PDPage page = new PDPage(); doc.addPage(page); new FilenameFooterWriter(false, doc).addFooter(page, "My Footer", 20); assertThat(new PdfTextExtractorByArea().extractFooterText(page).trim(), isEmptyOrNullString()); }
@Test public void write_filename_contains_bad_characters() throws TaskException { PDDocument doc = new PDDocument(); PDPage page = new PDPage(); doc.addPage(page); String withBadCharacter = "This is a bad \uF021character"; FilenameFooterWriter writer = new FilenameFooterWriter(true, doc); writer.addFooter(page, withBadCharacter, 1); assertPageFooterText(page, "This is a bad #character 1"); }