We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Math text is changed to square characters after changing font of Run nodes using NET

Dear Team

We are changing all font used in document to “Times New Roman”. We are facing issue when there are formulas in document. When font is changed in formulas, original content is changing to little boxes. Also some text is not changed. Can you please give reason on why some text is changing and why some text is not changing.
Please find code and input and output files

    public static void FormulaFont(String FileIn, String FileOut)
    {
        Document doc = new Document(FileIn);
        foreach (Section sct in doc)
        {
            NodeCollection Paragraphs = sct.Body.GetChildNodes(NodeType.Paragraph, true);
            foreach (Paragraph para in Paragraphs)
            {
                NodeCollection run1 = para.GetChildNodes(NodeType.Run, true);
                foreach (Run run in run1)
                {
                    Aspose.Words.Font font = run.Font;                      
                    font.Name = "Times New Roman";
                }
            }
        }
        doc.Save(FileOut);
    }

MathFormulaFontChange.zip (18.3 KB)

Thanks and Regards

@crshekharam

Please use the following code example to change the font of whole document. Hope this helps you.

If you still face problem, please share the screenshots of problematic sections of output document. We will then provide you more information on it.

Document doc = new Document(MyDir + "MathFormulaFontChange_input.docx");
FontChanger changer = new FontChanger();
doc.Accept(changer);

doc.Save(MyDir + "out.docx");

class FontChanger : DocumentVisitor
{
    ///
    /// Called when a FieldEnd node is encountered in the document.
    ///
    public override VisitorAction VisitFieldEnd(FieldEnd fieldEnd)
    {
        //Simply change font name
        ResetFont(fieldEnd.Font);
        return VisitorAction.Continue;
    }

    ///
    /// Called when a FieldSeparator node is encountered in the document.
    ///
    public override VisitorAction VisitFieldSeparator(FieldSeparator fieldSeparator)
    {
        ResetFont(fieldSeparator.Font);
        return VisitorAction.Continue;
    }

    ///
    /// Called when a FieldStart node is encountered in the document.
    ///
    public override VisitorAction VisitFieldStart(FieldStart fieldStart)
    {
        ResetFont(fieldStart.Font);
        return VisitorAction.Continue;
    }

    ///
    /// Called when a Footnote end is encountered in the document.
    ///
    public override VisitorAction VisitFootnoteEnd(Footnote footnote)
    {
        ResetFont(footnote.Font);
        return VisitorAction.Continue;
    }

    ///
    /// Called when a FormField node is encountered in the document.
    ///
    public override VisitorAction VisitFormField(FormField formField)
    {
        ResetFont(formField.Font);
        return VisitorAction.Continue;
    }

    ///
    /// Called when a Paragraph end is encountered in the document.
    ///
    public override VisitorAction VisitParagraphEnd(Paragraph paragraph)
    {
        ResetFont(paragraph.ParagraphBreakFont);
        return VisitorAction.Continue;
    }

    ///
    /// Called when a Run node is encountered in the document.
    ///
    public override VisitorAction VisitRun(Run run)
    {
        ResetFont(run.Font);
        return VisitorAction.Continue;
    }

    ///
    /// Called when a SpecialChar is encountered in the document.
    ///
    public override VisitorAction VisitSpecialChar(SpecialChar specialChar)
    {
        ResetFont(specialChar.Font);
        return VisitorAction.Continue;
    }

    private void ResetFont(Aspose.Words.Font font)
    {
        font.Name = mNewFontName;
    }

    private string mNewFontName = "Times New Roman";
}

Hi Tahir
Issue is not resolved even after using new code provided by you. You can clearly see issue in output document that I shared. Text in original document is converted to little boxes.
Regards

@crshekharam

Please check the attached image for your output document. ms word 2016.png (23.3 KB)

MS Word 2016 shows the output without any issue. Could you please share the screenshot of output along with MS Word version that you are using? We will investigate this issue further and provide you more information on it.

Please find screenshots of output. I am too using ms word 2016
image.png (27.3 KB)

@crshekharam

Please make sure that the font used in your document is installed on the machine where you are checking this document. Please also check the same document at some other system.

@tahir.manzoor
I tried in different systems. Problem remains same. Times New Roman is already installed in my system. Could you suggest what fonts should be installed.

@crshekharam

Please note that Aspose.Words mimics the behavior of MS Word. If you try to change the font of Math equation using MS Word, it will not allow you. However, we have logged this problem in our issue tracking system as WORDSNET-20950 . You will be notified via this forum thread once there is an update available on it.

@crshekharam

It is to inform you that the issue which you are facing is actually not a bug in Aspose.Words. So, we have closed this issue (WORDSNET-20950) as ‘Not a Bug’.