Out of the box PdfDocument supports the following fonts:
The first three fonts have four typefaces each,"normal", "bold", "oblique" and "bold oblique". This gives us a set of fonts known as "14 Standard PDF fonts".
The PdfFont class describes a particular font and its constructor accepts the following arguments:
The following instance is the default document font in terms of PdfDocument:
import * as wjPdf from '@grapecity/wijmo.pdf';
new wjPdf.PdfFont("times", 10, "normal", "normal");
Using the font in PdfDocument, the font can be specified in two ways:
The drawText method accepts an options? object, and the PdfFont instance can be passed through this oject via the font property.
doc.drawText("Lorem", null, null, {
font: new wjPdf.PdfFont("times", 10, "normal", "bold")
});
You can also cahnge the default document font using the setFont method. This must be done before drawing text to the document using drawText method. This approach is useful when a lot of text with the same font is needed to be drawn.
doc.setFont(new wjPdf.PdfFont("times", 10, "normal", "bold"));
doc.drawText("Lorem");
If font with exact given style and weight properties is not found, then the closest registered font will be used.
This sample shows how to use drawText and setFont methods to draw text using different fonts.
Along with use of the standard fonts, PdfDocument also provides the ability to embed custom fonts into document. The following font formats are supported:
First, a custom font must be registered using the registerFont or registerFontAsync method. This method loads font from a URL and associates it with a specified font family name and font attributes (like the style, weight, whether it is a serif or sans serif font, etc). After that the font family name can be used in association with PdfFont class just like any other standard font.
In the example below, the Fira font family is used and two fonts, FiraSans-Regular.ttf and FiraSans-Bold.ttf, are embedded into the document.
Example:
doc.registerFont({
source: "resources/fonts/fira/FiraSans-Regular.ttf",
name: "fira",
style: "normal",
weight: "normal",
sansSerif: true
});
doc.drawText("Here is the FiraSans-Regular font.", null, null, {
// an equivalent of font: new wjPdf.PdfFont("fira", 10, "normal", "normal")
font: new wjPdf.PdfFont("fira")
});