Hi ,
I have upgraded aspose words and pdf to latest version i.e from 19.10 to 24.10 I have tested few scenarios like retreive chinese font substitution and japanese as well both are not working as expected
This is the code I am using :
private void substituteFonts(Document sourceDoc, Map<String, String> fontSubstitutions) {
@SuppressWarnings("unchecked")
NodeCollection<Run> runs = sourceDoc.getChildNodes(NodeType.RUN, true);
log.info("Substitute fonts for run elements");
for (Run run : runs) {
if (run.getFont() != null && fontSubstitutions.containsKey(run.getFont().getName())) {
run.getFont().setName(fontSubstitutions.get(run.getFont().getName()));
}
}
log.info("Substitute fonts for form fields");
for (Section section : sourceDoc.getSections()) {
for (FormField field : section.getRange().getFormFields()) {
if (field.getFont() != null && fontSubstitutions.containsKey(field.getFont().getName())) {
field.getFont().setName(fontSubstitutions.get(field.getFont().getName()));
}
}
}
}
Is there anything I need to update in the code related to fonts as it is compiling successfully for me
@harsha23
It seems that after upgrading to the latest versions of Aspose.Words and Aspose.PDF, you are experiencing issues with font substitution, particularly for Chinese and Japanese fonts.
To address font substitution in Aspose.Words, you may want to utilize the FontSubstitutionSettings class, which allows you to define rules for font substitution. The substitution process follows a specific order of rules, starting with font name substitution, followed by font config substitution, and so on. If the first rule does not resolve the font, the next rule is checked, and this continues until a match is found or all rules are exhausted.
Here’s a brief overview of how you can set up font substitution:
-
Define Font Substitution Settings: You can create an instance of FontSettings and configure the SubstitutionSettings to specify your font substitutions.
-
Set Default Font: If a specific font cannot be found, you can set a default font that will be used in its place.
-
Load the Document with Font Settings: When loading your document, ensure that you apply the FontSettings to the LoadOptions.
Here’s an example of how you might implement this in your code:
FontSettings fontSettings = new FontSettings();
fontSettings.getSubstitutionSettings().getDefaultFontSubstitution().setDefaultFontName("Arial Unicode MS");
fontSettings.getSubstitutionSettings().getFontInfoSubstitution().setEnabled(false); // Disable font info substitution if needed
LoadOptions loadOptions = new LoadOptions();
loadOptions.setFontSettings(fontSettings);
Document sourceDoc = new Document("path/to/your/document.docx", loadOptions);
Make sure to replace "Arial Unicode MS" with the font you want to use as a fallback. This setup should help in resolving issues with missing fonts, especially for languages like Chinese and Japanese.
If you continue to face issues, consider checking the font availability on your system and ensure that the fonts you are trying to substitute are correctly installed and accessible.
For more detailed guidance on working with fonts in Aspose.Words, you can refer to the official documentation on font processing and substitution settings.
If you have any further questions or need additional assistance, feel free to ask!
@harsha23 It looks like your goal is to change font in your document. Please try using DocumentVisitor to achieve this:
Document doc = new Document("C:\\Temp\\in.html");
// Change font to SimSun 10.5
doc.accept(new FontChanger("SimSun", 10.5));
doc.save("C:\\Temp\\out.doc");
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;
}
If the problem still persist, please attach your input, output and expected output documents here for our reference. We will check the issue and provide you more information.
Is there any changes made to font in latest version as it is unable to recognise the fonts for few templates
@harsha23 I am not aware about such changes. Could you please attach the problematic documents here for our reference?