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

Free Support Forum - aspose.com

How to set footer text font of a section to arial

Hi,
I want to set footer text font of a section in a document to font type “Arial”. Kindly tell me how can i achieve this.

Hi,
I want to set font of a particulat section to arial and i am using the following code

foreach (Section sect in doc)
{
    Range rang = sect.Range;
    if (rang.Text != string.Empty && rang.Text.ToLower().Contains("i am fine"))
    {
        if (sect.Body != null && sect.Body.Paragraphs != null)
        {
            for (int j = 0; j < sect.Body.Paragraphs.Count; j++)
            {
                if (sect.Body.Paragraphs[j] != null)
                {
                    sect.Body.Paragraphs[j].ParagraphFormat.Style.Font.Name = "Arial";
                }
            }
        }
    }

But it is not working. I attatched a document. In that i want to set first section’s font to arial type. Kindly tell me how can achieve this.
And also i want to find second section’s footer text font size in the attatched document.Kindly tell me solution to this also.
PFA the document.

Hi there,

Thanks for your inquiry. In your case, I suggest you please use the following code example to achieve your requirements.

DocumentVisitor Class is base class for custom document visitors. With DocumentVisitor you can define and execute custom operations that require enumeration over the document tree.

For example, Aspose.Words uses DocumentVisitor internally for saving Document in various formats and for other operations like finding fields or bookmarks over a fragment of a document.

Document doc = new Document(MyDir + "document.doc");
FontChanger fontchanger = new FontChanger("Arial");
foreach (Section section in doc.Sections)
{
    Range rang = section.Range;
    if (rang.Text != string.Empty && rang.Text.ToLower().Contains("i am fine"))
    {
        section.Accept(fontchanger);
    }
}
doc.Save(MyDir + "Out.doc");
/// 
/// Class inherited from DocumentVisitor, that chnges font.
/// 
class FontChanger : DocumentVisitor
{
    public FontChanger()
    {
    }
    public FontChanger(string fontName)
    {
        mNewFont = fontName;
    }
    /// 
    /// 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.SkipThisNode;
    }
    /// 
    /// Called when a FieldSeparator node is encountered in the document.
    /// 
    public override VisitorAction VisitFieldSeparator(FieldSeparator fieldSeparator)
    {
        ResetFont(fieldSeparator.Font);
        return VisitorAction.SkipThisNode;
    }
    /// 
    /// Called when a FieldStart node is encountered in the document.
    /// 
    public override VisitorAction VisitFieldStart(FieldStart fieldStart)
    {
        ResetFont(fieldStart.Font);
        return VisitorAction.SkipThisNode;
    }
    /// 
    /// 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 = mNewFont;
        font.Size = mNewFontSize;
    }
    // Font by default
    private string mNewFont = "Arial";
    public double mNewFontSize = 12;
}

I hope, this helps.

Hi there,

Thanks for your inquiry.

*SankarGanesh M:

I want to set footer text font of a section in a document to font type “Arial”. Kindly tell me how can i achieve this.*

Please use the following code example to achieve your requirements.

Document doc = new Document(MyDir + "document.doc");
FontChanger fontchanger = new FontChanger("Arial");
foreach (Section section in doc.Sections)
{
    foreach (HeaderFooter hf in section.HeadersFooters)
    {
        if (hf.HeaderFooterType == HeaderFooterType.FooterPrimary)
            hf.Accept(fontchanger);
    }
}
doc.Save(MyDir + "Out.doc");

Hi,
Thanks for your reply.
Can you please tell me how can i get and set footer text font size and font type??

Hi there,

Thanks for your inquiry. Please use the code share in this post to set the font of text inside header/footer of a section.

Please note
that formatting is applied on a few different levels. For example, let’s
consider formatting of simple text. Text in documents is represented by
Run element and a Run can only be a child of a Paragraph. You can apply
formatting 1) to Run nodes by using Character Styles e.g. a Glyph Style, 2) to the parent of those Run nodes i.e. a Paragraph node (possibly via paragraph Styles) and 3) you can also apply direct formatting to Run nodes by using Run attributes (Font). In this case the Run will inherit formatting of Paragraph Style, a Glyph Style and then direct formatting.

Please use Run.Font property to get access to the font formatting of this object and use the ParagraphFormat.Style gets or sets the paragraph style applied. Hope this answers your query. Please let us know if you have any more queries.

Hi,I am using the following piece of code fro finding the footer tet font name

foreach (Section sec in doc.Sections)
{
    HeaderFooter footer;
    footer = sec.HeadersFooters[HeaderFooterType.FooterPrimary]if (footer != null)
    {
        if (footer.Paragraphs[0] != null)
            a = footer.Paragraphs[0].ParagraphFormat.Style.Font.Name;
    }
}

But this is not working it is not returning the appropriate font name. My footer is of font ‘Arial’ but the above code is showing ‘Courire new’.
Kindly tell me how can i get footer text font name and size.
PFA the input document.

Hi there,

Thanks for your inquiry.

Please
use Run.Font property to get access to the font formatting of this
object. Please check the following code snippet.

Document doc = new Document(MyDir + "input—footer.doc");
foreach (Section section in doc.Sections)
{
    foreach (HeaderFooter hf in section.HeadersFooters)
    {
        if (hf.ToString(SaveFormat.Text).Trim() != "")
        {
            foreach (Run run in hf.GetChildNodes(NodeType.Run, true))
            {
                Console.WriteLine(run.Font.Name + " -- " + run.Font.Size);
            }
        }
    }
}

Please let us know if you
have any more queries.