none
How to add different page numbers or number formats to different sections on word document using c# Microsoft.Office.Interop.Word dll? RRS feed

  • Question

  • 1. I want to add different page numbers or page number formats to different sections at the footer on word document using C# and Microsoft.Office.Interop.Word dll.

    2. Also, I want to start page number from 1 after new section start on a word document.

    Tuesday, October 16, 2018 12:40 PM

All replies

  • Hi sandesh,

    You could use the PageNumbers.Add(Object, Object) method to implement your requirements.

    Please refer to the following links:
    PageNumbers.Add(Object, Object) Method
    How to add page no. and print date ms interop word DLL

    Hopefully it helps you.

    Best Regards,
    Bruce

    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread.

    Wednesday, October 17, 2018 9:10 AM
    Moderator
  • Hello Bruce Dai,

    Thanks for the reply.

    The given solution is working fine for when we add page numbers for the whole word document with the same page number format.

    But in my case, I want to add page number as per the section-wise with the different page number format in the same word document.

    Consider, the word document contains 5 sections. I want to add page number for the first section like 1...2...3...

    and for the second section page number like i...ii....iii...

    Below is my the code.

    Range footerRanage = oDocument.Sections[i].Footers[WdHeaderFooterIndex.wdHeaderFooterPrimary].Range;
    oDocument.Sections[i].Footers[Microsoft.Office.Interop.Word.WdHeaderFooterIndex.wdHeaderFooterPrimary].PageNumbers.RestartNumberingAtSection = true;
    oDocument.Sections[i].Footers[Microsoft.Office.Interop.Word.WdHeaderFooterIndex.wdHeaderFooterPrimary].PageNumbers.StartingNumber = 1;
    oDocument.ActiveWindow.Selection.Font.Name = "Cambria";
    oDocument.ActiveWindow.Selection.Font.Size = 10;
    oDocument.ActiveWindow.Selection.TypeText(sFooter);
    oDocument.ActiveWindow.Selection.TypeText("\t");
    oDocument.ActiveWindow.Selection.TypeText(Convert.ToString(oSectionList[j].SectionNumber));
    oDocument.ActiveWindow.Selection.TypeText("-");
    object oFalse = (object)false;
    oDocument.Sections[i].Footers[WdHeaderFooterIndex.wdHeaderFooterPrimary].PageNumbers.Add(WdPageNumberAlignment.wdAlignPageNumberRight, ref oFalse);

    Thanks. 



    Wednesday, October 17, 2018 1:23 PM
  • Hi sandesh,

    You need to use c# to select different parts of the word document and then add page numbers or number formats for each part. 

    To get specific page from word document, Please refer to the following links:

    Select page in docx using C#

    get specific page from word in c#.net ?

    Best Regards,

    Bruce


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread.


    Tuesday, October 30, 2018 1:50 AM
    Moderator