How to apply a specific style to all content in a footer

I have created a custom type for a document object. How would I apply that style to the entire content of a footer or header where the footer/header already has text?

@dkapell,

To ensure a timely and accurate response, please ZIP and attach the following resources here for testing:

  • Your sample input Word document
  • Aspose.Words generated output document showing the undesired behavior (if any)
  • Your expected document which shows the correct output. Please create this document by using Microsoft Word application.

As soon as you get these pieces of information ready, we will start investigation into your issue and provide you code to achieve the same by using Aspose.Words. Thanks for your cooperation.

footerstyle.zip (4.8 MB)

Attached is the console app, as it is, with the input file and desired output file.

thanks,

@dkapell,

Please try using the following code:

Document doc = new Document("D:\\temp\\footerstyle\\input.docx");

foreach (Section sec in doc.Sections)
{
    foreach (HeaderFooter hf in sec.HeadersFooters)
    {
        if (hf.HeaderFooterType == HeaderFooterType.FooterFirst ||
            hf.HeaderFooterType == HeaderFooterType.FooterEven ||
            hf.HeaderFooterType == HeaderFooterType.FooterPrimary)
        {
            foreach (Paragraph para in hf.GetChildNodes(NodeType.Paragraph, true))
            {
                Style style = doc.Styles["FooterStyle1"];
                if (style == null)
                {
                    style = doc.Styles.Add(StyleType.Paragraph, "FooterStyle1");
                    style.Font.Size = 24;
                    style.Font.Name = "Verdana";
                    style.ParagraphFormat.SpaceAfter = 12;

                }
                para.ParagraphFormat.Style = style;
            }
        }
    }
}

doc.Save("D:\\Temp\\footerstyle\\18.5.docx");

That seems to work to some extent. The font type for the entire footer is set to Verdana, but the font size is only changed for the hanging carriage return. See attached image

ScreenShot923.png (707 Bytes)

@dkapell,

To get desired results, you can remove direct font formatting applied to Run nodes inside Paragraphs. Please try the following code:

Document doc = new Document("D:\\temp\\footerstyle\\input.docx");

foreach (Section sec in doc.Sections)
{
    foreach (HeaderFooter hf in sec.HeadersFooters)
    {
        if (hf.HeaderFooterType == HeaderFooterType.FooterFirst ||
            hf.HeaderFooterType == HeaderFooterType.FooterEven ||
            hf.HeaderFooterType == HeaderFooterType.FooterPrimary)
        {
            foreach (Paragraph para in hf.GetChildNodes(NodeType.Paragraph, true))
            {
                foreach(Run run in para.Runs)
                {
                    run.Font.ClearFormatting();
                }

                Style style = doc.Styles["FooterStyle1"];
                if (style == null)
                {
                    style = doc.Styles.Add(StyleType.Paragraph, "FooterStyle1");
                    style.Font.Size = 24;
                    style.Font.Name = "Verdana";
                    style.ParagraphFormat.SpaceAfter = 12;

                }
                para.ParagraphFormat.Style = style;
            }
        }
    }
}

doc.Save("D:\\Temp\\footerstyle\\18.5.docx");