Reformatting a section font

Could you please direct me to the easiest way to format a section after the fact? I need to go back and change the font properties of all test in a particular section.

Thanks.

Hi,
Thank you for your inquiry.
I think the best thing in this case fit to use a class DocumentVisitor. Please see article here in our documentation: https://reference.aspose.com/words/net/aspose.words/documentvisitor/. If you still have questions, feel free to ask.

Hi Rob,
Thanks for your inquiry.
Viktor is correct, the best way to do this is by using DocumentVisitor. You can find the full implementation from this post here.
Thanks,

Where in the example code do I need to make the format changes? Basically, all the text in section of the four sections of my document needs to be converted to size 10, black, and new times roman. Note that I have not generated the document yet, but still have it in the builder.

Thanks.

Hi

Thanks for your request. The best way to change font name and font size in the whole document, is using DocumentVisitor. Here is simple example:

// Open document
Document doc = new Document("C:\\Temp\\in.doc");
DocumentBuilder builder = new DocumentBuilder(doc);
FontChanger changer = new FontChanger("Arrial", 10, System.Drawing.Color.Red);
builder.Document.Accept(changer);
doc.Save("C:\\Temp\\out.doc");
class FontChanger: DocumentVisitor
{
    public FontChanger(string fontName, double fontSize, System.Drawing.Color fontColor)
    {
        mFontName = fontName;
        mFontSize = fontSize;
        mFontColor = fontColor;
    }
    ///
    /// Called when a FieldEnd node is encountered in the document.
    ///
    public override VisitorAction VisitFieldEnd(Aspose.Words.Fields.FieldEnd fieldEnd)
    {
        // Simply change font name
        ChangeFont(fieldEnd.Font);
        return VisitorAction.Continue;
    }
    ///
    /// Called when a FieldSeparator node is encountered in the document.
    ///
    public override VisitorAction VisitFieldSeparator(Aspose.Words.Fields.FieldSeparator fieldSeparator)
    {
        ChangeFont(fieldSeparator.Font);
        return VisitorAction.Continue;
    }
    ///
    /// Called when a FieldStart node is encountered in the document.
    ///
    public override VisitorAction VisitFieldStart(Aspose.Words.Fields.FieldStart fieldStart)
    {
        ChangeFont(fieldStart.Font);
        return VisitorAction.Continue;
    }
    ///
    /// Called when a Footnote end is encountered in the document.
    ///
    public override VisitorAction VisitFootnoteEnd(Footnote footnote)
    {
        ChangeFont(footnote.Font);
        return VisitorAction.Continue;
    }
    ///
    /// Called when a FormField node is encountered in the document.
    ///
    public override VisitorAction VisitFormField(Aspose.Words.Fields.FormField formField)
    {
        ChangeFont(formField.Font);
        return VisitorAction.Continue;
    }
    ///
    /// Called when a Paragraph end is encountered in the document.
    ///
    public override VisitorAction VisitParagraphEnd(Paragraph paragraph)
    {
        ChangeFont(paragraph.ParagraphBreakFont);
        return VisitorAction.Continue;
    }
    ///
    /// Called when a Run node is encountered in the document.
    ///
    public override VisitorAction VisitRun(Run run)
    {
        ChangeFont(run.Font);
        return VisitorAction.Continue;
    }
    ///
    /// Called when a SpecialChar is encountered in the document.
    ///
    public override VisitorAction VisitSpecialChar(SpecialChar specialChar)
    {
        ChangeFont(specialChar.Font);
        return VisitorAction.Continue;
    }
    private void ChangeFont(Font font)
    {
        font.Name = mFontName;
        font.Size = mFontSize;
        font.Color = mFontColor;
    }
    private string mFontName = "Arial";
    private double mFontSize = 10;
    private System.Drawing.Color mFontColor = System.Drawing.Color.Black;
}

Best regards,

Hi Andrey,

I just need to change one section in my document, not the whole document. Also does the document have to saved first and then reopened before I can use this method? Can I apply the method to just a the DocumentBuilder?

Thanks!

-Rob

Hi Rob,

In this case you should just need to change the one line of code:

// Open document
Document doc = new Document("C:\\Temp\\in.doc");
DocumentBuilder builder = new DocumentBuilder(doc);
FontChanger changer = new FontChanger("Arrial", 10, System.Drawing.Color.Red);
// Apply changes for FIRST section
builder.Document.Sections[0].Accept(changer);
doc.Save("C:\\Temp\\out.doc");

You do not need saving the document before applying changer.
Best regards,