Changing Default Font Formatting (Name Size etc) of entire Text in Word Document using Java Code of Aspose.Words SDK

I was able to solve an issue with incorrectly rendering text by manually editing the document.xml file (after unzipping the docx archive) to change the Fonts ascii “LOFFWC+Arial” (I don’t think that 6 letter prefix is predictable?) to just “Arial” solved the issue.

Is there a way I can accomplish this with the Aspose Words Java sdk? I can automate it by unzipping, editing the XML, and zipping back up but if it can be done with Aspose, that’d be preferable. Thank you!

@jmcginnis,

You can change the font of text in entire Word document by using the following code of Aspose.Words for Java.

Document doc = new Document("C:\\Temp\\input.docx");
FontChanger changer = new FontChanger();
doc.accept(changer);
doc.save("C:\\Temp\\awjava-21.2.docx");

public static 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 = "Calibri"; // or specify some other
    // private double mNewFontSize = 19.0;
}

Exactly what I needed. Thank you