com.aspose.words

Class FontInfoCollection

  • java.lang.Object
    • com.aspose.words.FontInfoCollection
  • All Implemented Interfaces:
    java.lang.Iterable
    public class FontInfoCollection 
    extends java.lang.Object

Represents a collection of fonts used in a document.

Items are FontInfo objects.

You do not create instances of this class directly. Use the DocumentBase.FontInfos property to access the collection of fonts defined in the document.

Example:

Shows how to save a document with embedded TrueType fonts.
Document doc = new Document(getMyDir() + "Document.docx");

FontInfoCollection fontInfos = doc.getFontInfos();
fontInfos.setEmbedTrueTypeFonts(embedAllFonts);
fontInfos.setEmbedSystemFonts(embedAllFonts);
fontInfos.setSaveSubsetFonts(embedAllFonts);

doc.save(getArtifactsDir() + "Font.FontInfoCollection.docx");

Example:

Shows how to print the details of what fonts are present in a document.
Document doc = new Document(getMyDir() + "Embedded font.docx");

FontInfoCollection allFonts = doc.getFontInfos();

// Print all the used and unused fonts in the document.
for (int i = 0; i < allFonts.getCount(); i++)
{
    System.out.println("Font index #{i}");
    System.out.println("\tName: {allFonts[i].Name}");
    System.out.println("\tIs {(allFonts[i].IsTrueType ? ");
}
See Also:
FontInfo, DocumentBase.FontInfos

Property Getters/Setters Summary
intgetCount()
Gets the number of elements contained in the collection.
booleangetEmbedSystemFonts()
void
setEmbedSystemFonts(booleanvalue)
          

Specifies whether or not to embed System fonts into the document. Default value for this property is false.

This option works only when EmbedTrueTypeFonts option is set to true.

booleangetEmbedTrueTypeFonts()
void
setEmbedTrueTypeFonts(booleanvalue)
           Specifies whether or not to embed TrueType fonts in a document when it is saved. Default value for this property is false.
booleangetSaveSubsetFonts()
void
setSaveSubsetFonts(booleanvalue)
          

Specifies whether or not to save a subset of the embedded TrueType fonts with the document. Default value for this property is false.

This option works only when EmbedTrueTypeFonts property is set to true.

FontInfoget(int index)
Gets a font at the specified index.
FontInfoget(java.lang.String name)
Gets a font with the specified name.
 
