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

Free Support Forum - aspose.com

Set custom font in Mail Merge

Is there a way I can set a custom font when using mail merging.

Document doc = new Document(path);
 // mail merge stuff
 FontSettings.DefaultFontName = "Forte";
doc.MailMerge.ExecuteWithRegions(dt);

This doesn’t work. Any help please?

@Raed_Alaraj

Thanks for your inquiry. Please check following sample code snippet to change document font. Hopefully it will help you to accomplish the task. However, if there is some difference in your requirement and my understanding then please share your input and expected output documents with your sample code here. We will further look into it and will guide you accordingly.

Document doc = new Document("\input.doc");
....
....
FontChanger changer = new FontChanger();
doc.Accept(changer);
doc.Save(@"C:\Temp\out.doc");
/////////////

 /// <summary>
    /// Class inherited from DocumentVisitor, that chnges font.
    /// </summary>
    class FontChanger : DocumentVisitor
    {
        /// <summary>
        /// Called when a FieldEnd node is encountered in the document.
        /// </summary>
        public override VisitorAction VisitFieldEnd(FieldEnd fieldEnd)
        {
            //Simply change font name
            ResetFont(fieldEnd.Font);
            return VisitorAction.Continue;
        }

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

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

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

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

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

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

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

        private void ResetFont(Aspose.Words.Font font)
        {
            font.Name = mNewFont;
            //if (font.SmallCaps == true)
            //     font.AllCaps = true;
           // font.Hidden = mHidden;
        }

        //Font by default
        private string mNewFont = "Arial Narraow";
        //private bool mHidden = true;
    }

Best Regards,