user1130403
user1130403

Reputation: 45

iText Font Strategy

I have a web application that will generate a PDF from data and allow a user to download. We have a custom font so I am registering four font files (regular, bold, italic, and bold italic ttf files).

  1. Is this necessary? Can I just use the regular font file and set the weight/styles through the font class or do I need all the files? It looks like it works ok but I am still learning.

  2. What is a good strategy or pattern for managing these different fonts to put into my documents? Has anyone done much with this from a Java perspective?

Upvotes: 0

Views: 160

Answers (1)

Bruno Lowagie
Bruno Lowagie

Reputation: 77528

You're not telling us how you're registering your fonts, nor how you're using them, so let me tell you how I would work (I'm the original developer of iText). If you're using those 4 fonts, the best way is to register them like this:

BaseFont bfRegular = BaseFont.createFont(regularTTF, BaseFont.IDENTITY_H, BaseFont_EMBEDDED);
BaseFont bfBold = BaseFont.createFont(boldTTF, BaseFont.IDENTITY_H, BaseFont_EMBEDDED);
BaseFont bfItalic = BaseFont.createFont(italicTTF, BaseFont.IDENTITY_H, BaseFont_EMBEDDED);
BaseFont bfBoldItalic = BaseFont.createFont(boldItalicTTF, BaseFont.IDENTITY_H, BaseFont_EMBEDDED);

I'd use these BaseFont instances whenever adding content using low-level operations.

Then I would do:

Font fRegular = new Font(bfRegular, 12);
Font fBold = new Font(bfBold, 12);
Font fItalic = new Font(bfItalic, 12);
Font fBoldItalic = new Font(bfBoldItalic, 12);

I'd use these fonts whenever I need to create a high-level object, such as a Paragraph.

Of course, I may also need something like this:

Font fRegularSmall = new Font(bfRegular, 9);
Font fBoldSmall = new Font(bfBold, 9);
Font fItalicSmall = new Font(bfItalic, 9);
Font fBoldItalicSmall = new Font(bfBoldItalic, 9);
Font fRegularBig = new Font(bfRegular, 20);
Font fBoldBig = new Font(bfBold, 20);
Font fItalicBig = new Font(bfItalic, 20);
Font fBoldItalicBig = new Font(bfBoldItalic, 20);

Usually, I create a helper class in which I create the BaseFont objects only once (they need to be reused), and I create getters for the Font objects.

Upvotes: 1

Related Questions