Spire.Doc is a professional Word .NET library specifically designed for developers to create, read, write, convert and print Word document files. Get free and professional technical support for Spire.Doc for .NET, Java, Android, C++, Python.

Wed Jun 24, 2015 11:34 am

I have a question
Is there a section inside the Word document, how to set the page number?
My Word document has two sections, a total of 10 pages, I want in the first section and the second page number begins with 1;
The first section page Numbers from 1 to 7 pages, the second section page Numbers from 1 to 3.

1665593517@qq.com
 
Posts: 5
Joined: Fri Mar 20, 2015 5:43 am

Thu Jun 25, 2015 3:40 am

Hello,

Thanks for your inquiry. Please refer to the below code:
Code: Select all
Document document = new Document("sample.docx");
                     
            //add page number on the first section
            HeaderFooter footer = document.Sections[0].HeadersFooters.Footer;
            Paragraph footerParagraph = footer.AddParagraph();
            footerParagraph.AppendField("page number", FieldType.FieldPage);
            footerParagraph.AppendText(" of ");               
            footerParagraph.AppendField("number of pages", FieldType.FieldSectionPages);
            footerParagraph.Format.HorizontalAlignment = HorizontalAlignment.Right;
           

            //restart page number of section and set the starting page number to 1
            document.Sections[1].PageSetup.RestartPageNumbering = true;
            document.Sections[1].PageSetup.PageStartingNumber = 1;

            //add page number on the second section
            HeaderFooter footer2 = document.Sections[1].HeadersFooters.Footer;
            Paragraph footerParagraph2 = footer2.AddParagraph();
            footerParagraph2.AppendField("page number", FieldType.FieldPage);
            footerParagraph2.AppendText(" of ");
            footerParagraph2.AppendField("number of pages", FieldType.FieldSectionPages);
            footerParagraph2.Format.HorizontalAlignment = HorizontalAlignment.Right;

            document.SaveToFile("result.docx", FileFormat.Docx);

Please feel free to contact us if you have any questions or needs.

Best Regards,
Betsy
E-iceblue support team
User avatar

Betsy
 
Posts: 802
Joined: Mon Jan 19, 2015 6:14 am

Thu Jun 25, 2015 5:07 am

Thank you very much, my problem is resolved.

1665593517@qq.com
 
Posts: 5
Joined: Fri Mar 20, 2015 5:43 am

Thu Jun 25, 2015 5:54 am

Hello,

Thanks for your feedback. If there are any questions, welcome to get it back to us.

Sincerely,
Betsy
E-iceblue support team
User avatar

Betsy
 
Posts: 802
Joined: Mon Jan 19, 2015 6:14 am

Fri Feb 03, 2023 7:18 am

Hi, I have the same problem in Java.
My code is:
HeaderFooter footer = document.getSections().get(0).getHeadersFooters().getFooter(); //section.getHeadersFooters().getFooter();
Paragraph footerParagraph = footer.addParagraph();
footerParagraph.appendField("page number", FieldType.Field_Page);
footerParagraph.appendText(" of ");
footerParagraph.appendField("number of pages", FieldType.Field_Section_Pages);
footerParagraph.getFormat().setHorizontalAlignment(HorizontalAlignment.Right);

and the result is:

1 of number of pages

instead of the actual number, the text is displayed number of pages

Where am I wrong?
Thank you

bogdan.sb
 
Posts: 1
Joined: Wed Nov 09, 2022 12:38 pm

Fri Feb 03, 2023 8:11 am

Hello,

Thanks for your inquiry.
I simluated a word document and tesed the code you provided with the latest version of Spire.Doc11.1.1, but I didn’t reproduce your issue, as shown in the screenshot below. If you don’t use the latest version of Spire.Doc, please update to it and have a test. If the issue still exists, please offer the following message. Thanks for your assistance in advance.

1) Your input word document, you can attach here or send it to us via email (support@e-iceblue.com).
2) Your test environment, such as OS info (E.g. Windows 7, 64-bit) and region setting (E.g. China, Chinese).

Sincerely
Abel
E-iceblue support team
User avatar

Abel.He
 
Posts: 860
Joined: Tue Mar 08, 2022 2:02 am

Return to Spire.Doc