Change Font (Name, Size) of Word DOCX Document & Convert to PDF using Java Code

Hello there,

I wanted to update the full document by changing the font to Calibri.

The input document contains mix fonts (Arial, Times new roman, Garamond). By updating the document I’m expecting the document in the Calibri font only.

I’ve tried using following code but unable find the solution.

public static void main(String[] args) throws Exception {
    Document srcDoc = new Document("/home/vivek/Desktop/TestInputFile.docx");

    //Update Font
    DocumentBuilder builder = new DocumentBuilder(srcDoc);
    Font font = builder.getFont();
    font.setName("Calibri");

    srcDoc.save("/home/vivek/Desktop/output.docx");
}

is there any other way to update the document with expected font?

Thanks

@Cipet31490,

You can make use of the DocumentVisitor Class of Aspose.Words for Java API to change the Font name (Size and other Font formatting) of entire Word document. Please try the following Java code:

Document doc = new Document("C:\\Temp\\input.docx");
FontChanger changer = new FontChanger();
doc.accept(changer);
doc.save("C:\\Temp\\awjava-20.12.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";
    // private double mNewFontSize = 19.0;
}

Thanks @awais.hafeez. Its working for the installed font in the system

Can you please let me know that, is there any similar way to change the font in pdf using aspose?

@Cipet31490,

Do you want to load Word document with Aspose.Words for Java API, change the font of entire document and then finally save changes to PDF format? If yes, then the following code will work:

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

If you want to process your source PDF file with Aspose.PDF for Java API, then I suggest you to please post a separate topic in Aspose.PDF forum where you will be guided appropriately.

Hello @awais.hafeez:

I’ve tried with the same way its converting with the required font

But the issue is its not converting bullets in the doc/pdf. Please check attached snapshot and let me know if anything else required to do to resolve the same.

image.png (8.1 KB)

@Cipet31490,

Have you tried the latest (21.1) version of Aspose.Words for Java on your end? In case the problem still remains, then please ZIP and upload your input Word document and Aspose.Words generated PDF file showing the undesired behavior here for testing. We will then investigate the issue on our end and provide you more information.

Have created the following class and its working fine now…

class FontChanger(val fontName: String) : DocumentVisitor() {
    /**
     * Called when a Run node is encountered in the document.
     */
    override fun visitRun(run: Run): Int {
        run.font.name = fontName
        return VisitorAction.CONTINUE
    }
}

Use of the above class:

doc.accept(FontChanger("Arial"))

Thanks @awais.hafeez

@Cipet31490,

It is great that you were able to find what you were looking for. Please let us know any time you may have any further queries in future.