hi ,
i have a .docx file which has a footer contains page information.
i want to add a new section ,after the new section .all page information must be starting with 1.
i used the below codes,but it does not work:
private void AddNewSection(string fileFullname)
{
var doc = new ASP.Words.Document(fileFullname);
var builder = new ASP.Words.DocumentBuilder(doc);
builder.MoveToDocumentEnd();
builder.InsertBreak(ASP.Words.BreakType.SectionBreakNewPage);
var currentSection = builder.CurrentSection;
var pageSetup = currentSection.PageSetup;
pageSetup.DifferentFirstPageHeaderFooter = false;
currentSection.HeadersFooters.LinkToPrevious(false);
currentSection.HeadersFooters.Clear();
builder.MoveToHeaderFooter(ASP.Words.HeaderFooterType.HeaderPrimary);
builder.ParagraphFormat.Alignment = ASP.Words.ParagraphAlignment.Center;
builder.Font.Name = "Arial";
builder.Font.Size = 12;
builder.Write("");
doc.Save(fileFullname);
}
my aspose.word version is :21.8.0
thank you.
@vs6060_qq_com
To set the footer content for a new section in a .docx file and ensure that the page numbering starts from 1, you can follow these steps:
-
Insert a New Section: Use the InsertBreak
method to add a new section break at the desired location in your document.
-
Clear Existing Headers and Footers: After creating the new section, you need to unlink it from the previous section’s headers and footers. This can be done by setting LinkToPrevious(false)
and clearing the existing headers and footers.
-
Set Up the Footer: Move to the footer of the new section and insert the desired content, including page numbering.
-
Reset Page Numbering: To start the page numbering from 1 in the new section, you can set the PageSetup.RestartNumbering
property to true
.
Here is an example code snippet that demonstrates these steps:
private void AddNewSectionWithFooter(string fileFullname)
{
var doc = new Aspose.Words.Document(fileFullname);
var builder = new Aspose.Words.DocumentBuilder(doc);
// Move to the end of the document and insert a new section
builder.MoveToDocumentEnd();
builder.InsertBreak(Aspose.Words.BreakType.SectionBreakNewPage);
// Get the current section and its page setup
var currentSection = builder.CurrentSection;
currentSection.HeadersFooters.LinkToPrevious(false);
currentSection.HeadersFooters.Clear();
// Move to the footer and insert content
builder.MoveToHeaderFooter(Aspose.Words.HeaderFooterType.FooterPrimary);
builder.ParagraphFormat.Alignment = Aspose.Words.ParagraphAlignment.Center;
builder.Font.Name = "Arial";
builder.Font.Size = 12;
builder.Write("Page ");
builder.InsertField("PAGE", "");
// Set up page numbering to restart from 1
currentSection.PageSetup.RestartNumbering = true;
currentSection.PageSetup.PageStartingNumber = 1;
// Save the document
doc.Save(fileFullname);
}
In this code:
- A new section is added at the end of the document.
- The footer is cleared and customized to include page numbering.
- The page numbering is set to restart from 1 for the new section.
Make sure to adjust the file path and any specific footer content as needed. This should help you achieve the desired footer setup in your document.
@vs6060_qq_com Could you please attach your input, output and expected output documents here for our reference. We will check the issue and provide you more information.
hi alexey
i have a a.docx file.
a.docx (20.5 KB)
there are 3 pages on the document. and the footer is PageNo:1 PageCount:3,PageNo:2 PageCount:3…
now i want to add a document (b.docx)
b.docx (20.6 KB)
to a.docx file.
i want a.docx footer of page1 page2 and page3 keeps no any changes. (i.e PageNO:1 PageCount:3,PageNo:2 PageCount 3), and the added 3 pages start with new pages.
hope you understand what i need.
thank you very much.
@vs6060_qq_com You can use PageSetup.RestartPageNumbering
to restart page numbering in the appended document:
Document docA = new Document(@"C:\Temp\a.docx");
Document docB = new Document(@"C:\Temp\a.docx");
// Restart page numbering in the second document.
docB.FirstSection.PageSetup.RestartPageNumbering = true;
// There are NUMPAGES fields in both document. There is no way to restart them,
// But if the final document is no supposed to be changed these fields can be unlinked.
docA.UpdateFields();
docB.UpdateFields();
docA.Range.Fields.Where(f => f.Type == FieldType.FieldNumPages).ToList().ForEach(f => f.Unlink());
docB.Range.Fields.Where(f => f.Type == FieldType.FieldNumPages).ToList().ForEach(f => f.Unlink());
// Concatenate documents.
docA.AppendDocument(docB, ImportFormatMode.KeepSourceFormatting);
docA.Save(@"C:\Temp\out.docx");
out.docx (18.0 KB)
Though there are NUMPAGES
fields that cannot be restarted. NUMPAGES
returns number of pages in the whole document. So in the above code these fields are updated and unlinked (replaced with plain text).
Alternatively, if your sub-documents consist of single section, you can use SECTIONPAGES
field to return pages per section.
hi elexey,
this issue got resloved.
thank you very much.
btw, the below reminder is very useful :
Alternatively, if your sub-documents consist of single section, you can use SECTIONPAGES
field to return pages per section
1 Like