@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)