3

I am using iText 2.1.2 to generate PDF. I am using the java.awt.Graphics2D to draw objects in the PDF. I am also using the java.awt.font to set fonts.

I would like to embed the font into the PDF. Is is possible to embed the java.awt.font into the PDF?

Alexis Pigeon
  • 7,423
  • 11
  • 39
  • 44

1 Answers1

2

As Alexis wrote in his comment, you really shouldn't use iText 2.1.2 anymore. The solution that works for more recent versions of iText may work, but this doesn't solve all problems related to old iText versions.

This being said, you could solve this problem in recent iText versions, but using a FontMapper.

Suppose that dir is a directory where you have stored the font programs of the fonts you want to use (.ttf-files, .otf-files,...). In that case you could use the DefaultFontMapper like this:

DefaultFontMapper mapper = new DefaultFontMapper();
mapper.insertDirectory(dir);
Graphics2D g2 = new PdfGraphics2D(canvas, 600, 60, mapper);

If you read chapter 14 of my book, you'll notice that you can hit a couple of problems.

  • The name of the font needs to match,
  • Not every font program will be embedded automatically (only Type1 fonts for which you have an .afm as well as a .pfb file).

You can solve these problems by looking at some of the examples.

For instance: this maps the name MS Gothic (used when creating a Java font) to the corresponding font program (in this case, a specific font in a TrueType collection):

DefaultFontMapper mapper = new DefaultFontMapper();
BaseFontParameters parameters = new BaseFontParameters("c:/windows/fonts/msgothic.ttc,1");
parameters.encoding = BaseFont.IDENTITY_H;
mapper.putName("MS PGothic", parameters );

As we've used IDENTITY_H as encoding, the characters will be stored in Unicode and a subset of the font will be embedded.

You can also create your own FontMapper implementation, for instance:

FontMapper arialuni = new FontMapper() {
    public BaseFont awtToPdf(Font font) {
        try {
            return BaseFont.createFont(
                    "c:/windows/fonts/arialuni.ttf",
                    BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
        } catch (DocumentException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
    public Font pdfToAwt(BaseFont font, int size) {
        return null;
    }
};
Graphics2D g2 = new PdfGraphics2D(canvas, 300, 150, arialuni);

Now it doesn't matter which java.awt.Font you're using: all fonts will be mapped to MS Arial Unicode and the font will be embedded (BaseFont.EMBEDDED).

These are only some examples. There are more on the official web site and in the book.

As I said before, this may work in iText 2.1.2, but if you take pride in what you do and if you value our customer, you'll upgrade to a more recent version of iText.

Pont
  • 333
  • 3
  • 12
Bruno Lowagie
  • 75,994
  • 9
  • 109
  • 165
  • What did you try? For instance: Helvetica is never embedded, nor are CJK fonts (for obvious reasons that may not be obvious to you). – Bruno Lowagie May 05 '14 at 08:00
  • We created a test program using itextpdf-5.4.0.jar. But the font are not embedded. We are using AttributedString & TextLayout to draw the text. We are using AttributedString to set the RTL & LTR for text. graphics2D.setFont(graphicsFont); AttributedString attStr = new AttributedString("Hello World"); attStr.addAttribute(TextAttribute.FONT, graphicsFont.getFont("Verdana")); TextLayout textLayout = new TextLayout(attStr.getIterator(), graphics2D.getFontRenderContext()); textLayout.draw(graphics2D, 5, 24); – user3585044 May 05 '14 at 08:03
  • Did you follow the recommendations as described in chapter 14 of my book? You'll see that combining fonts and `Graphics2D` isn't as trivial as you might think. Chapter 14 explains some pitfalls. – Bruno Lowagie May 05 '14 at 08:39
  • We tried using iText5.4.0 & code suggested by Bruno above & graphics2D.drawString now the fonts are getting embedded but the Arabic text is not displayed properly. If we use TextLayout to draw the Arabic text is displayed properly but the fonts are not embedded. Any suggestions is appreciated. – user3585044 May 21 '14 at 06:43
  • 1
    What you say is true and not true at the same time. You say the font isn't embedded, but... there is no font in your file if you use the `TextLayout` approach. Actually: there is no text in your file! All the glyphs are drawn using path construction and path painting operators. This means that you can view the PDF on any device, even if it doesn't have any Arabic fonts. This also means that nobody can copy/paste or extract the text, and that the file size can be huge. That's all explained in my book, isn't it? – Bruno Lowagie May 21 '14 at 07:54