Hi Team,
I am splitting a document page-wise using the code below. However, when I do this, the bullet numbering is getting reset and not maintaining the correct sequence. For example, the item labeled “c)” is repeated multiple times, and “d)” is missing.
Could you please help me identify what needs to be changed in my logic to fix this issue? I am using the latest version of Aspose (v25.3).
Thank you!
Snippet :
Document doc = new Document(@"D:\Temp\BulletNumberIssue1\Sample.docx");
CreateSectionBreak(ref doc);
doc.Save(@"D:\Temp\BulletNumberIssue1\SampleOutput.docx");
private void CreateSectionBreak(ref Document doc)
{
try
{
Aspose.Words.Document tempDoc = (Aspose.Words.Document)doc.Clone(false);
int pageNumber = 1;
for (int i = 0; i < doc.PageCount; i++)
{
//Console.WriteLine("page number :" + i + " started");
Aspose.Words.Document page = doc.ExtractPages(i, 1);
// Remove section breaks in the page.
while (page.Sections.Count > 1)
{
page.FirstSection.AppendContent(page.Sections[1]);
page.Sections[1].Remove();
}
// Reset section start of the section.
//page.FirstSection.PageSetup.SectionStart = SectionStart.NewPage;
// unlink page field
FixPageNumber(page, ref pageNumber);
SetHeaderFooterIntoBody(page);
// Remove headers/footers since we already moved their content to main body.
page.GetChildNodes(NodeType.HeaderFooter, true).Clear();
//RemoveSectionBreak(ref page);
tempDoc.AppendDocument(page, ImportFormatMode.KeepSourceFormatting);
//destDoc.LastSection.PageSetup.RestartPageNumbering = true;
//Console.WriteLine("page number :" + i + " completed");
page = null;
}
doc = tempDoc;
tempDoc = null;
}
catch (Exception ex)
{
}
}
private void FixPageNumber(Document page, ref int pageNumber)
{
try
{
//// unlink page field
//page.Range.Fields.Where(f => f.Type == FieldType.FieldPage).ToList()
// .ForEach(f => { f.Update(); f.Unlink(); });
DocumentBuilder builder = new DocumentBuilder(page);
var pageFields = page.Range.Fields.Where(f => f.Type == FieldType.FieldPage).ToList();
if (pageFields.Count > 0)
{
for (int i = 0; i < pageFields.Count; i++)
{
builder.MoveToField(pageFields[i], true);
builder.Write(pageNumber.ToString());
pageFields[i].Remove();
}
pageNumber++;
}
}
catch (Exception ex)
{
}
}
private void SetHeaderFooterIntoBody(Document pageDoc)
{
// Check whether header/footer displaid
// This might be either primary or field page header/footer.
HeaderFooter displayedHeader = pageDoc.FirstSection.PageSetup.DifferentFirstPageHeaderFooter ?
pageDoc.FirstSection.HeadersFooters[HeaderFooterType.HeaderFirst] :
pageDoc.FirstSection.HeadersFooters[HeaderFooterType.HeaderPrimary];
HeaderFooter displayedFooter = pageDoc.LastSection.PageSetup.DifferentFirstPageHeaderFooter ?
pageDoc.LastSection.HeadersFooters[HeaderFooterType.FooterFirst] :
pageDoc.LastSection.HeadersFooters[HeaderFooterType.FooterPrimary];
// Move content into the main body.
if (displayedHeader != null)
{
while (displayedHeader.HasChildNodes)
pageDoc.FirstSection.Body.PrependChild(displayedHeader.LastChild);
}
if (displayedFooter != null)
{
//Paragraph paragraph = new Paragraph(pageDoc);
//Run run = new Run(pageDoc, ControlChar.LineBreak);
//paragraph.AppendChild(run);
//pageDoc.LastSection.Body.AppendChild(paragraph);
while (displayedFooter.HasChildNodes)
pageDoc.LastSection.Body.AppendChild(displayedFooter.FirstChild);
}
}
Source Document :
Sample.docx (113.4 KB)
Output :
SampleOutput.docx (104.8 KB)