Answer the question
In order to leave comments, you need to log in
Why is the font not applied when creating a PDF document?
Question. I am creating PDF documents with iText. When I try to enable the "Agenda Tabular Light" font, iText ignores my selection. You can verify this on the File > Properties > Fonts tab in Adobe Reader. The PDF shows that the Helvetica font is being used even though that font was not selected. The colors are displayed but not the font.
My code looks like this:
public static final Font FONT_HEADER = FontFactory.getFont(AGENDA_TABULAR_LIGHT, 18, Font.NORMAL, TITLE_COLOR);
// step 1
Document document = new Document();
// step 2
PdfWriter.getInstance(document, new FileOutputStream(filename));
// step 3
document.open();
// step 4: Font font = FontFactory.getFont("Agenda Tabular Light");
System.out.println(font.toString());
document.add(new Phrase("Agenda Tabular Light J j", font));
Font fontbold = FontFactory.getFont("Times-Roman", 12, Font.BOLD);
document.add(new Phrase("Times-Roman, Bold", fontbold));
document.add(Chunk.NEWLINE);
document.close();
Answer the question
In order to leave comments, you need to log in
By default FontProgramFactory (FontFactory
in iText 5) only knows type 1 fonts, and the font "garamond bold"
is not in this list, so Helvetica is used (Helvetica is the default font in iText).
You can "teach" FontProgramFactory
how to find other fonts by registering them. You can try the following:
But this is a very costly operation, because here iText looks for font files in different directories in your operating system (for example, in C:/Windows/Fonts
). This may take a few seconds, and the output will be many more fonts than necessary (it is possible that the desired font will not be registered).
It's better to register fonts like this.
We tell iText where to find the .ttf file ( "c:/windows/fonts/garabd.ttf"
) and define an alias for the desired font ("garamond bold"). Now that the font name is registered, it can be used:
PdfFont myBoldFont = PdfFontFactory.createRegisteredFont("garamond bold");
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question