I've got a document that is comprised of many other documents. At a particular point, I need to change all of the text in a particular section. I have a reference to the section whose font needs to change.
How can I change the font for just the text inside a particular section?
For some reason, when inserting the contents of one document to my current one, the font for all of the fields changes from "Times New Roman" (which is what the document I'm inserting is set to) to "Courier" (which is the font of the current document I'm inserting into).
So, I'm left which just trying to change the font programmatically. Searching through the documentation doesn't yield results for change the font of a section.
Please use the following code to change the font name of text inside a section:
Document doc = new Document(MyDir + @"input.docx");
Section sec = doc.LastSection;
FontChanger changer = new FontChanger();
sec.Accept(changer);
doc.Save(MyDir + @"17.4.docx");
/// <summary>
/// Class inherited from DocumentVisitor, that changes 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)
{
// Add your font changing code here
font.Name = mNewFontName;
font.Size = mNewFontSize;
}
private double mNewFontSize = 18;
private string mNewFontName = "Times New Roman";
}
Hope, this helps.
I work with Aspose as Developer Evangelist