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?
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;
}
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");
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.
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
}
}
I have encountered an issue where unwanted tab characters are added to the document after applying font changes using Aspose.Words. Specifically, when I change the font to a new one (e.g., Times New Roman), extra tab characters are inserted into the document. This is causing formatting issues.
I have tried applying the font change using the method discussed in the previous comment, but after the font change, tab characters are present where they weren’t originally. Below are the input and output documents:
Input Document:
The original document is properly formatted, with no unwanted tabs. The content is displayed in a consistent font (e.g., Calibry).
After changing the font to Times New Roman using Aspose.Words, additional tab characters appear in the document, disrupting the layout and alignment of the content.
Could you please provide guidance on how to avoid or remove these extra tab characters while applying font changes? It seems like the font change operation is inadvertently introducing these unwanted tabs.
@nobilex As I have mentioned the tab is not added it is there in the input document. If you change font in MS Word you will see exactly the same result as Aspose.Words.
Also you can unzip the document and see there are 5 tabs in your source document:
Sets consent for sending user data to Google for online advertising purposes.
Sets consent for personalized advertising.
Cookie Notice
To provide you with the best experience, we use cookies for personalization, analytics, and ads. By using our site, you agree to our cookie policy.
More info
Enables storage, such as cookies, related to analytics.
Enables storage, such as cookies, related to advertising.
Sets consent for sending user data to Google for online advertising purposes.
Sets consent for personalized advertising.
Cookie Notice
To provide you with the best experience, we use cookies for personalization, analytics, and ads. By using our site, you agree to our cookie policy.
More info
Enables storage, such as cookies, related to analytics.
Enables storage, such as cookies, related to advertising.
Sets consent for sending user data to Google for online advertising purposes.