Header font

I am trying to override the Header font, but it only works sometimes. Here is my code. any thoughts?

foreach (HeaderFooter header in firstSection.HeadersFooters)
{
    var isFooter = header.HeaderFooterType == HeaderFooterType.FooterPrimary
        || header.HeaderFooterType == HeaderFooterType.FooterFirst
        || header.HeaderFooterType == HeaderFooterType.FooterEven;

    //Loop through all the paragrapghs in the header
    foreach (Paragraph paragraph in header.Paragraphs)
    {
        paragraph.ParagraphBreakFont.Size = 10;
        paragraph.ParagraphBreakFont.SizeBi = 10;
        paragraph.ParagraphBreakFont.Name = "Arial";
        paragraph.ParagraphBreakFont.NameAscii = "Arial";
        paragraph.ParagraphBreakFont.NameBi = "Arial";
        paragraph.ParagraphBreakFont.NameFarEast = "Arial";
        paragraph.ParagraphBreakFont.NameOther = "Arial";

        //Loop through all the runs in the paragraph
        foreach (Run run in paragraph.Runs)
        {
            run.Font.Name = "Arial";
            run.Font.NameAscii = "Arial";
            run.Font.NameBi = "Arial";
            run.Font.NameFarEast = "Arial";
            run.Font.NameOther = "Arial";
            run.Font.Size = 10;
            run.Font.SizeBi = 10;

            if (isFooter)
            {
                run.Font.Size = 9;
                run.Font.SizeBi = 9;
                run.Font.Bold = true;
                run.Font.BoldBi = true;
            }
        }
    }
}

@brandonlevasseur

To override the header font in your document, you can use the DocumentBuilder class, which simplifies the process of modifying headers and footers. It appears that your current implementation is quite comprehensive, but there are a few things to consider that might help ensure consistency in font application.

Here’s a refined approach to set the header font using DocumentBuilder:

Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);

// Move to the header section
builder.MoveToHeaderFooter(HeaderFooterType.HeaderPrimary);

// Clear existing paragraphs if necessary
builder.CurrentParagraph.RemoveAllChildren();

// Add a new paragraph with the desired font settings
builder.ParagraphFormat.Style.Font.Name = "Arial";
builder.ParagraphFormat.Style.Font.Size = 10;

// Add text to the header
builder.Write("Your Header Text Here");

// If you need to set different fonts for footers, repeat the process for the footer
builder.MoveToHeaderFooter(HeaderFooterType.FooterPrimary);
builder.CurrentParagraph.RemoveAllChildren();
builder.ParagraphFormat.Style.Font.Name = "Arial";
builder.ParagraphFormat.Style.Font.Size = 9; // Example for footer
builder.Write("Your Footer Text Here");

// Save the document
doc.Save("HeaderFooterExample.docx");

Key Points:

  1. Use DocumentBuilder: This class provides a more straightforward way to manipulate headers and footers.
  2. Clear Existing Content: If you want to ensure that the new font settings apply correctly, consider removing existing paragraphs in the header/footer before adding new content.
  3. Set Font Properties: You can set the font properties directly on the ParagraphFormat.Style.Font to ensure that all text added to that paragraph inherits these properties.

This method should help you achieve consistent font settings in your headers and footers. If you continue to experience issues, ensure that there are no conflicting styles or settings applied elsewhere in your document.

For more detailed information on working with headers and footers, you can refer to the official documentation on Working with Headers and Footers and Change or Replace Header and Footer.

Sources:
[1]: Working with Headers and Footers
[2]: Change or Replace Header and Footer

@brandonlevasseur Yu can use DocumentVisitor to change font. For example see the following code:

Document doc = new Document(@"C:\Temp\in.docx");
foreach (HeaderFooter header in doc.FirstSection.HeadersFooters)
{
    header.Accept(new FontChanger("Arial"));
}
doc.Save("C:\\Temp\\out.docx");
internal class FontChanger : DocumentVisitor
{
    public FontChanger(string fontName)
    {
        mFontName = fontName;
    }

    /// <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(Font font)
    {
        font.Name = mFontName;
    }

    private string mFontName = "Arial";
}

If the problem still persists, please attach your input, output and expected documents here for our reference. We will check the issue and provide you more information.