Inserted lists become Arial 11 regardless of default font

Hello,
I am using the Aspose words 21.2(Licensed),
I have set the default font as Garamond and the font size as 12 in the generated doc file. The issue is when I try to add a list inside it, its default font is getting changed in Microsoft Word. The solution I want is consistent font in the whole document.

Also note that this behavior is specific to Microsoft Word editor only, not to other editors. and the font is not changed when I add paragraphs.

@wivo Could you please attach your input and output documents along with code that will allow us to reproduce the problem on our side? We will check the issue and provide you more information.

Formatting of list items are defined in list levels:
https://reference.aspose.com/words/java/com.aspose.words/listlevel/

So most likely in your case different font is defined in the list level.

Here is code snippet and output file.

package nl.nobilex.document.generation.service

import com.aspose.words.Document
import com.aspose.words.DocumentVisitor
import com.aspose.words.Run
import com.aspose.words.VisitorAction

fun main() {
    val doc = Document("Desktop/template.doc")
    doc.accept(FontChanger("Garamond", 14.0))
    doc.save("modified-template.docx")
}

class FontChanger(val fontName: String, val fontSize: Double) : DocumentVisitor() {
    override fun visitRun(run: Run): Int {
        run.font.name = fontName
        run.font.size = fontSize
        return VisitorAction.CONTINUE
    }
}

modified-template.docx (18.8 KB)

@wivo Please try using the following FontChnager:

import com.aspose.words.*;

public class FontChanger extends DocumentVisitor {
    
    public FontChanger(String fontName)  {
        mFontName = fontName;
        mFontSize = 0;
    }
    
    public FontChanger(String fontName, double fontSize)
    {
        mFontName = fontName;
        mFontSize = fontSize;
    }

    /// <summary>
    /// Called when a FieldEnd node is encountered in the document.
    /// </summary>
    @Override
    public int visitFieldEnd(FieldEnd fieldEnd)
    {
        //Simply change font name
        resetFont(fieldEnd.getFont());
        return VisitorAction.CONTINUE;
    }

    /// <summary>
    /// Called when a FieldSeparator node is encountered in the document.
    /// </summary>
    @Override
    public int visitFieldSeparator(FieldSeparator fieldSeparator)
    {
        resetFont(fieldSeparator.getFont());
        return VisitorAction.CONTINUE;
    }

    /// <summary>
    /// Called when a FieldStart node is encountered in the document.
    /// </summary>
    @Override
    public int visitFieldStart(FieldStart fieldStart)
    {
        resetFont(fieldStart.getFont());
        return VisitorAction.CONTINUE;
    }

    /// <summary>
    /// Called when a Footnote end is encountered in the document.
    /// </summary>
    @Override
    public int visitFootnoteEnd(Footnote footnote)
    {
        resetFont(footnote.getFont());
        return VisitorAction.CONTINUE;
    }

    /// <summary>
    /// Called when a FormField node is encountered in the document.
    /// </summary>
    @Override
    public int visitFormField(FormField formField)
    {
        resetFont(formField.getFont());
        return VisitorAction.CONTINUE;
    }

    /// <summary>
    /// Called when a Paragraph end is encountered in the document.
    /// </summary>
    @Override
    public int visitParagraphEnd(Paragraph paragraph)
    {
        resetFont(paragraph.getParagraphBreakFont());
        return VisitorAction.CONTINUE;
    }

    /// <summary>
    /// Called when a Run node is encountered in the document.
    /// </summary>
    @Override
    public int visitRun(Run run)
    {
        resetFont(run.getFont());
        return VisitorAction.CONTINUE;
    }

    /// <summary>
    /// Called when a SpecialChar is encountered in the document.
    /// </summary>
    @Override
    public int visitSpecialChar(SpecialChar specialChar)
    {
        resetFont(specialChar.getFont());
        return VisitorAction.CONTINUE;
    }

    private void resetFont(Font font)
    {
        font.setName(mFontName);
        if(mFontSize > 0)
            font.setSize(mFontSize);
    }

    private String mFontName = "Arial";
    private double mFontSize = 0;
}

Thanks, @alexey.noskov for your reply. This works with numbered lists, bulleted lists, and multilevel lists. However, the issue persists when I add a list with a heading.

