Check whether required data is present in footer

Hi team, i need help to check two conditions.

  1. Add paragraph data if not present in footer. If paragraph is present ignore it check for next footer
  2. Add page number if not present in footer. If page no is present ignore it check for next footer

Both are independent, one can exist or both can exist or both are missing.
Paragraph content is ā€œA footer in a Microsoft Word document is a section that appears at the bottom of every page.ā€.

@tanzeel123 unfortunately there is not easy way to evaluate this with a single method, but you can use the following code to achieve what you want:

public void Main()
{
    string textToFind = "A footer in a Microsoft Word document is a section that appears at the bottom of every page.";
    Document doc = new Document("C:\\Temp\\input.docx");
    Document clonDoc = (Document)doc.Clone(true);
    DocumentBuilder builder = new DocumentBuilder(clonDoc);

    int sectionIndex = 0;
    foreach (Section sec in clonDoc.Sections)
    {
        // Set Primary Footer, because is the default
        CheckAndAddPageInFooter(HeaderFooterType.FooterPrimary, sec, doc, builder, sectionIndex);
        CheckAndAddTextInFooter(HeaderFooterType.FooterPrimary, sec, doc, textToFind);

        // Check if First Footer is different and set it
        if (sec.PageSetup.DifferentFirstPageHeaderFooter)
        {
            CheckAndAddPageInFooter(HeaderFooterType.FooterFirst, sec, doc, builder, sectionIndex);
            CheckAndAddTextInFooter(HeaderFooterType.FooterFirst, sec, doc, textToFind);
        }

        // Check if Footer is different in odd and even pages and set it
        if (sec.PageSetup.OddAndEvenPagesHeaderFooter)
        {
            CheckAndAddPageInFooter(HeaderFooterType.FooterEven, sec, doc, builder, sectionIndex);
            CheckAndAddTextInFooter(HeaderFooterType.FooterEven, sec, doc, textToFind);
        }

        sectionIndex++;
    }

    clonDoc.Save("C:\\Temp\\output.docx");
}

public void CheckAndAddPageInFooter(HeaderFooterType footerType, Section sec, Document doc, DocumentBuilder builder, int sectionIndex)
{
    HeaderFooter headerFooter = sec.HeadersFooters.FirstOrDefault(hf =>
        !((HeaderFooter)hf).IsHeader
        && ((HeaderFooter)hf).HeaderFooterType == footerType) as HeaderFooter;

    // Check if there is a HeaderFooter of this type and if the existing contains a PAGE field
    if (headerFooter == null || !headerFooter.Range.Text.Contains("\u0013PAGE\u0014"))
    {
        builder.MoveToSection(sectionIndex);
        builder.MoveToHeaderFooter(footerType);
        builder.InsertField("PAGE");
    }
}

public void CheckAndAddTextInFooter(HeaderFooterType footerType, Section sec, Document doc, string textToFind)
{
    var foundText = false;
    HeaderFooter headerFooter = sec.HeadersFooters.FirstOrDefault(hf =>
        !((HeaderFooter)hf).IsHeader
        && ((HeaderFooter)hf).HeaderFooterType == footerType) as HeaderFooter;

    if (headerFooter != null)
    {
        var found = headerFooter.Range.Replace(textToFind, textToFind);
        foundText = found > 0;
    }
    else
    {
        HeaderFooter footer = new HeaderFooter(doc, footerType);
        sec.AppendChild(footer);

        headerFooter = sec.HeadersFooters.FirstOrDefault(hf =>
            !((HeaderFooter)hf).IsHeader
            && ((HeaderFooter)hf).HeaderFooterType == footerType) as HeaderFooter;
    }

    if (!foundText)
    {
        headerFooter.AppendParagraph(textToFind);
    }
}

If you want to know more about how to deal with Headers and Footers you can learn more following this link

Thank you @Eduardo_Canal. But the format is paragraph comes first and then below it comes the pages number. In the above solution i get page number first then i get the paragraph contents. Can you please help me with this.

@tanzeel123 can you please attach an example file with the expected result?

Sure,
Present_Output.docx (26.2 KB)
Expected_Output.docx (26.3 KB)

@tanzeel123 to achieve what you want you need to perform a small change to the methods provided in order to add paragraph and page number in the same way:

