Issue with Bullet Numbering While Splitting Document Pages

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)

@AlpeshChaudhariDev

It seems that the issue you’re experiencing with bullet numbering resetting when splitting document pages is related to how the sections and lists are being handled during the extraction process. When you split the document into pages, the list formatting may not be preserved correctly, leading to repeated bullet points and missing items.

To address this, you can consider the following adjustments to your logic:

  1. Ensure List Formatting is Preserved: When you extract pages, make sure that the list formatting is retained. This can be done by ensuring that the list items are correctly linked to their parent list and that the numbering is updated appropriately.

  2. Use a Document Page Splitter: Instead of manually extracting pages, you might want to utilize a dedicated page splitter class that handles the complexities of page extraction while preserving formatting. This can help maintain the integrity of lists and other formatting elements.

  3. Update List Numbering: After extracting each page, you may need to explicitly update the list numbering to ensure that it reflects the correct sequence. This can be done by iterating through the paragraphs and checking if they are list items, then updating their numbering accordingly.

Here’s a simplified example of how you might implement a page splitter that preserves list formatting:

public void SplitDocumentToPages(string docName)
{
    Document doc = new Document(docName);
    DocumentPageSplitter splitter = new DocumentPageSplitter(doc);

    for (int page = 1; page <= doc.PageCount; page++)
    {
        Document pageDoc = splitter.GetDocumentOfPage(page);
        UpdateListNumbering(pageDoc);
        pageDoc.Save($"OutputPage{page}.docx");
    }
}

private void UpdateListNumbering(Document pageDoc)
{
    foreach (Paragraph paragraph in pageDoc.GetChildNodes(NodeType.Paragraph, true))
    {
        if (paragraph.IsListItem)
        {
            paragraph.ListFormat.ListLevel.NumberStyle = NumberStyle.Arabic; // or any other style
            paragraph.ListFormat.ListLevel.StartingNumber = 1; // Reset starting number if needed
        }
    }
}

This example demonstrates how to extract pages while ensuring that list formatting is preserved and updated correctly. You may need to adapt the logic based on your specific requirements and document structure.

If you continue to face issues, consider reviewing the official documentation or examples related to document splitting and list formatting in Aspose.Words, as they may provide additional insights or methods to handle your specific case effectively.

Feel free to reach out if you have further questions or need additional assistance!

@AlpeshChaudhariDev Please try using ImportFormatOptions.KeepSourceNumbering:

tempDoc.AppendDocument(page, ImportFormatMode.KeepSourceFormatting, new ImportFormatOptions() { KeepSourceNumbering = true });