Working with Fonts

Getting Font Line Spacing

The line spacing of a font is the vertical distance between the baselines of two consecutive lines of text. Thus, the line spacing includes the blank space between lines along with the height of the character itself.

The LineSpacing property was introduced in the Font class to obtain this value as shown in the example given below:

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java
// The path to the documents directory.
String dataDir = Utils.getDataDir(GetFontLineSpacing.class);
// Initialize document.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
builder.getFont().setName("Calibri");
builder.write("I'm a very nice formatted string.");
// Obtain line spacing.
Font font = builder.getDocument().getFirstSection().getBody().getFirstParagraph().getRuns().get(0).getFont();
System.out.println("lineSpacing = " + font.getLineSpacing());

Font Formatting

Current font formatting is represented by a Font object returned by the DocumentBuilder.Font property. The Font class contains a wide variety of font properties possible in Microsoft Word. The below example shows how to set font formatting.

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java
// Open the document.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
// Specify font formatting before adding text.
Font font = builder.getFont();
font.setSize(16);
font.setColor(Color.blue);
font.setBold(true);
font.setName("Arial");
font.setUnderline(Underline.DASH);
builder.write("Sample text.");
doc.save(dataDir + "SetFontFormatting_out.doc");

Fill properties now are also available for fonts to set fill formatting of text. It gives an ability to change, for example, the foreground color or transparency of text fill.

Font EmphasisMark

The Font class provides EmphasisMark property to get or set EmphasisMark enumeration values to be applied in the formatting. The code example given below demonstrates how to set the EphasisMark property.

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java
Document document = new Document();
DocumentBuilder builder = new DocumentBuilder(document);
builder.getFont().setEmphasisMark(EmphasisMark.UNDER_SOLID_CIRCLE);
builder.write("Emphasis text");
builder.writeln();
builder.getFont().clearFormatting();
builder.write("Simple text");
document.save(dataDir + "FontEmphasisMark_out.doc");