public void Main()
{
    string textToFind = "A footer in a Microsoft Word document is a section that appears at the bottom of every page.";
    Document doc = new Document("C:\\Temp\\input.docx");
    Document clonDoc = (Document)doc.Clone(true);
    DocumentBuilder builder = new DocumentBuilder(clonDoc);

    int sectionIndex = 0;
    foreach (Section sec in clonDoc.Sections) {
        // Set Primary Footer, because is the default
                
        CheckAndAddTextInFooter(HeaderFooterType.FooterPrimary, sec, clonDoc, builder, sectionIndex, textToFind);
        builder.InsertBreak(BreakType.LineBreak);
        CheckAndAddPageInFooter(HeaderFooterType.FooterPrimary, sec, clonDoc, builder, sectionIndex);
        builder.InsertBreak(BreakType.ParagraphBreak);

        // Check if First Footer is different and set it
        if (sec.PageSetup.DifferentFirstPageHeaderFooter)
        {
            CheckAndAddTextInFooter(HeaderFooterType.FooterFirst, sec, clonDoc, builder, sectionIndex, textToFind);
            builder.InsertBreak(BreakType.LineBreak);
            CheckAndAddPageInFooter(HeaderFooterType.FooterFirst, sec, clonDoc, builder, sectionIndex);
            builder.InsertBreak(BreakType.ParagraphBreak);
        }

        // Check if Footer is different in odd and even pages and set it
        if (sec.PageSetup.OddAndEvenPagesHeaderFooter)
        {
            CheckAndAddTextInFooter(HeaderFooterType.FooterEven, sec, clonDoc, builder, sectionIndex, textToFind);
            builder.InsertBreak(BreakType.LineBreak);
            CheckAndAddPageInFooter(HeaderFooterType.FooterEven, sec, clonDoc, builder, sectionIndex);
            builder.InsertBreak(BreakType.ParagraphBreak);
        }

        sectionIndex++;
    }

    clonDoc.Save("C:\\Temp\\output.docx");
}

public void CheckAndAddPageInFooter(HeaderFooterType footerType, Section sec, Document doc, DocumentBuilder builder, int sectionIndex)
{
    HeaderFooter headerFooter = sec.HeadersFooters.FirstOrDefault(hf =>
                !((HeaderFooter)hf).IsHeader
                && ((HeaderFooter)hf).HeaderFooterType == footerType) as HeaderFooter;

    if (headerFooter == null || !headerFooter.Range.Text.Contains("\u0013PAGE\u0014"))
    {
        builder.MoveToSection(sectionIndex);
        builder.MoveToHeaderFooter(footerType);
        builder.InsertParagraph();
                
        builder.MoveToParagraph(headerFooter.Paragraphs.Count - 1, 0);
        builder.Write("Page ");
        builder.InsertField(FieldType.FieldPage, true);
        builder.Write(" of "); 
        builder.InsertField(FieldType.FieldNumPages, true);
    }
}

public void CheckAndAddTextInFooter(HeaderFooterType footerType, Section sec, Document doc, DocumentBuilder builder, int sectionIndex, string textToFind)
{
    var (foundText, headerFooter) = CheckTextAndAddFooter(footerType, sec, doc, textToFind);

    if (!foundText)
    {
        builder.MoveToSection(sectionIndex);
        builder.MoveToHeaderFooter(footerType);
        builder.ParagraphFormat.Alignment = ParagraphAlignment.Center;
        builder.Writeln(textToFind);
    }
}

public (bool, HeaderFooter) CheckTextAndAddFooter(HeaderFooterType footerType, Section sec, Document doc, string textToFind)
{
    var foundText = false;
    HeaderFooter headerFooter = sec.HeadersFooters.FirstOrDefault(hf =>
                !((HeaderFooter)hf).IsHeader
                && ((HeaderFooter)hf).HeaderFooterType == footerType) as HeaderFooter;

    if (headerFooter != null)
    {
        var found = headerFooter.Range.Replace(textToFind, textToFind);
        foundText = found > 0;
    }
    else
    {
        HeaderFooter footer = new HeaderFooter(doc, footerType);
        sec.AppendChild(footer);

        headerFooter = sec.HeadersFooters.FirstOrDefault(hf =>
                    !((HeaderFooter)hf).IsHeader
                    && ((HeaderFooter)hf).HeaderFooterType == footerType) as HeaderFooter;
    }

    return (foundText, headerFooter);
}

output.docx (543.4 KB)

Thanks @Eduardo_Canal for the solution.
But there is an issue with the solution whenever the footer is empty page number is printed twice.

@tanzeel123 sorry, Iā€™m not getting that issue, can you please attach your input file
input.docx (17.9 KB)
input1.docx (538.9 KB)
output.docx (13.1 KB)
output1.docx (543.4 KB)

No problem Eduardo_Canal i got a solution for it

1 Like