0

public static void AddNumeration(ref WordprocessingDocument finalDoc) {

        IEnumerable<FooterPart> foo = finalDoc.MainDocumentPart.FooterParts;

        int count = 1;
        foreach (FooterPart fp in foo){

        Footer f = new Footer();

        ParagraphProperties paragraphProperties1 = new ParagraphProperties(new SectionProperties(new PageNumberType { Start = 1 }));
        paragraphProperties1.NumberingProperties = NumberingProperties
        paragraphProperties1.Justification = new Justification() { Val = JustificationValues.Right };
        Run run1 = new Run();
        Text text1 = new Text();

        text1.Text = count.ToString();
        count++;
        run1.Append(text1);
        Paragraph paragraph1 = new Paragraph() { RsidParagraphAddition = "00164C17", RsidRunAdditionDefault = "00164C17" };

        paragraph1.Append(paragraphProperties1);
        paragraph1.Append(run1);

        f.Append(paragraph1);

        fp.Footer = f;
     }


    }

This code always display the number 1 on the footer section of each page.

2 Answers2

0

You don't need to increment page number manually. Try something like this.

FooterPart footerPart = mainDocumentPart.AddNewPart<FooterPart>();
string footerPartId = mainDocumentPart.GetIdOfPart(footerPart);

Footer footer = new Footer(new Paragraph(
                new ParagraphProperties(
                    new ParagraphStyleId() { Val = "Footer" },
                    new Run(
                        new SimpleField(){ Instruction = "PAGE"}))));
footerPart.Footer = footer;
IEnumerable<SectionProperties> sectionProperties =
                        mainDocumentPart.Document.Body.Elements<SectionProperties>();

foreach (var sectionProperty in sectionProperties)
{
     sectionProperty.RemoveAllChildren<FooterReference>();
     sectionProperty.PrependChild<FooterReference>(new FooterReference()
     {
         Id = footerPartId
     });
}

This will add footer and increment it dynamically page by page.

-2

A little search on google : https://janewdaisy.wordpress.com/2012/03/01/insert-footer-in-word-document-cvb-net/

it should do the work

Maxime Porté
  • 1,034
  • 8
  • 13
  • 1
    That is... if you want to purchase the Spire.Doc for .NET package then the 'little search on Google' might be helpful – paul Mar 10 '16 at 12:44