PageTextWriter { public void write(PDPage page, HorizontalAlign hAlign, VerticalAlign vAlign, String rawLabel, PDFont font, Double fontSize, Color color) throws TaskIOException { try { String label = StringUtils.normalizeWhitespace(rawLabel); List<TextWithFont> resolvedStringsToFonts = FontUtils.resolveFonts(label, font, document); float stringWidth = 0.0f; for (TextWithFont stringAndFont : resolvedStringsToFonts) { String s = stringAndFont.getText(); PDFont f = stringAndFont.getFont(); stringWidth += f.getStringWidth(s) * fontSize.floatValue() / 1000f; } PDRectangle pageSize = page.getCropBox().rotate(page.getRotation()); Point2D position = new Point2D.Double(hAlign.position(pageSize.getWidth(), stringWidth, DEFAULT_MARGIN), vAlign.position(pageSize.getHeight(), DEFAULT_MARGIN, fontSize.floatValue())); write(page, position, label, font, fontSize, color); } catch (IOException e) { throw new TaskIOException("An error occurred writing the header or footer of the page.", e); } } PageTextWriter(PDDocument document); void write(PDPage page, HorizontalAlign hAlign, VerticalAlign vAlign, String rawLabel, PDFont font,
            Double fontSize, Color color); void write(PDPage page, Point2D position, String rawLabel, PDFont font, Double fontSize, Color color); static PDColor toPDColor(Color color); void write(PDPage page, Point2D position, String rawLabel, PDFont font, Double fontSize, PDColor color); void write(PDPage page, Point2D position, String rawLabel, PDFont font, Double fontSize, PDColor color,
            RenderingMode renderingMode, boolean fauxItalic); int getStringWidth(String rawLabel, PDFont font, float fontSize); static void writeHeader(PDDocument doc, PDPage page, String text); static final Float DEFAULT_MARGIN; }
@Test public void resolveTextAndFontsWhenTextRepeats() throws TaskException { write("123α456α789"); }
@Test public void resolvedSpaceSeparately() throws TaskException { write("ab cd"); }
@Test(expected = UnsupportedTextException.class) public void throwsWhenCharacterUnsupported() throws TaskException { write("\uFE0F"); }