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
Modifier and TypeClassDescriptionstatic enum
Enum values for font embedding strategies. -
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionstatic PdfFont
Creates a new instance of default font, namelyStandardFonts.HELVETICA
standard font withPdfEncodings.WINANSI
encoding.static PdfFont
createFont
(byte[] fontProgram, PdfFontFactory.EmbeddingStrategy embeddingStrategy) Created aPdfFont
instance by the bytes of the underlying font program.static PdfFont
createFont
(byte[] fontProgram, String encoding) Created aPdfFont
instance by the bytes of the underlying font program.static PdfFont
createFont
(byte[] fontProgram, String encoding, PdfFontFactory.EmbeddingStrategy embeddingStrategy) Created aPdfFont
instance by the bytes of the underlying font program.static PdfFont
createFont
(byte[] fontProgram, String encoding, PdfFontFactory.EmbeddingStrategy embeddingStrategy, boolean cached) Created aPdfFont
instance by the bytes of the underlying font program.static PdfFont
createFont
(FontProgram fontProgram) Created aPdfFont
instance given the given underlyingFontProgram
instance.static PdfFont
createFont
(FontProgram fontProgram, String encoding) Created aPdfFont
instance given the given underlyingFontProgram
instance.static PdfFont
createFont
(FontProgram fontProgram, String encoding, PdfFontFactory.EmbeddingStrategy embeddingStrategy) Created aPdfFont
instance given the given underlyingFontProgram
instance.static PdfFont
createFont
(PdfDictionary fontDictionary) Creates aPdfFont
by already existing font dictionary.static PdfFont
createFont
(String fontProgram) Creates aPdfFont
instance by the path of the font program filestatic PdfFont
createFont
(String fontProgram, PdfFontFactory.EmbeddingStrategy embeddingStrategy) Created aPdfFont
instance given the path to the font file.static PdfFont
createFont
(String fontProgram, String encoding) Creates aPdfFont
instance by the path of the font program file and given encoding.static PdfFont
createFont
(String fontProgram, String encoding, PdfFontFactory.EmbeddingStrategy embeddingStrategy) Created aPdfFont
instance given the path to the font file.static PdfFont
createFont
(String fontProgram, String encoding, PdfFontFactory.EmbeddingStrategy embeddingStrategy, boolean cached) Created aPdfFont
instance given the path to the font file.static PdfFont
createFont
(String fontProgram, String encoding, PdfFontFactory.EmbeddingStrategy embeddingStrategy, PdfDocument cacheTo) Creates aPdfFont
instance by the path of the font program file and given encoding and place it inside thePdfDocument
.static PdfFont
createFont
(String fontProgram, String encoding, PdfDocument cacheTo) Creates aPdfFont
instance by the path of the font program file and given encoding and place it inside thePdfDocument
.static PdfFont
createRegisteredFont
(String fontName) CreatesPdfFont
based on registeredFontProgram
's.static PdfFont
createRegisteredFont
(String fontName, String encoding) CreatesPdfFont
based on registeredFontProgram
's.static PdfFont
createRegisteredFont
(String fontName, String encoding, PdfFontFactory.EmbeddingStrategy embeddingStrategy) CreatesPdfFont
based on registeredFontProgram
's.static PdfFont
createRegisteredFont
(String fontName, String encoding, PdfFontFactory.EmbeddingStrategy embeddingStrategy, boolean cached) CreatesPdfFont
based on registeredFontProgram
's.static PdfFont
createRegisteredFont
(String fontName, String encoding, PdfFontFactory.EmbeddingStrategy embeddingStrategy, int style) CreatesPdfFont
based on registeredFontProgram
's.static PdfFont
createRegisteredFont
(String fontName, String encoding, PdfFontFactory.EmbeddingStrategy embeddingStrategy, int style, boolean cached) CreatesPdfFont
based on registeredFontProgram
's.static PdfFont
createTtcFont
(byte[] ttc, int ttcIndex, String encoding, PdfFontFactory.EmbeddingStrategy embeddingStrategy, boolean cached) Creates aPdfFont
instance from the TrueType Collection represented by its byte contents.static PdfFont
createTtcFont
(String ttc, int ttcIndex, String encoding, PdfFontFactory.EmbeddingStrategy embeddingStrategy, boolean cached) Creates aPdfFont
instance from the TrueType Collection given by the path to the .ttc file.static PdfType3Font
createType3Font
(PdfDocument document, boolean colorized) Creates a new instance ofPdfType3Font
static PdfType3Font
createType3Font
(PdfDocument document, String fontName, String fontFamily, boolean colorized) Creates a new instance ofPdfType3Font
Gets a set of registered font families.Gets a set of registered font names.static boolean
isRegistered
(String fontName) Checks if a certain font is registered.static void
Registers a .ttf, .otf, .afm, .pfm, or a .ttc font file.static void
Register a font file and use an alias for the font contained in it.static int
registerDirectory
(String dirPath) Registers all the fonts in a directory.static void
registerFamily
(String familyName, String fullName, String path) Register a font by giving explicitly the font family and name.static int
Register fonts in some probable directories.
-
Constructor Details
-
PdfFontFactory
public PdfFontFactory()
-
-
Method Details
-
createFont
Creates a new instance of default font, namelyStandardFonts.HELVETICA
standard font withPdfEncodings.WINANSI
encoding. 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 aPdfFont
by already existing font dictionary.Note, the font won't be added to any document, until you add it to
PdfCanvas
. While adding toPdfCanvas
, or toPdfResources
the 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
PdfFont
instance
-
createFont
public static PdfFont createFont(String fontProgram, String encoding, PdfDocument cacheTo) throws IOException Creates aPdfFont
instance by the path of the font program file and given encoding and place it inside thePdfDocument
. If suchPdfFont
has already been created and placed inside thePdfDocument
, then retries its instance instead of creating.PdfFontFactory.EmbeddingStrategy.PREFER_EMBEDDED
will be used as embedding strategy.- Parameters:
-
fontProgram
- the path of the font program file -
encoding
- the font encoding. SeePdfEncodings
-
cacheTo
- thePdfDocument
to cache the font - Returns:
-
created
PdfFont
instance - 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 aPdfFont
instance by the path of the font program file and given encoding and place it inside thePdfDocument
. If suchPdfFont
has 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.EmbeddingStrategy
which will define whether the font will be embedded -
cacheTo
- thePdfDocument
to cache the font - Returns:
-
created
PdfFont
instance - Throws:
-
IOException
- exception is thrown in case an I/O error occurs when reading the file
-
createFont
Creates aPdfFont
instance by the path of the font program file- Parameters:
-
fontProgram
- the path of the font program file - Returns:
-
created
PdfFont
instance - Throws:
-
IOException
- exception is thrown in case an I/O error occurs when reading the file
-
createFont
Creates aPdfFont
instance 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
PdfFont
instance - 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 aPdfFont
instance given the path to the font file.- Parameters:
-
fontProgram
- the font program file -
embeddingStrategy
- thePdfFontFactory.EmbeddingStrategy
which will define whether the font will be embedded - Returns:
-
created
PdfFont
instance - 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 aPdfFont
instance 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.EmbeddingStrategy
which will define whether the font will be embedded - Returns:
-
created
PdfFont
instance - 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 aPdfFont
instance 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.EmbeddingStrategy
which will define whether the font will be embedded -
cached
- indicates whether the font will be cached - Returns:
-
created
PdfFont
instance - 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 aPdfFont
instance given the given underlyingFontProgram
instance.- Parameters:
-
fontProgram
- the font program of thePdfFont
instance to be created -
encoding
- the encoding of the font to be created. SeePdfEncodings
-
embeddingStrategy
- thePdfFontFactory.EmbeddingStrategy
which will define whether the font will be embedded - Returns:
-
created
PdfFont
instance
-
createFont
Created aPdfFont
instance given the given underlyingFontProgram
instance.- Parameters:
-
fontProgram
- the font program of thePdfFont
instance to be created -
encoding
- the encoding of the font to be created. SeePdfEncodings
- Returns:
-
created
PdfFont
instance
-
createFont
Created aPdfFont
instance given the given underlyingFontProgram
instance. -
createFont
Created aPdfFont
instance 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
PdfFont
instance - Throws:
-
IOException
- signals that an I/O exception has occurred.
-
createFont
public static PdfFont createFont(byte[] fontProgram, PdfFontFactory.EmbeddingStrategy embeddingStrategy) throws IOException Created aPdfFont
instance by the bytes of the underlying font program.- Parameters:
-
fontProgram
- the bytes of the underlying font program -
embeddingStrategy
- thePdfFontFactory.EmbeddingStrategy
which will define whether the font will be embedded - Returns:
-
created
PdfFont
instance - 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 aPdfFont
instance 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.EmbeddingStrategy
which will define whether the font will be embedded - Returns:
-
created
PdfFont
instance - 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 aPdfFont
instance 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.EmbeddingStrategy
which will define whether the font will be embedded -
cached
- indicates whether the font will be cached - Returns:
-
created
PdfFont
instance - 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 aPdfFont
instance 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.EmbeddingStrategy
which will define whether the font will be embedded -
cached
- indicates whether the font will be cached - Returns:
-
created
PdfFont
instance - 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 aPdfFont
instance 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.EmbeddingStrategy
which will define whether the font will be embedded -
cached
- indicates whether the font will be cached - Returns:
-
created
PdfFont
instance - 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 CreatesPdfFont
based 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.EmbeddingStrategy
which 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
FontProgram
was 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 CreatesPdfFont
based 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.EmbeddingStrategy
which 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
FontProgram
was 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 CreatesPdfFont
based 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.EmbeddingStrategy
which will define whether the font will be embedded. Note, standard font won't be embedded in any case. - Returns:
-
created font if required
FontProgram
was 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 CreatesPdfFont
based 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.EmbeddingStrategy
which 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
FontProgram
was found among registered, otherwise null. - Throws:
-
IOException
- exception is thrown in case an I/O error occurs when reading the file - See Also:
-
createRegisteredFont
CreatesPdfFont
based 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
FontProgram
was found among registered, otherwise null. - Throws:
-
IOException
- exception is thrown in case an I/O error occurs when reading the file - See Also:
-
createRegisteredFont
CreatesPdfFont
based 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
FontProgram
was 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
-
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:
-
true
if the font is found,false
otherwise
-