com.aspose.words
Class FontInfoCollection

java.lang.Object
    extended by 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(true);
fontInfos.setEmbedSystemFonts(false);
fontInfos.setSaveSubsetFonts(false);

doc.save(getMyDir() + "/Artifacts/Document.docx");

Example:

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

FontInfoCollection fonts = doc.getFontInfos();
int fontIndex = 1;

// The fonts info extracted from this document does not necessarily mean that the fonts themselves are
// used in the document. If a font is present but not used then most likely they were referenced at some time
// and then removed from the Document.
for (FontInfo info : fonts) {
    // Print out some important details about the font.
    System.out.println(MessageFormat.format("Font #{0}", fontIndex));
    System.out.println(MessageFormat.format("Name: {0}", info.getName()));
    System.out.println(MessageFormat.format("IsTrueType: {0}", info.isTrueType()));
    fontIndex++;
}
See Also:
FontInfo, DocumentBase.FontInfos

Property Getters/Setters Summary
intgetCount()
           Gets the number of elements contained in the collection.
booleangetEmbedSystemFonts()
voidsetEmbedSystemFonts(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.

booleangetEmbedTrueTypeFonts()
voidsetEmbedTrueTypeFonts(boolean value)
           Specifies whether or not to embed TrueType fonts in a document when it is saved. Default value for this property is false.
booleangetSaveSubsetFonts()
voidsetSaveSubsetFonts(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.

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.
// Create a new document
Document doc = new Document();
// A blank document comes with 3 fonts
Assert.assertEquals(doc.getFontInfos().getCount(), 3);
Assert.assertEquals(doc.getFontInfos().contains("Times New Roman"), true);
Assert.assertEquals(doc.getFontInfos().contains("Symbol"), true);
Assert.assertEquals(doc.getFontInfos().contains("Arial"), true);

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(true);
fontInfos.setEmbedSystemFonts(false);
fontInfos.setSaveSubsetFonts(false);

doc.save(getMyDir() + "/Artifacts/Document.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(true);
fontInfos.setEmbedSystemFonts(false);
fontInfos.setSaveSubsetFonts(false);

doc.save(getMyDir() + "/Artifacts/Document.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(true);
fontInfos.setEmbedSystemFonts(false);
fontInfos.setSaveSubsetFonts(false);

doc.save(getMyDir() + "/Artifacts/Document.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 embedded font from a document.
Document doc = new Document(getMyDir() + "Font.Embedded.docx");
// Let's get the font we are interested in
FontInfo mittelschriftInfo = doc.getFontInfos().get(2);
// We can now extract this embedded font
byte[] embeddedFontBytes = mittelschriftInfo.getEmbeddedFont(EmbeddedFontFormat.OPEN_TYPE, EmbeddedFontStyle.REGULAR);
Assert.assertNotNull(embeddedFontBytes);
// Then we can save the font to our directory
Files.write(Paths.get(getArtifactsDir() + "Alte DIN 1451 Mittelschrift.ttf"), embeddedFontBytes,
        new StandardOpenOption[]{StandardOpenOption.CREATE, StandardOpenOption.APPEND});

// If we want to extract a font from a .doc as opposed to a .docx, we need to make sure to set the appropriate embedded font format
doc = new Document(getMyDir() + "Font.Embedded.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));

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 embedded font from a document.
Document doc = new Document(getMyDir() + "Font.Embedded.docx");
// Let's get the font we are interested in
FontInfo mittelschriftInfo = doc.getFontInfos().get(2);
// We can now extract this embedded font
byte[] embeddedFontBytes = mittelschriftInfo.getEmbeddedFont(EmbeddedFontFormat.OPEN_TYPE, EmbeddedFontStyle.REGULAR);
Assert.assertNotNull(embeddedFontBytes);
// Then we can save the font to our directory
Files.write(Paths.get(getArtifactsDir() + "Alte DIN 1451 Mittelschrift.ttf"), embeddedFontBytes,
        new StandardOpenOption[]{StandardOpenOption.CREATE, StandardOpenOption.APPEND});

// If we want to extract a font from a .doc as opposed to a .docx, we need to make sure to set the appropriate embedded font format
doc = new Document(getMyDir() + "Font.Embedded.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));

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.
// Create a new document
Document doc = new Document();
// A blank document comes with 3 fonts
Assert.assertEquals(doc.getFontInfos().getCount(), 3);
Assert.assertEquals(doc.getFontInfos().contains("Times New Roman"), true);
Assert.assertEquals(doc.getFontInfos().contains("Symbol"), true);
Assert.assertEquals(doc.getFontInfos().contains("Arial"), true);

iterator

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

Example:

Shows how to get information about each font in a document.
Document doc = new Document(getMyDir() + "Font.Embedded.docx");

// We can iterate over all the fonts with an enumerator
Iterator<FontInfo> fontCollectionEnumerator = doc.getFontInfos().iterator();
// Print detailed information about each font to the console
while (fontCollectionEnumerator.hasNext()) {
    FontInfo fontInfo = fontCollectionEnumerator.next();
    if (fontInfo != null) {
        System.out.println("Font name: " + fontInfo.getName());
        System.out.println("Alt name: " + fontInfo.getAltName()); // Alt names are usually blank
        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));
    }
}

See Also:
          Aspose.Words Documentation - the home page for the Aspose.Words Product Documentation.
          Aspose.Words Support Forum - our preferred method of support.