We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

How to set default font formatting of text using Java

We use Aspose Word for Java.

We are getting the font on document as “Times New Roman” but want to use “Calibri” as default one(used when not specified).

I tried below line but still no effect.

FontSettings.getDefaultInstance().getSubstitutionSettings().getDefaultFontSubstitution().setDefaultFontName(“Calibri”);

Regards,
Zeeshan

java_code_and_output.zip (14.4 KB)

The above zip contains java code to generate document and output file generated.

@Zeeshan_Noor_Khan

Please use the DocumentBuilder.Font property as shown below to get the desired output. We suggest you please read the following articles.
Use DocumentBuilder to Insert Document Elements
Using DocumentBuilder to Modify a Document

Document doc = new Document();
DocumentBuilder docBuilder = new DocumentBuilder(doc);
docBuilder.getFont().setName("Calibri");
docBuilder.writeln("A text in a document");
docBuilder.write("A text");
doc.save(MyDir + "Rendering.doc");

Hello Tahir,

Thanks for you reply.

But what i wanted was to set the default font of document.

What we see is that Aspose sets “Arial Unicode MS” font by default. Arial Unicode MS was previously distributed with Microsoft Office, but this ended in 2016 version. Also, this font is no longer supported on Windows servers where documents are commonly generated. As a result, there will be “font substitution” and the documents generated will have unpredictable output formatting. Hence we want “Calibri” as default font.

On my system the default formatting taken is “Times New Roman”(may be it didn’t found “Arial Unicode MS”).
Hence when i clear the current formatting by using below line it falls back to “Times New Roman”.
docBuilder.getFont().clearFormatting();

But instead I want the default formatting to be set as Calibri, and docBuilder.getFont().setName(“Calibri”) only set it temporary.

Regards,
Zeeshan
IBM IELO Publishing.

@Zeeshan_Noor_Khan

You are facing the expected behavior of Aspose.Words. You can use DocumentBuilder to set the font of text and style of paragraph. When you clear the font formatting, you will get the default font as ‘Times New Roman’.

In your case, we suggest you please create a template document that contains ‘Normal’ style for paragraph with your default setting. Import this document into Aspose.Words’ DOM and move the cursor to the desired location and insert the text.

If you still face problem, please share some more detail about your requirement. We will then provide you more information about your query.

Hi Tahir,

Aspose Cells for java provides a way to change the default font on whole excel document by below code:
Style style = this.workBook.getDefaultStyle();
Font font = style.getFont();
font.setName(“Calibri”);//$NON-NLS-1$

Aspose Word should also have such feature, to change default font on whole document. Please let me know your view on this.

@Zeeshan_Noor_Khan

Unfortunately, Aspose.Words does not has same API. However, you can set the font formatting of whole document.

Please use the following code example to change the font name and size of whole document. Hope this helps you.

Document doc = new Document(MyDir + "input.docx");
FontChanger changer = new FontChanger();
doc.accept(changer);

doc.save(MyDir + "output.docx");

public class FontChanger extends DocumentVisitor
{
///
/// Called when a FieldEnd node is encountered in the document.
///
public int VisitFieldEnd(final FieldEnd fieldEnd)
        {
        //Simply change font name
        ResetFont(fieldEnd.getFont());
        return VisitorAction.CONTINUE;
        }

///
/// Called when a FieldSeparator node is encountered in the document.
///
public int VisitFieldSeparator(final FieldSeparator fieldSeparator)
        {
        ResetFont(fieldSeparator.getFont());
        return VisitorAction.CONTINUE;
        }

///
/// Called when a FieldStart node is encountered in the document.
///
public int VisitFieldStart(final FieldStart fieldStart)
        {
        ResetFont(fieldStart.getFont());
        return VisitorAction.CONTINUE;
        }

///
/// Called when a Footnote end is encountered in the document.
///
public int VisitFootnoteEnd(final Footnote footnote)
        {
        ResetFont(footnote.getFont());
        return VisitorAction.CONTINUE;
        }

///
/// Called when a FormField node is encountered in the document.
///
public int VisitFormField(final FormField formField)
        {
        ResetFont(formField.getFont());
        return VisitorAction.CONTINUE;
        }

///
/// Called when a Paragraph end is encountered in the document.
///
public int VisitParagraphEnd(final Paragraph paragraph)
        {
        ResetFont(paragraph.getParagraphBreakFont());
        return VisitorAction.CONTINUE;
        }

///
/// Called when a Run node is encountered in the document.
///
public int visitRun(final Run run)
        {
        ResetFont(run.getFont());
        return VisitorAction.CONTINUE;
        }

///
/// Called when a SpecialChar is encountered in the document.
///
public int VisitSpecialChar(final SpecialChar specialChar)
        {
        ResetFont(specialChar.getFont());
        return VisitorAction.CONTINUE;
        }

private void ResetFont(Font font)
        {
            font.setName(mNewFontName);
            font.setSize(mNewFontSize);

        }

    private String mNewFontName = "Arial";
    private double mNewFontSize = 19.0;
}