Method Summary
booleancontains(java.lang.String name)
Determines whether the collection contains a font with the given name.
java.util.Iterator<FontInfo>iterator()
Returns an iterator object that can be used to iterate over all items in the collection.
 

    • Property Getters/Setters Detail

      • getCount

        public int getCount()
        
        Gets the number of elements contained in the collection.

        Example:

        Shows info about the fonts that are present in the blank document.
        Document doc = new Document();
        
        // A blank document contains 3 default fonts. Each font in the document
        // will have a corresponding FontInfo object which contains details about that font.
        Assert.assertEquals(3, doc.getFontInfos().getCount());
        
        Assert.assertTrue(doc.getFontInfos().contains("Times New Roman"));
        Assert.assertEquals(204, doc.getFontInfos().get("Times New Roman").getCharset());
        
        Assert.assertTrue(doc.getFontInfos().contains("Symbol"));
        Assert.assertTrue(doc.getFontInfos().contains("Arial"));
      • getEmbedSystemFonts/setEmbedSystemFonts

        public boolean getEmbedSystemFonts() / public void setEmbedSystemFonts(boolean value)
        

        Specifies whether or not to embed System fonts into the document. Default value for this property is false.

        This option works only when EmbedTrueTypeFonts option is set to true.

        Setting this property to True is useful if the user is on an East Asian system and wants to create a document that is readable by others who do not have fonts for that language on their system. For example, a user on a Japanese system could choose to embed the fonts in a document so that the Japanese document would be readable on all systems.

        This option works for DOC, DOCX and RTF formats only.

        Example:

        Shows how to save a document with embedded TrueType fonts.
        Document doc = new Document(getMyDir() + "Document.docx");
        
        FontInfoCollection fontInfos = doc.getFontInfos();
        fontInfos.setEmbedTrueTypeFonts(embedAllFonts);
        fontInfos.setEmbedSystemFonts(embedAllFonts);
        fontInfos.setSaveSubsetFonts(embedAllFonts);
        
        doc.save(getArtifactsDir() + "Font.FontInfoCollection.docx");
      • getEmbedTrueTypeFonts/setEmbedTrueTypeFonts

        public boolean getEmbedTrueTypeFonts() / public void setEmbedTrueTypeFonts(boolean value)
        
        Specifies whether or not to embed TrueType fonts in a document when it is saved. Default value for this property is false.

        Embedding TrueType fonts allows others to view the document with the same fonts that were used to create it, but may substantially increase the document size.

        This option works for DOC, DOCX and RTF formats only.

        Example:

        Shows how to save a document with embedded TrueType fonts.
        Document doc = new Document(getMyDir() + "Document.docx");
        
        FontInfoCollection fontInfos = doc.getFontInfos();
        fontInfos.setEmbedTrueTypeFonts(embedAllFonts);
        fontInfos.setEmbedSystemFonts(embedAllFonts);
        fontInfos.setSaveSubsetFonts(embedAllFonts);
        
        doc.save(getArtifactsDir() + "Font.FontInfoCollection.docx");
      • getSaveSubsetFonts/setSaveSubsetFonts

        public boolean getSaveSubsetFonts() / public void setSaveSubsetFonts(boolean value)
        

        Specifies whether or not to save a subset of the embedded TrueType fonts with the document. Default value for this property is false.

        This option works only when EmbedTrueTypeFonts property is set to true.

        This option works for DOC, DOCX and RTF formats only.

        Example:

        Shows how to save a document with embedded TrueType fonts.
        Document doc = new Document(getMyDir() + "Document.docx");
        
        FontInfoCollection fontInfos = doc.getFontInfos();
        fontInfos.setEmbedTrueTypeFonts(embedAllFonts);
        fontInfos.setEmbedSystemFonts(embedAllFonts);
        fontInfos.setSaveSubsetFonts(embedAllFonts);
        
        doc.save(getArtifactsDir() + "Font.FontInfoCollection.docx");
      • get

        public FontInfo get(int index)
        
        Gets a font at the specified index.
        Parameters:
        index - Zero-based index of the font.

        Example:

        Shows how to extract an embedded font from a document, and save it to the local file system.
        Document doc = new Document(getMyDir() + "Embedded font.docx");
        
        FontInfo embeddedFont = doc.getFontInfos().get("Alte DIN 1451 Mittelschrift");
        byte[] embeddedFontBytes = embeddedFont.getEmbeddedFont(EmbeddedFontFormat.OPEN_TYPE, EmbeddedFontStyle.REGULAR);
        
        FileUtils.writeByteArrayToFile(new File(getArtifactsDir() + "Alte DIN 1451 Mittelschrift.ttf"), embeddedFontBytes);
        
        // Embedded font formats may be different in other formats such as .doc.
        // We need to know the correct format before we can extract the font.
        doc = new Document(getMyDir() + "Embedded font.doc");
        
        Assert.assertNull(doc.getFontInfos().get("Alte DIN 1451 Mittelschrift").getEmbeddedFont(EmbeddedFontFormat.OPEN_TYPE, EmbeddedFontStyle.REGULAR));
        Assert.assertNotNull(doc.getFontInfos().get("Alte DIN 1451 Mittelschrift").getEmbeddedFont(EmbeddedFontFormat.EMBEDDED_OPEN_TYPE, EmbeddedFontStyle.REGULAR));
        
        // Also, we can convert embedded OpenType format, which comes from .doc documents, to OpenType.
        embeddedFontBytes = doc.getFontInfos().get("Alte DIN 1451 Mittelschrift").getEmbeddedFontAsOpenType(EmbeddedFontStyle.REGULAR);
        
        FileUtils.writeByteArrayToFile(new File(getArtifactsDir() + "Alte DIN 1451 Mittelschrift.otf"), embeddedFontBytes);
      • get

        public FontInfo get(java.lang.String name)
        
        Gets a font with the specified name.
        Parameters:
        name - Case-insensitive name of the font to locate.

        Example:

        Shows how to extract an embedded font from a document, and save it to the local file system.
        Document doc = new Document(getMyDir() + "Embedded font.docx");
        
        FontInfo embeddedFont = doc.getFontInfos().get("Alte DIN 1451 Mittelschrift");
        byte[] embeddedFontBytes = embeddedFont.getEmbeddedFont(EmbeddedFontFormat.OPEN_TYPE, EmbeddedFontStyle.REGULAR);
        
        FileUtils.writeByteArrayToFile(new File(getArtifactsDir() + "Alte DIN 1451 Mittelschrift.ttf"), embeddedFontBytes);
        
        // Embedded font formats may be different in other formats such as .doc.
        // We need to know the correct format before we can extract the font.
        doc = new Document(getMyDir() + "Embedded font.doc");
        
        Assert.assertNull(doc.getFontInfos().get("Alte DIN 1451 Mittelschrift").getEmbeddedFont(EmbeddedFontFormat.OPEN_TYPE, EmbeddedFontStyle.REGULAR));
        Assert.assertNotNull(doc.getFontInfos().get("Alte DIN 1451 Mittelschrift").getEmbeddedFont(EmbeddedFontFormat.EMBEDDED_OPEN_TYPE, EmbeddedFontStyle.REGULAR));
        
        // Also, we can convert embedded OpenType format, which comes from .doc documents, to OpenType.
        embeddedFontBytes = doc.getFontInfos().get("Alte DIN 1451 Mittelschrift").getEmbeddedFontAsOpenType(EmbeddedFontStyle.REGULAR);
        
        FileUtils.writeByteArrayToFile(new File(getArtifactsDir() + "Alte DIN 1451 Mittelschrift.otf"), embeddedFontBytes);
    • Method Detail

      • contains

        public boolean contains(java.lang.String name)
        Determines whether the collection contains a font with the given name.
        Parameters:
        name - Case-insensitive name of the font to locate.
        Returns:
        True if the item is found in the collection; otherwise, false.

        Example:

        Shows info about the fonts that are present in the blank document.
        Document doc = new Document();
        
        // A blank document contains 3 default fonts. Each font in the document
        // will have a corresponding FontInfo object which contains details about that font.
        Assert.assertEquals(3, doc.getFontInfos().getCount());
        
        Assert.assertTrue(doc.getFontInfos().contains("Times New Roman"));
        Assert.assertEquals(204, doc.getFontInfos().get("Times New Roman").getCharset());
        
        Assert.assertTrue(doc.getFontInfos().contains("Symbol"));
        Assert.assertTrue(doc.getFontInfos().contains("Arial"));
      • iterator

        public java.util.Iterator<FontInfo> iterator()
        Returns an iterator object that can be used to iterate over all items in the collection.

        Example:

        Shows how to access and print details of each font in a document.
        Document doc = new Document(getMyDir() + "Document.docx");
        
        Iterator fontCollectionEnumerator = doc.getFontInfos().iterator();
        while (fontCollectionEnumerator.hasNext())
        {
            FontInfo fontInfo = (FontInfo)fontCollectionEnumerator.next();
            if (fontInfo != null)
            {
                System.out.println("Font name: " + fontInfo.getName());
        
                // Alt names are usually blank.
                System.out.println("Alt name: " + fontInfo.getAltName());
                System.out.println("\t- Family: " + fontInfo.getFamily());
                System.out.println("\t- " + (fontInfo.isTrueType() ? "Is TrueType" : "Is not TrueType"));
                System.out.println("\t- Pitch: " + fontInfo.getPitch());
                System.out.println("\t- Charset: " + fontInfo.getCharset());
                System.out.println("\t- Panose:");
                System.out.println("\t\tFamily Kind: " + (fontInfo.getPanose()[0] & 0xFF));
                System.out.println("\t\tSerif Style: " + (fontInfo.getPanose()[1] & 0xFF));
                System.out.println("\t\tWeight: " + (fontInfo.getPanose()[2] & 0xFF));
                System.out.println("\t\tProportion: " + (fontInfo.getPanose()[3] & 0xFF));
                System.out.println("\t\tContrast: " + (fontInfo.getPanose()[4] & 0xFF));
                System.out.println("\t\tStroke Variation: " + (fontInfo.getPanose()[5] & 0xFF));
                System.out.println("\t\tArm Style: " + (fontInfo.getPanose()[6] & 0xFF));
                System.out.println("\t\tLetterform: " + (fontInfo.getPanose()[7] & 0xFF));
                System.out.println("\t\tMidline: " + (fontInfo.getPanose()[8] & 0xFF));
                System.out.println("\t\tX-Height: " + (fontInfo.getPanose()[9] & 0xFF));
            }
        }