com.aspose.pdf

Interfaces

Classes

Enums

Exceptions

com.aspose.pdf

Class FontRepository



  • public final class FontRepository
    extends Object

    Performs font search. Searches in system installed fonts and standard Pdf fonts. Also provides functionality to open custom fonts.


     The example demonstrates how to find font and replace the font of text of first page.
     
      
      // Find font
      Font font = FontRepository.findFont("Arial");
      
      // Open document
      Document doc = new Document("D:\\Tests\\input.pdf");
      // Create TextFragmentAbsorber object to find all "hello world" text occurrences
      TextFragmentAbsorber absorber = new TextFragmentAbsorber("hello world");
      
      // Accept the absorber for first page
      doc.getPages().get_Item(1).accept(absorber);
      
      // Change font of the first text occurrence
      absorber.getTextFragments().get_Item(1).getTextState().setFont(font);
      
      // Save document
      doc.save("D:\\Tests\\output.pdf"); 
            
    See Also:
    TextFragmentAbsorber, IDocument
    • Constructor Detail

      • FontRepository

        public FontRepository()
    • Method Detail

      • getSubstitutions

        public static FontSubstitutionCollection getSubstitutions()

        Gets font substitution strategies collection.

        Returns:
        FontSubstitutionCollection object
      • getSources

        public static FontSourceCollection getSources()

        Gets font sources collection.

        Returns:
        FontSourceCollection object
      • findFont

        public static Font findFont(String fontName)

        Searches and returns font with specified font name.


         The example demonstrates how to find font and replace the font of text of first page.
         
          // Find font
          Font font = FontRepository.findFont("Arial");
          
          // Open document
          Document doc = new Document("D:\\Tests\\input.pdf");
          // Create TextFragmentAbsorber object to find all "hello world" text occurrences
          TextFragmentAbsorber absorber = new TextFragmentAbsorber("hello world");
          
          // Accept the absorber for first page
          doc.getPages().get_Item(1).accept(absorber);
          
          // Change font of the first text occurrence
          absorber.getTextFragments().get_Item(1).getTextState().setFont ( font);
          
          // Save document
          doc.save("D:\\Tests\\output.pdf"); 
                
        Parameters:
        fontName - Font name.
        Returns:
        Font object.
      • findFont

        public static Font findFont(String fontName,
                                    boolean ignoreCase)

        Searches and returns font with specified font name ignoring or honoring case sensitivity.


         The example demonstrates how to find font and replace the font of text of first page.
         
          // Find font
          Font font = FontRepository.findFont("Arial", FontStyles.Italic);
          
          // Open document
          Document doc = new Document("D:\\Tests\\input.pdf");
          // Create TextFragmentAbsorber object to find all "hello world" text occurences
          TextFragmentAbsorber absorber = new TextFragmentAbsorber("hello world");
          
          // Accept the absorber for first page
          doc.getPages().get_Item(1).accept(absorber);
          
          // Change font of the first text occurence
          absorber.getTextFragments().get_Item(1).getTextState().setFont(font);
          
          // Save document
          doc.save("D:\\Tests\\output.pdf"); 
                
        Parameters:
        fontName - Font name.
        ignoreCase - case sensitivity
        Returns:
        Font object.
      • findFont

        public static Font findFont(String fontFamilyName,
                                    int stl)

        Searches and returns font with specified font name and font style.


         The example demonstrates how to find font and replace the font of text of first page.
         
          // Find font
          Font font = FontRepository.findFont("Arial", FontStyles.Italic);
          
          // Open document
          Document doc = new Document("D:\\Tests\\input.pdf");
          // Create TextFragmentAbsorber object to find all "hello world" text occurences
          TextFragmentAbsorber absorber = new TextFragmentAbsorber("hello world");
          
          // Accept the absorber for first page
          doc.getPages().get_Item(1).accept(absorber);
          
          // Change font of the first text occurence
          absorber.getTextFragments().get_Item(1).getTextState().setFont(font);
          
          // Save document
          doc.save("D:\\Tests\\output.pdf"); 
                
        Parameters:
        fontFamilyName - Font family name.
        stl - Font style value.
        Returns:
        Font object corresponding to search request parameters.
        See Also:
        FontStyles
      • findFont

        public static Font findFont(String fontFamilyName,
                                    int stl,
                                    boolean ignoreCase)

        Searches and returns font with specified font name and font style ignoring or honoring case sensitivity.


         The example demonstrates how to find font and replace the font of text of first page.
         
          // Find font
          
          Font font = FontRepository.findFont("Arial", FontStyles.Italic, true);
          
          // Open document
          Document doc = new Document("D:\\Tests\\input.pdf");
          // Create TextFragmentAbsorber object to find all "hello world" text occurences
          TextFragmentAbsorber absorber = new TextFragmentAbsorber("hello world");
          
          // Accept the absorber for first page
          doc.getPages().get_Item(1).accept(absorber);
          
          // Change font of the first text occurence
          absorber.getTextFragments().get_Item(1).getTextState().setFont(font);
          
          // Save document
          doc.save("D:\\Tests\\output.pdf"); 
                
        Parameters:
        fontFamilyName - Font family name.
        stl - Font style value.
        ignoreCase - case sensitivity
        Returns:
        Font object corresponding to search request parameters.
      • openFont

        public static Font openFont(InputStream fontStream,
                                    int fontType)

        Opens font with specified font stream.


         The example demonstrates how to open font and replace the font of text of first page.
          
          // Open font
          InputStream fontStream = new FileInputStream("C:\\WINDOWS\\Fonts\\arial.ttf"))
          {
              Font font = FontRepository.openFont(fontStream, , FontTypes.TTF);
          
              // Open document
              Document doc = new Document("D:\\Tests\\input.pdf");
              // Create TextFragmentAbsorber object to find all "hello world" text occurrences
              TextFragmentAbsorber absorber = new TextFragmentAbsorber("hello world");
          
              // Accept the absorber for first page
              doc.getPages().get_Item(1).accept(absorber);
          
              // Change font of the first text occurrence
              absorber.getTextFragments().get_Item(1).getTextState().setFont ( font);
          
              // Save document
              doc.save("D:\\Tests\\output.pdf"); 
          }
                
        Parameters:
        fontStream - Font stream.
        fontType - Font type value.
        Returns:
        Font object.
        See Also:
        FontTypes
      • openFont

        public static Font openFont(String fontFilePath)

        Opens font with specified font file path.


         The example demonstrates how to open font and replace the font of text of first page.
         
          // Open font
          Font font = FontRepository.openFont("C:\\WINDOWS\\Fonts\\arial.ttf");
          
          // Open document
          Document doc = new Document("D:\\Tests\\input.pdf");
          // Create TextFragmentAbsorber object to find all "hello world" text occurrences
          TextFragmentAbsorber absorber = new TextFragmentAbsorber("hello world");
          
          // Accept the absorber for first page
          doc.getPages().get_Item(1).accept(absorber);
          
          // Change font of the first text occurrence
          absorber.getTextFragments().get_Item(1).getTextState().setFont ( font);
          
          // Save document
          doc.save("D:\\Tests\\output.pdf"); 
                
        Parameters:
        fontFilePath - Font file path.
        Returns:
        Font object.
      • openFont

        public static Font openFont(String fontFilePath,
                                    String metricsFilePath)

        Opens font with specified font file path and metrics file path.


         The example demonstrates how to open Type1 font with metrics and replace the font of text of first page.
          
          // Open font
          Font font = FontRepository.openFont("courier.pfb", "courier.afm");
          
          // Open document
          Document doc = new Document("D:\\Tests\\input.pdf");
          // Create TextFragmentAbsorber object to find all "hello world" text occurrences
          TextFragmentAbsorber absorber = new TextFragmentAbsorber("hello world");
          
          // Accept the absorber for first page
          doc.getPages().get_Item(1).accept(absorber);
          
          // Change font of the first text occurrence
          absorber.getTextFragments().get_Item(1).sgetTextState().setFont(font);
          
          // Save document
          doc.save("D:\\Tests\\output.pdf"); 
                
        Parameters:
        fontFilePath - Font file path.
        metricsFilePath - Font metrics file patrh.
        Returns:
        Font object.
      • loadFonts

        public static void loadFonts()

        Loads system installed fonts and standard Pdf fonts. This method was designed to speed up font loading process. By default fonts are loaded on first request for any font. Use of this method loads system and standard Pdf fonts immediately before any Pdf document was open.

      • isReplaceNotFoundFonts

        public static boolean isReplaceNotFoundFonts()
        Is not found fonts will be replaced by standard font.
        Returns:
        boolean value
      • setReplaceNotFoundFonts

        public static void setReplaceNotFoundFonts(boolean value)
        Set TRUE if need to replace not found fonts with the default font. The default value is false.
        Parameters:
        value - boolean