Package com.itextpdf.kernel.font
Class PdfFontFactory
java.lang.Object
com.itextpdf.kernel.font.PdfFontFactory
This class provides helpful methods for creating fonts ready to be used in a
PdfDocument
Note, just created PdfFont is almost empty until it will be flushed, because it is impossible to fulfill font data until flush.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic enumEnum values for font embedding strategies. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic PdfFontCreates a new instance of default font, namelyStandardFonts.HELVETICAstandard font withPdfEncodings.WINANSIencoding.static PdfFontcreateFont(byte[] fontProgram, PdfFontFactory.EmbeddingStrategy embeddingStrategy) Created aPdfFontinstance by the bytes of the underlying font program.static PdfFontcreateFont(byte[] fontProgram, String encoding) Created aPdfFontinstance by the bytes of the underlying font program.static PdfFontcreateFont(byte[] fontProgram, String encoding, PdfFontFactory.EmbeddingStrategy embeddingStrategy) Created aPdfFontinstance by the bytes of the underlying font program.static PdfFontcreateFont(byte[] fontProgram, String encoding, PdfFontFactory.EmbeddingStrategy embeddingStrategy, boolean cached) Created aPdfFontinstance by the bytes of the underlying font program.static PdfFontcreateFont(FontProgram fontProgram) Created aPdfFontinstance given the given underlyingFontPrograminstance.static PdfFontcreateFont(FontProgram fontProgram, String encoding) Created aPdfFontinstance given the given underlyingFontPrograminstance.static PdfFontcreateFont(FontProgram fontProgram, String encoding, PdfFontFactory.EmbeddingStrategy embeddingStrategy) Created aPdfFontinstance given the given underlyingFontPrograminstance.static PdfFontcreateFont(PdfDictionary fontDictionary) Creates aPdfFontby already existing font dictionary.static PdfFontcreateFont(String fontProgram) Creates aPdfFontinstance by the path of the font program filestatic PdfFontcreateFont(String fontProgram, PdfFontFactory.EmbeddingStrategy embeddingStrategy) Created aPdfFontinstance given the path to the font file.static PdfFontcreateFont(String fontProgram, String encoding) Creates aPdfFontinstance by the path of the font program file and given encoding.static PdfFontcreateFont(String fontProgram, String encoding, PdfFontFactory.EmbeddingStrategy embeddingStrategy) Created aPdfFontinstance given the path to the font file.static PdfFontcreateFont(String fontProgram, String encoding, PdfFontFactory.EmbeddingStrategy embeddingStrategy, boolean cached) Created aPdfFontinstance given the path to the font file.static PdfFontcreateFont(String fontProgram, String encoding, PdfFontFactory.EmbeddingStrategy embeddingStrategy, PdfDocument cacheTo) Creates aPdfFontinstance by the path of the font program file and given encoding and place it inside thePdfDocument.static PdfFontcreateFont(String fontProgram, String encoding, PdfDocument cacheTo) Creates aPdfFontinstance by the path of the font program file and given encoding and place it inside thePdfDocument.static PdfFontcreateRegisteredFont(String fontName) CreatesPdfFontbased on registeredFontProgram's.static PdfFontcreateRegisteredFont(String fontName, String encoding) CreatesPdfFontbased on registeredFontProgram's.static PdfFontcreateRegisteredFont(String fontName, String encoding, PdfFontFactory.EmbeddingStrategy embeddingStrategy) CreatesPdfFontbased on registeredFontProgram's.static PdfFontcreateRegisteredFont(String fontName, String encoding, PdfFontFactory.EmbeddingStrategy embeddingStrategy, boolean cached) CreatesPdfFontbased on registeredFontProgram's.static PdfFontcreateRegisteredFont(String fontName, String encoding, PdfFontFactory.EmbeddingStrategy embeddingStrategy, int style) CreatesPdfFontbased on registeredFontProgram's.static PdfFontcreateRegisteredFont(String fontName, String encoding, PdfFontFactory.EmbeddingStrategy embeddingStrategy, int style, boolean cached) CreatesPdfFontbased on registeredFontProgram's.static PdfFontcreateTtcFont(byte[] ttc, int ttcIndex, String encoding, PdfFontFactory.EmbeddingStrategy embeddingStrategy, boolean cached) Creates aPdfFontinstance from the TrueType Collection represented by its byte contents.static PdfFontcreateTtcFont(String ttc, int ttcIndex, String encoding, PdfFontFactory.EmbeddingStrategy embeddingStrategy, boolean cached) Creates aPdfFontinstance from the TrueType Collection given by the path to the .ttc file.static PdfType3FontcreateType3Font(PdfDocument document, boolean colorized) Creates a new instance ofPdfType3Fontstatic PdfType3FontcreateType3Font(PdfDocument document, String fontName, String fontFamily, boolean colorized) Creates a new instance ofPdfType3FontGets a set of registered font families.Gets a set of registered font names.static booleanisRegistered(String fontName) Checks if a certain font is registered.static voidRegisters a .ttf, .otf, .afm, .pfm, or a .ttc font file.static voidRegister a font file and use an alias for the font contained in it.static intregisterDirectory(String dirPath) Registers all the fonts in a directory.static intregisterDirectoryRecursively(String dirPath) Registers all the fonts in a directory recursively.static voidregisterFamily(String familyName, String fullName, String path) Register a font by giving explicitly the font family and name.static intRegister fonts in some probable directories.
-
Constructor Details
-
PdfFontFactory
public PdfFontFactory()
-
-
Method Details
-
createFont
Creates a new instance of default font, namelyStandardFonts.HELVETICAstandard font withPdfEncodings.WINANSIencoding. Note, if you want to reuse the same instance of default font, you may usePdfDocument.getDefaultFont().- Returns:
- created font
- Throws:
-
IOException- if error occurred while creating the font, e.g. metrics loading failure
-
createFont
Creates aPdfFontby already existing font dictionary.Note, the font won't be added to any document, until you add it to
PdfCanvas. While adding toPdfCanvas, or toPdfResourcesthe font will be made indirect implicitly.PdfDocument.getFont(com.itextpdf.kernel.pdf.PdfDictionary)method is strongly recommended if you want to get PdfFont by both existing font dictionary, or just created and hasn't flushed yet.- Parameters:
-
fontDictionary- the font dictionary to create the font from - Returns:
-
created
PdfFontinstance
-
createFont
public static PdfFont createFont(String fontProgram, String encoding, PdfDocument cacheTo) throws IOException Creates aPdfFontinstance by the path of the font program file and given encoding and place it inside thePdfDocument. If suchPdfFonthas already been created and placed inside thePdfDocument, then retries its instance instead of creating.PdfFontFactory.EmbeddingStrategy.PREFER_EMBEDDEDwill be used as embedding strategy.- Parameters:
-
fontProgram- the path of the font program file -
encoding- the font encoding. SeePdfEncodings -
cacheTo- thePdfDocumentto cache the font - Returns:
-
created
PdfFontinstance - Throws:
-
IOException- exception is thrown in case an I/O error occurs when reading the file
-
createFont
public static PdfFont createFont(String fontProgram, String encoding, PdfFontFactory.EmbeddingStrategy embeddingStrategy, PdfDocument cacheTo) throws IOException Creates aPdfFontinstance by the path of the font program file and given encoding and place it inside thePdfDocument. If suchPdfFonthas already been created and placed inside thePdfDocument, then retries its instance instead of creating.- Parameters:
-
fontProgram- the path of the font program file -
encoding- the font encoding. SeePdfEncodings -
embeddingStrategy- thePdfFontFactory.EmbeddingStrategywhich will define whether the font will be embedded -
cacheTo- thePdfDocumentto cache the font - Returns:
-
created
PdfFontinstance - Throws:
-
IOException- exception is thrown in case an I/O error occurs when reading the file
-
createFont
Creates aPdfFontinstance by the path of the font program file- Parameters:
-
fontProgram- the path of the font program file - Returns:
-
created
PdfFontinstance - Throws:
-
IOException- exception is thrown in case an I/O error occurs when reading the file
-
createFont
Creates aPdfFontinstance by the path of the font program file and given encoding.- Parameters:
-
fontProgram- the path of the font program file -
encoding- the font encoding. SeePdfEncodings - Returns:
-
created
PdfFontinstance - Throws:
-
IOException- exception is thrown in case an I/O error occurs when reading the file
-
createFont
public static PdfFont createFont(String fontProgram, PdfFontFactory.EmbeddingStrategy embeddingStrategy) throws IOException Created aPdfFontinstance given the path to the font file.- Parameters:
-
fontProgram- the font program file -
embeddingStrategy- thePdfFontFactory.EmbeddingStrategywhich will define whether the font will be embedded - Returns:
-
created
PdfFontinstance - Throws:
-
IOException- in case the file is not found or the contents of the font file is mal-formed
-
createFont
public static PdfFont createFont(String fontProgram, String encoding, PdfFontFactory.EmbeddingStrategy embeddingStrategy) throws IOException Created aPdfFontinstance given the path to the font file.- Parameters:
-
fontProgram- the font program file -
encoding- the encoding of the font to be created. SeePdfEncodings -
embeddingStrategy- thePdfFontFactory.EmbeddingStrategywhich will define whether the font will be embedded - Returns:
-
created
PdfFontinstance - Throws:
-
IOException- in case the file is not found or the contents of the font file is mal-formed
-
createFont
public static PdfFont createFont(String fontProgram, String encoding, PdfFontFactory.EmbeddingStrategy embeddingStrategy, boolean cached) throws IOException Created aPdfFontinstance given the path to the font file.- Parameters:
-
fontProgram- the font program file -
encoding- the encoding of the font to be created. SeePdfEncodings -
embeddingStrategy- thePdfFontFactory.EmbeddingStrategywhich will define whether the font will be embedded -
cached- indicates whether the font will be cached - Returns:
-
created
PdfFontinstance - Throws:
-
IOException- in case the file is not found or the contents of the font file is mal-formed
-
createFont
public static PdfFont createFont(FontProgram fontProgram, String encoding, PdfFontFactory.EmbeddingStrategy embeddingStrategy) Created aPdfFontinstance given the given underlyingFontPrograminstance.- Parameters:
-
fontProgram- the font program of thePdfFontinstance to be created -
encoding- the encoding of the font to be created. SeePdfEncodings -
embeddingStrategy- thePdfFontFactory.EmbeddingStrategywhich will define whether the font will be embedded - Returns:
-
created
PdfFontinstance
-
createFont
Created aPdfFontinstance given the given underlyingFontPrograminstance.- Parameters:
-
fontProgram- the font program of thePdfFontinstance to be created -
encoding- the encoding of the font to be created. SeePdfEncodings - Returns:
-
created
PdfFontinstance
-
createFont
Created aPdfFontinstance given the given underlyingFontPrograminstance. -
createFont
Created aPdfFontinstance by the bytes of the underlying font program.- Parameters:
-
fontProgram- the bytes of the underlying font program -
encoding- the encoding of the font to be created. SeePdfEncodings - Returns:
-
created
PdfFontinstance - Throws:
-
IOException- signals that an I/O exception has occurred.
-
createFont
public static PdfFont createFont(byte[] fontProgram, PdfFontFactory.EmbeddingStrategy embeddingStrategy) throws IOException Created aPdfFontinstance by the bytes of the underlying font program.- Parameters:
-
fontProgram- the bytes of the underlying font program -
embeddingStrategy- thePdfFontFactory.EmbeddingStrategywhich will define whether the font will be embedded - Returns:
-
created
PdfFontinstance - Throws:
-
IOException- signals that an I/O exception has occurred.
-
createFont
public static PdfFont createFont(byte[] fontProgram, String encoding, PdfFontFactory.EmbeddingStrategy embeddingStrategy) throws IOException Created aPdfFontinstance by the bytes of the underlying font program.- Parameters:
-
fontProgram- the bytes of the underlying font program -
encoding- the encoding of the font to be created. SeePdfEncodings -
embeddingStrategy- thePdfFontFactory.EmbeddingStrategywhich will define whether the font will be embedded - Returns:
-
created
PdfFontinstance - Throws:
-
IOException- signals that an I/O exception has occurred.
-
createFont
public static PdfFont createFont(byte[] fontProgram, String encoding, PdfFontFactory.EmbeddingStrategy embeddingStrategy, boolean cached) throws IOException Created aPdfFontinstance by the bytes of the underlying font program.- Parameters:
-
fontProgram- the bytes of the underlying font program -
encoding- the encoding of the font to be created. SeePdfEncodings -
embeddingStrategy- thePdfFontFactory.EmbeddingStrategywhich will define whether the font will be embedded -
cached- indicates whether the font will be cached - Returns:
-
created
PdfFontinstance - Throws:
-
IOException- signals that an I/O exception has occurred.
-
createTtcFont
public static PdfFont createTtcFont(byte[] ttc, int ttcIndex, String encoding, PdfFontFactory.EmbeddingStrategy embeddingStrategy, boolean cached) throws IOException Creates aPdfFontinstance from the TrueType Collection represented by its byte contents.- Parameters:
-
ttc- the byte contents of the TrueType Collection -
ttcIndex- the index of the font in the collection, zero-based -
encoding- the encoding of the font to be created. SeePdfEncodings -
embeddingStrategy- thePdfFontFactory.EmbeddingStrategywhich will define whether the font will be embedded -
cached- indicates whether the font will be cached - Returns:
-
created
PdfFontinstance - Throws:
-
IOException- in case the contents of the TrueType Collection is mal-formed or an error occurred during reading the font
-
createTtcFont
public static PdfFont createTtcFont(String ttc, int ttcIndex, String encoding, PdfFontFactory.EmbeddingStrategy embeddingStrategy, boolean cached) throws IOException Creates aPdfFontinstance from the TrueType Collection given by the path to the .ttc file.- Parameters:
-
ttc- the path of the .ttc file -
ttcIndex- the index of the font in the collection, zero-based -
encoding- the encoding of the font to be created. SeePdfEncodings -
embeddingStrategy- thePdfFontFactory.EmbeddingStrategywhich will define whether the font will be embedded -
cached- indicates whether the font will be cached - Returns:
-
created
PdfFontinstance - Throws:
-
IOException- in case the file is not found, contents of the TrueType Collection is mal-formed or an error occurred during reading the font
-
createType3Font
Creates a new instance ofPdfType3Font- Parameters:
-
document- the target document of the new font -
colorized- indicates whether the font will be colorized - Returns:
- created font
-
createType3Font
public static PdfType3Font createType3Font(PdfDocument document, String fontName, String fontFamily, boolean colorized) Creates a new instance ofPdfType3Font- Parameters:
-
document- the target document of the new font. -
fontName- the PostScript name of the font, shall not be null or empty. -
fontFamily- a preferred font family name. -
colorized- indicates whether the font will be colorized - Returns:
- created font.
-
createRegisteredFont
public static PdfFont createRegisteredFont(String fontName, String encoding, PdfFontFactory.EmbeddingStrategy embeddingStrategy, int style, boolean cached) throws IOException CreatesPdfFontbased on registeredFontProgram's. Required font program is expected to be previously registered by one of the register method fromPdfFontFactory.- Parameters:
-
fontName- Path to font file or Standard font name -
encoding- Font encoding fromPdfEncodings. -
embeddingStrategy- thePdfFontFactory.EmbeddingStrategywhich will define whether the font will be embedded. Note, standard font won't be embedded in any case. -
style- Font style fromFontStyles. -
cached- If true font will be cached for another PdfDocument - Returns:
-
created font if required
FontProgramwas found among registered, otherwise null. - Throws:
-
IOException- exception is thrown in case an I/O error occurs when reading the file - See Also:
-
createRegisteredFont
public static PdfFont createRegisteredFont(String fontName, String encoding, PdfFontFactory.EmbeddingStrategy embeddingStrategy, boolean cached) throws IOException CreatesPdfFontbased on registeredFontProgram's. Required font program is expected to be previously registered by one of the register method fromPdfFontFactory.- Parameters:
-
fontName- Path to font file or Standard font name -
encoding- Font encoding fromPdfEncodings. -
embeddingStrategy- thePdfFontFactory.EmbeddingStrategywhich will define whether the font will be embedded. Note, standard font won't be embedded in any case. -
cached- If true font will be cached for another PdfDocument - Returns:
-
created font if required
FontProgramwas found among registered, otherwise null. - Throws:
-
IOException- exception is thrown in case an I/O error occurs when reading the file - See Also:
-
createRegisteredFont
public static PdfFont createRegisteredFont(String fontName, String encoding, PdfFontFactory.EmbeddingStrategy embeddingStrategy) throws IOException CreatesPdfFontbased on registeredFontProgram's. Required font program is expected to be previously registered by one of the register method fromPdfFontFactory.- Parameters:
-
fontName- Path to font file or Standard font name -
encoding- Font encoding fromPdfEncodings. -
embeddingStrategy- thePdfFontFactory.EmbeddingStrategywhich will define whether the font will be embedded. Note, standard font won't be embedded in any case. - Returns:
-
created font if required
FontProgramwas found among registered, otherwise null. - Throws:
-
IOException- exception is thrown in case an I/O error occurs when reading the file - See Also:
-
createRegisteredFont
public static PdfFont createRegisteredFont(String fontName, String encoding, PdfFontFactory.EmbeddingStrategy embeddingStrategy, int style) throws IOException CreatesPdfFontbased on registeredFontProgram's. Required font program is expected to be previously registered by one of the register method fromPdfFontFactory.- Parameters:
-
fontName- Path to font file or Standard font name -
encoding- Font encoding fromPdfEncodings. -
embeddingStrategy- thePdfFontFactory.EmbeddingStrategywhich will define whether the font will be embedded. Note, standard font won't be embedded in any case. -
style- Font style fromFontStyles. - Returns:
-
created font if required
FontProgramwas found among registered, otherwise null. - Throws:
-
IOException- exception is thrown in case an I/O error occurs when reading the file - See Also:
-
createRegisteredFont
CreatesPdfFontbased on registeredFontProgram's. Required font program is expected to be previously registered by one of the register method fromPdfFontFactory.- Parameters:
-
fontName- Path to font file or Standard font name -
encoding- Font encoding fromPdfEncodings. - Returns:
-
created font if required
FontProgramwas found among registered, otherwise null. - Throws:
-
IOException- exception is thrown in case an I/O error occurs when reading the file - See Also:
-
createRegisteredFont
CreatesPdfFontbased on registeredFontProgram's. Required font program is expected to be previously registered by one of the register method fromPdfFontFactory.- Parameters:
-
fontName- Path to font file or Standard font name - Returns:
-
created font if required
FontProgramwas found among registered, otherwise null. - Throws:
-
IOException- exception is thrown in case an I/O error occurs when reading the file - See Also:
-
registerFamily
Register a font by giving explicitly the font family and name.- Parameters:
-
familyName- the font family -
fullName- the font name -
path- the font path
-
register
Registers a .ttf, .otf, .afm, .pfm, or a .ttc font file. In case if TrueType Collection (.ttc), an additional parameter may be specified defining the index of the font to be registered, e.g. "path/to/font/collection.ttc,0". The index is zero-based.- Parameters:
-
path- the path to a font file
-
register
Register a font file and use an alias for the font contained in it.- Parameters:
-
path- the path to a font file -
alias- the alias you want to use for the font
-
registerDirectory
Registers all the fonts in a directory.- Parameters:
-
dirPath- the directory path to be registered as a font directory path - Returns:
- the number of fonts registered
-
registerDirectoryRecursively
Registers all the fonts in a directory recursively.- Parameters:
-
dirPath- the directory path to be registered as a font directory path - Returns:
- the number of fonts registered
-
registerSystemDirectories
public static int registerSystemDirectories()Register fonts in some probable directories. It usually works in Windows, Linux and Solaris.- Returns:
- the number of fonts registered
-
getRegisteredFonts
Gets a set of registered font names.- Returns:
- a set of registered fonts
-
getRegisteredFamilies
Gets a set of registered font families.- Returns:
- a set of registered font families
-
isRegistered
Checks if a certain font is registered.- Parameters:
-
fontName- the name of the font that has to be checked. - Returns:
-
trueif the font is found,falseotherwise
-