frIT
frIT

Reputation: 3305

Drawing glyph from ZapfDingbats using PDFbox

I am trying to draw a checkmark (found in the PDF standard ZapfDingbats font, Unicode 2714) in my PDF document. I'm a newbie to Apache's PDFBox, using version 2.0.0 at the moment (no specific reason except that it's the newest).

My code looks as follows:

PDDocument document = PDDocument.load(new File("myfile.pdf"));
PDPage page = document.getPages().get(0); // first page
PDPageContentStream contentStream = new PDPageContentStream(document, page, AppendMode.APPEND, true);

PDType1Font font = PDType1Font.ZAPF_DINGBATS;
String glyph = "\u2714";

contentStream.beginText();
contentStream.setFont(font, fontSize);
contentStream.newLineAtOffset(10, 10); // towards lower left corner of page
contentStream.showText(glyph);
contentStream.endText();
contentStream.close();

document.save("output.pdf");
document.close();

... but this produces a nice Exception:

Exception in thread "main" java.lang.IllegalArgumentException: U+2714 ('a20') is not available in this font's encoding: WinAnsiEncoding
    at org.apache.pdfbox.pdmodel.font.PDType1Font.encode(PDType1Font.java:345)
    at org.apache.pdfbox.pdmodel.font.PDFont.encode(PDFont.java:286)
    :

Debugging through the code shows that what happens at PDType1Font.java:345 is:

  1. (PDType1Font extends PDSimpleFont)
  2. PDSimpleFont.glyphList correctly contains a mapping from the Unicode codepoint (U+2714) to a PDF name ("a20") as shown in the Exception text (set up in PDSimpleFont's constructor for the ZapfDingbat glyphs).
  3. ... but the PDSimpleFont.encoding, which is set to WinAnsiEncoding in PDType1Font's constructor line 110, does not contain the name a20 - these names (encodings) are set up statically in the WinAnsiEncoding class - see the WIN_ANSI_ENCODING_TABLE constant at line 36.

Has anyone managed showing Dingbat glyphs using PDFbox - even if in a lower version?

Upvotes: 2

Views: 1995

Answers (1)

Tilman Hausherr
Tilman Hausherr

Reputation: 18916

I suspect it is a bug (a20 should be converted to 064 according to "ZapfDingbats Set and Encoding" and I can't find where this is being done), please open an issue in JIRA. In the meantime, here's a workaround if you're using windows:

instead of

PDType1Font font = PDType1Font.ZAPF_DINGBATS;

use

PDFont font = PDType0Font.load(document, new File("c:/windows/fonts/arialuni.ttf"));

Update: now solved

This was indeed found to be a bug and JIRA issue PDFBOX-3298 addressed this. It is now resolved in PDFBox version 2.0.3.

Upvotes: 1

Related Questions