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

Free Support Forum - aspose.com

Change font of whole document

hi may i know how to force the whole page to have a specific font whole document sorry my problem is the data is comming from the db and its html so there are different fonts i needed to make it consistent

Hi Joebet,

Thanks for your inquiry.

Thanks for your inquiry. Once the document is loaded
into Aspose.Words’ DOM, you can use code like below to change the font
size of the entire Word document.

You can load the document from database and save it into database. Please read following documentation link.
https://docs.aspose.com/words/net/serialize-and-work-with-a-document-in-a-database/

Document doc = new Document(@"C:\Temp\input.html");
FontChanger changer = new FontChanger();
doc.Accept(changer);
doc.Save(@"C:\Temp\out.docx");
/// 
/// Class inherited from DocumentVisitor, that chnges font.
/// 
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 = mNewFont;
    }

    //Font by default
    private string mNewFont = "Arial";
}

I hope, this helps.

Works!

thanks!

I have another question since the data is comming from the html. it will ruin the table contents example.

http://www.mediafire.com/view/203ucy62wg7g2vq/MultieStateLaw_(13).doc

i needed not to include the other h1 and h2 that is inside the body… below is my code.

Aspose.Words.License l = new Aspose.Words.License();
l.SetLicense("Aspose.Words.lic");
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
builder.ParagraphFormat.StyleIdentifier = StyleIdentifier.BodyText;
builder.ParagraphFormat.Alignment = ParagraphAlignment.Center;
builder.Font.Bold = true;
builder.Write("Table of Contents");
builder.Writeln(Environment.NewLine);
builder.Font.Bold = false;
builder.InsertTableOfContents(@"\o ""1-3"" \h \z \u");
builder.InsertBreak(BreakType.PageBreak);
builder.ParagraphFormat.Alignment = ParagraphAlignment.Left;
if (topicHeaderOnTopDesign)
{
    #region Topic Top
    foreach (var innerItem in statelList)
    {
        builder.ParagraphFormat.StyleIdentifier = StyleIdentifier.Heading1;
        builder.Writeln(innerItem.Text);
        foreach (var item in jModelList)
        {
            builder.ParagraphFormat.StyleIdentifier = StyleIdentifier.Heading2;
            builder.Writeln(item.Text);
            builder.ParagraphFormat.StyleIdentifier = StyleIdentifier.BodyText;
            // builder.Writeln(GetStateHtmlForWord("\HR State Chart Builder\" + item.Text + "\" + innerItem.Text));
            builder.InsertHtml(GetStateHtmlForWord("\\HR State Chart Builder\\" + item.Text + "\\" + innerItem.Text));
        }
        builder.InsertBreak(BreakType.PageBreak);
    }
    #endregion
}
else
{
    #region State Top
    foreach (var innerItem in jModelList)
    {
        builder.ParagraphFormat.StyleIdentifier = StyleIdentifier.Heading1;
        builder.Writeln(innerItem.Text);
        foreach (var item in statelList)
        {
            builder.ParagraphFormat.StyleIdentifier = StyleIdentifier.Heading2;
            builder.Writeln(item.Text);
            builder.ParagraphFormat.StyleIdentifier = StyleIdentifier.BodyText;
            builder.InsertHtml(GetStateHtmlForWord("\\HR State Chart Builder\\" + innerItem.Text + "\\" + item.Text));
        }
        builder.InsertBreak(BreakType.PageBreak);
    }
    #endregion
}
doc.UpdateFields();
FontChanger changer = new FontChanger();
doc.Accept(changer);
string unique = Guid.NewGuid().ToString();
string path = Server.MapPath("~/Apps_and_Tools/MultiStateLawsComparisonTool/temp/") + unique + ".doc";
doc.Save(path, SaveFormat.Doc);
Response.ContentType = "application/msword";
Response.AddHeader("content-disposition", "attachment; filename=MultieStateLaw");
Response.WriteFile(path);
Response.End();

i needed to exlcude anyting that “GetStateHtmlForWord” is generated to the table of contents.

Hi Joebet,

Thanks for your inquiry. It would be great if you please share following detail for investigation purposes.

  • Please attach your input Word document.
  • Please

create a standalone/runnable simple application (for example a Console
Application Project
) that demonstrates the code (Aspose.Words code) you used to generate
your output document

  • Please attach the output Word file that shows the undesired behavior.
  • Please
    attach your target Word document showing the desired behavior. You can
    use Microsoft Word to create your target Word document. I will
    investigate as to how you are expecting your final document be generated
    like.

Unfortunately,
it is difficult to say what the problem is without the Document(s) and
simplified application. We need your Document(s) and simple project to
reproduce the problem. As soon as you get these pieces of information to
us we’ll start our investigation into your issue.