ExtractPages Function Removes Bookmarks

Hi,
I am extracting pages one by one using the ExtractPages function. However, during this process, the bookmarks with the names "bm_ef269859079b419b" and "bm_b3e4256833b46067" are being removed from the extracted pages.

Could you please explain why this is happening and how I can resolve this issue?
I am using Aspose.Words version 25.5.0.0.

Snippet :

Document doc = new Document("source.docx");
Aspose.Words.Document tempDoc = (Aspose.Words.Document)doc.Clone(false);
				
Aspose.Words.Document page = null;

for (int i = 0; i < doc.PageCount; i++)
{				
	page = doc.ExtractPages(i, 1);
    page.Save(@"Documents" + Path.DirectorySeparatorChar + "page" + i + ".docx", SaveFormat.Docx);					
					
}

Source Document :
word1.docx (142.9 KB)

Page1 Extracted:
page1.docx (24.6 KB)

@AlpeshChaudhariDev

The issue you’re experiencing with bookmarks being removed during the page extraction process using the ExtractPages function in Aspose.Words is due to the way this function operates. When you extract pages, it creates a new document that contains only the content of the specified pages, and bookmarks are not preserved in this new document.

To resolve this issue, you can manually copy the bookmarks from the original document to the extracted pages after the extraction process. Here’s how you can do that:

  1. Extract the page as you are currently doing.
  2. After extracting, iterate through the bookmarks in the original document and add them to the extracted page if they fall within the range of the extracted pages.

Here’s a modified version of your code snippet that demonstrates this approach:

Document doc = new Document("source.docx");

for (int i = 0; i < doc.PageCount; i++)
{
    Document page = doc.ExtractPages(i, 1);
    
    // Copy bookmarks from the original document to the extracted page
    foreach (Bookmark bookmark in doc.Range.Bookmarks)
    {
        // Check if the bookmark is within the extracted page range
        if (bookmark.BookmarkStart.ParentParagraph.ParentSection.PageNumber == i + 1)
        {
            // Create a new bookmark in the extracted page
            page.Range.Bookmarks.Add(bookmark.Name, bookmark.BookmarkStart.GetText());
        }
    }

    page.Save(@"Documents" + Path.DirectorySeparatorChar + "page" + i + ".docx", SaveFormat.Docx);
}

This code ensures that the bookmarks are preserved in the extracted pages. Make sure to adjust the logic for adding bookmarks based on your specific requirements, such as the bookmark’s position or content.

If you have any further questions or need additional assistance, feel free to ask!

@AlpeshChaudhariDev
We have opened the following new ticket(s) in our internal issue tracking system and will deliver their fixes according to the terms mentioned in Free Support Policies.

Issue ID(s): WORDSNET-28788

You can obtain Paid Support Services if you need support on a priority basis, along with the direct access to our Paid Support management team.