Class PdfFontFactory


  • public final class PdfFontFactory
    extends java.lang.Object
    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.

    • Field Detail

      • DEFAULT_ENCODING

        private static final java.lang.String DEFAULT_ENCODING
        This is the default encoding to use.
        See Also:
        Constant Field Values
      • DEFAULT_CACHED

        private static final boolean DEFAULT_CACHED
        This is the default value of the cached variable.
        See Also:
        Constant Field Values
    • Constructor Detail

      • PdfFontFactory

        public PdfFontFactory()
    • Method Detail

      • createFont

        public static PdfFont createFont()
                                  throws java.io.IOException
        Creates a new instance of default font, namely StandardFonts.HELVETICA standard font with PdfEncodings.WINANSI encoding. Note, if you want to reuse the same instance of default font, you may use PdfDocument.getDefaultFont().
        Returns:
        created font
        Throws:
        java.io.IOException - if error occurred while creating the font, e.g. metrics loading failure
      • createFont

        public static PdfFont createFont​(java.lang.String fontProgram,
                                         java.lang.String encoding,
                                         PdfDocument cacheTo)
                                  throws java.io.IOException
        Creates a PdfFont instance by the path of the font program file and given encoding and place it inside the PdfDocument. If such PdfFont has already been created and placed inside the PdfDocument, 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. See PdfEncodings
        cacheTo - the PdfDocument to cache the font
        Returns:
        created PdfFont instance
        Throws:
        java.io.IOException - exception is thrown in case an I/O error occurs when reading the file
      • createFont

        public static PdfFont createFont​(java.lang.String fontProgram,
                                         java.lang.String encoding,
                                         PdfFontFactory.EmbeddingStrategy embeddingStrategy,
                                         PdfDocument cacheTo)
                                  throws java.io.IOException
        Creates a PdfFont instance by the path of the font program file and given encoding and place it inside the PdfDocument. If such PdfFont has already been created and placed inside the PdfDocument, then retries its instance instead of creating.
        Parameters:
        fontProgram - the path of the font program file
        encoding - the font encoding. See PdfEncodings
        embeddingStrategy - the PdfFontFactory.EmbeddingStrategy which will define whether the font will be embedded
        cacheTo - the PdfDocument to cache the font
        Returns:
        created PdfFont instance
        Throws:
        java.io.IOException - exception is thrown in case an I/O error occurs when reading the file
      • createFont

        public static PdfFont createFont​(java.lang.String fontProgram)
                                  throws java.io.IOException
        Creates a PdfFont instance by the path of the font program file
        Parameters:
        fontProgram - the path of the font program file
        Returns:
        created PdfFont instance
        Throws:
        java.io.IOException - exception is thrown in case an I/O error occurs when reading the file
      • createFont

        public static PdfFont createFont​(java.lang.String fontProgram,
                                         java.lang.String encoding)
                                  throws java.io.IOException
        Creates a PdfFont 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. See PdfEncodings
        Returns:
        created PdfFont instance
        Throws:
        java.io.IOException - exception is thrown in case an I/O error occurs when reading the file
      • createFont

        public static PdfFont createFont​(java.lang.String fontProgram,
                                         PdfFontFactory.EmbeddingStrategy embeddingStrategy)
                                  throws java.io.IOException
        Created a PdfFont instance given the path to the font file.
        Parameters:
        fontProgram - the font program file
        embeddingStrategy - the PdfFontFactory.EmbeddingStrategy which will define whether the font will be embedded
        Returns:
        created PdfFont instance
        Throws:
        java.io.IOException - in case the file is not found or the contents of the font file is mal-formed
      • createFont

        public static PdfFont createFont​(java.lang.String fontProgram,
                                         java.lang.String encoding,
                                         PdfFontFactory.EmbeddingStrategy embeddingStrategy)
                                  throws java.io.IOException
        Created a PdfFont instance given the path to the font file.
        Parameters:
        fontProgram - the font program file
        encoding - the encoding of the font to be created. See PdfEncodings
        embeddingStrategy - the PdfFontFactory.EmbeddingStrategy which will define whether the font will be embedded
        Returns:
        created PdfFont instance
        Throws:
        java.io.IOException - in case the file is not found or the contents of the font file is mal-formed
      • createFont

        public static PdfFont createFont​(java.lang.String fontProgram,
                                         java.lang.String encoding,
                                         PdfFontFactory.EmbeddingStrategy embeddingStrategy,
                                         boolean cached)
                                  throws java.io.IOException
        Created a PdfFont instance given the path to the font file.
        Parameters:
        fontProgram - the font program file
        encoding - the encoding of the font to be created. See PdfEncodings
        embeddingStrategy - the PdfFontFactory.EmbeddingStrategy which will define whether the font will be embedded
        cached - indicates whether the font will be cached
        Returns:
        created PdfFont instance
        Throws:
        java.io.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,
                                         java.lang.String encoding)
        Created a PdfFont instance given the given underlying FontProgram instance.
        Parameters:
        fontProgram - the font program of the PdfFont instance to be created
        encoding - the encoding of the font to be created. See PdfEncodings
        Returns:
        created PdfFont instance
      • createFont

        public static PdfFont createFont​(FontProgram fontProgram)
        Created a PdfFont instance given the given underlying FontProgram instance.
        Parameters:
        fontProgram - the font program of the PdfFont instance to be created
        Returns:
        created PdfFont instance
      • createFont

        public static PdfFont createFont​(byte[] fontProgram,
                                         java.lang.String encoding)
                                  throws java.io.IOException
        Created a PdfFont 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. See PdfEncodings
        Returns:
        created PdfFont instance
        Throws:
        java.io.IOException - signals that an I/O exception has occurred.
      • createFont

        public static PdfFont createFont​(byte[] fontProgram,
                                         PdfFontFactory.EmbeddingStrategy embeddingStrategy)
                                  throws java.io.IOException
        Created a PdfFont instance by the bytes of the underlying font program.
        Parameters:
        fontProgram - the bytes of the underlying font program
        embeddingStrategy - the PdfFontFactory.EmbeddingStrategy which will define whether the font will be embedded
        Returns:
        created PdfFont instance
        Throws:
        java.io.IOException - signals that an I/O exception has occurred.
      • createFont

        public static PdfFont createFont​(byte[] fontProgram,
                                         java.lang.String encoding,
                                         PdfFontFactory.EmbeddingStrategy embeddingStrategy)
                                  throws java.io.IOException
        Created a PdfFont 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. See PdfEncodings
        embeddingStrategy - the PdfFontFactory.EmbeddingStrategy which will define whether the font will be embedded
        Returns:
        created PdfFont instance
        Throws:
        java.io.IOException - signals that an I/O exception has occurred.
      • createFont

        public static PdfFont createFont​(byte[] fontProgram,
                                         java.lang.String encoding,
                                         PdfFontFactory.EmbeddingStrategy embeddingStrategy,
                                         boolean cached)
                                  throws java.io.IOException
        Created a PdfFont 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. See PdfEncodings
        embeddingStrategy - the PdfFontFactory.EmbeddingStrategy which will define whether the font will be embedded
        cached - indicates whether the font will be cached
        Returns:
        created PdfFont instance
        Throws:
        java.io.IOException - signals that an I/O exception has occurred.
      • createTtcFont

        public static PdfFont createTtcFont​(byte[] ttc,
                                            int ttcIndex,
                                            java.lang.String encoding,
                                            PdfFontFactory.EmbeddingStrategy embeddingStrategy,
                                            boolean cached)
                                     throws java.io.IOException
        Creates a PdfFont 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. See PdfEncodings
        embeddingStrategy - the PdfFontFactory.EmbeddingStrategy which will define whether the font will be embedded
        cached - indicates whether the font will be cached
        Returns:
        created PdfFont instance
        Throws:
        java.io.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​(java.lang.String ttc,
                                            int ttcIndex,
                                            java.lang.String encoding,
                                            PdfFontFactory.EmbeddingStrategy embeddingStrategy,
                                            boolean cached)
                                     throws java.io.IOException
        Creates a PdfFont 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. See PdfEncodings
        embeddingStrategy - the PdfFontFactory.EmbeddingStrategy which will define whether the font will be embedded
        cached - indicates whether the font will be cached
        Returns:
        created PdfFont instance
        Throws:
        java.io.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

        public static PdfType3Font createType3Font​(PdfDocument document,
                                                   boolean colorized)
        Creates a new instance of PdfType3Font
        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,
                                                   java.lang.String fontName,
                                                   java.lang.String fontFamily,
                                                   boolean colorized)
        Creates a new instance of PdfType3Font
        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.
      • registerFamily

        public static void registerFamily​(java.lang.String familyName,
                                          java.lang.String fullName,
                                          java.lang.String path)
        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

        public static void register​(java.lang.String path)
        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

        public static void register​(java.lang.String path,
                                    java.lang.String alias)
        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

        public static int registerDirectory​(java.lang.String dirPath)
        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

        public static java.util.Set<java.lang.String> getRegisteredFonts()
        Gets a set of registered font names.
        Returns:
        a set of registered fonts
      • getRegisteredFamilies

        public static java.util.Set<java.lang.String> getRegisteredFamilies()
        Gets a set of registered font families.
        Returns:
        a set of registered font families
      • isRegistered

        public static boolean isRegistered​(java.lang.String fontName)
        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