How to write a Zapfdingbats character at a specific location on a page?

Tags: fontsiText 7

I want to put a check mark using Zapfdingbats on a specific location in my PDF document.

What I achieved so far is this: I can show the check mark but it's on the side of the document and not on the specific X, Y coordinate that I want it to be.

Posted on StackOverflow on May 4, 2013 by δοῦλος Ἰησοῦς Χριστός

Let's start with a PdfFont object that knows how to draw a Zapfdingbats character:

PdfFont zapfdingbats = PdfFontFactory.createFont(FontConstants.ZAPFDINGBATS);

Once you have a PdfFont object, you can create a Paragraph:

Paragraph p = new Paragraph(zapfstring).setFont(zapfdingbats);

Where zapfstring is a string containing any Zapfdingbats character you want.

To add this text at an absolute position, you can use the showTextAligned() method and PdfWriter's direct content:

PdfCanvas pdfCanvas = new PdfCanvas(pdfDoc.getPage(pageNumber));
new Canvas(pdfCanvas, pdfDoc, page.getPageSize())
    .showTextAligned(p, 200, 500, pdfDoc.getPageNumber(page),
            TextAlignment.CENTER, VerticalAlignment.MIDDLE, 0);

Where 200 and 500 are an X and Y coordinate and 0 is an angle expressed in degrees. Instead of TextAlignment.CENTER, you can also choose TextAlignment.LEFT, TextAlignment.RIGHT or TextAlignment.JUSTIFIED.

Click this link if you want to see how to answer this question in iText 5.