@wivo In this case formatting is defined in heading style. So you should change the heading style in your document.

Hi @alexey.noskov the issue persists when I use a different input document. Here is code snippet and output file.

package nl.nobilex.document.generation.service

import com.aspose.words.Document
import com.aspose.words.DocumentVisitor
import com.aspose.words.FieldEnd
import com.aspose.words.FieldSeparator
import com.aspose.words.FieldStart
import com.aspose.words.Font
import com.aspose.words.Footnote
import com.aspose.words.FormField
import com.aspose.words.Paragraph
import com.aspose.words.Run
import com.aspose.words.SpecialChar
import com.aspose.words.VisitorAction


fun main() {
    val doc = Document("/Original-1 (1).docx")
    doc.accept(FontChanger("Garamond", 14.0))
    doc.save("/modified-template2.docx")
}


class FontChanger : DocumentVisitor {
    constructor(fontName: String) {
        mFontName = fontName
        mFontSize = 0.0
    }

    constructor(fontName: String, fontSize: Double) {
        mFontName = fontName
        mFontSize = fontSize
    }

    /// <summary>
    /// Called when a FieldEnd node is encountered in the document.
    /// </summary>
    override fun visitFieldEnd(fieldEnd: FieldEnd): Int {
        //Simply change font name
        resetFont(fieldEnd.font)
        return VisitorAction.CONTINUE
    }

    /// <summary>
    /// Called when a FieldSeparator node is encountered in the document.
    /// </summary>
    override fun visitFieldSeparator(fieldSeparator: FieldSeparator): Int {
        resetFont(fieldSeparator.font)
        return VisitorAction.CONTINUE
    }

    /// <summary>
    /// Called when a FieldStart node is encountered in the document.
    /// </summary>
    override fun visitFieldStart(fieldStart: FieldStart): Int {
        resetFont(fieldStart.font)
        return VisitorAction.CONTINUE
    }

    /// <summary>
    /// Called when a Footnote end is encountered in the document.
    /// </summary>
    override fun visitFootnoteEnd(footnote: Footnote): Int {
        resetFont(footnote.font)
        return VisitorAction.CONTINUE
    }

    /// <summary>
    /// Called when a FormField node is encountered in the document.
    /// </summary>
    override fun visitFormField(formField: FormField): Int {
        resetFont(formField.font)
        return VisitorAction.CONTINUE
    }

    /// <summary>
    /// Called when a Paragraph end is encountered in the document.
    /// </summary>
    override fun visitParagraphEnd(paragraph: Paragraph): Int {
        resetFont(paragraph.paragraphBreakFont)
        return VisitorAction.CONTINUE
    }

    /// <summary>
    /// Called when a Run node is encountered in the document.
    /// </summary>
    override fun visitRun(run: Run): Int {
        resetFont(run.font)
        return VisitorAction.CONTINUE
    }

    /// <summary>
    /// Called when a SpecialChar is encountered in the document.
    /// </summary>
    override fun visitSpecialChar(specialChar: SpecialChar): Int {
        resetFont(specialChar.font)
        return VisitorAction.CONTINUE
    }

    private fun resetFont(font: Font) {
        font.setName(mFontName)
        if (mFontSize > 0) font.setSize(mFontSize)
    }

    private var mFontName = "Arial"
    private var mFontSize = 0.0
}

modified-template2.docx (28.0 KB)

Original-1 (1).docx (17.1 KB)

@wivo Could you please elaborate the problem? As I can see the font in modified-template2.docx is properly changed to Garamond 14. Just as specified in the code.

@alexey.noskovyes you are right, The font changes to Garamond 14 as specified in the code but when I insert bullets in modified-template2.docx, the font changes to Arial 11. I want the font of the inserted list to be Garamond14.

@wivo The above code changes font of existing content in the document. When you create a list item List Paragraph style is applied to the paragraph. This style has Arial font. You can change it using the following code:

doc.getStyles().getByStyleIdentifier(StyleIdentifier.LIST_PARAGRAPH).getFont().setName("Garamond");
doc.getStyles().getByStyleIdentifier(StyleIdentifier.LIST_PARAGRAPH).getFont().setSize(14);