Error on extracting pages (This instance is not attached to a document)

Hi,

Aspose words version: 22.5.0

We are using special bookmark names to determine each content of a document.

the following code is being called after taking the proper start & end page # of each bookmark.

The idea is the first bookmark is on the 1st page until the 7th page.
While the second bookmark is from the 8th page until the 14th page.

In the given sample document there are 14pages.

private Document extractDocumentPages(Document sourceDoc, int startPage, int count) throws Exception {     
Document splitDocument = new Document();     
splitDocument.removeAllChildren();      
// Extract pages     
Document extractedPages = sourceDoc.extractPages(startPage, count);     
 // Copy sections from extracted pages to split document     
for (Section section : extractedPages.getSections()) {        
 Section newSection = (Section) splitDocument.importNode(section, true);         
splitDocument.appendChild(newSection);         
 // Keep the section start type from the original document         
newSection.getPageSetup().setSectionStart(section.getPageSetup().getSectionStart());    
 }    
    // Ensure the first section starts on a new page if it's not the very first page    
   if (startPage > 1 && splitDocument.getFirstSection() != null) {         
  splitDocument.getFirstSection().getPageSetup().setSectionStart(SectionStart.NEW_PAGE);     
  }      
  return splitDocument;
 }

The first bookmark which calls extractDocumentPages from page 1 to 7 is working correctly.
which is consumed by extractDocumentPages(0, 7);

However when I extract from page 7 to 14
which is consumed by extractDocumentPages(7, 7);

it throws an error This instance is not attached to a document.

The specific error is encountered on sourceDoc.extractPages(startPage, count)

java.lang.IllegalStateException: This instance is not attached to a document.
	at com.aspose.words.LayoutCollector.zzZ67(Unknown Source) ~[aspose.jar:22.5.0]
	at com.aspose.words.LayoutCollector.getStartPageIndex(Unknown Source) ~[aspose.jar:22.5.0]
	at com.aspose.words.zzZ3T.zzYxS(Unknown Source) ~[aspose.jar:22.5.0]
	at com.aspose.words.zzZ3T.extractPages(Unknown Source) ~[aspose.jar:22.5.0]
	at com.aspose.words.Document.extractPages(Unknown Source) ~[aspose.jar:22.5.0]

Any help is appreciated

sample_failure_doc.docx (59.2 KB)

@nre Unfortunately, I cannot reproduce the problem on my side using the latest 24.9 version of Aspose.Words. I tested with the following simple code:

Document doc = new Document("C:\\Temp\\in.docx");
Document doc1 = extractDocumentPages(doc, 0, 7);
Document doc2 = extractDocumentPages(doc, 7, 7);

Also, it is not quite clear why you copy content from extracted document into another document created from scratch. Could you please elaborate?

Hi @alexander.noskov

Our exact use-case is to merge multiple documents into a single merged document file that’s why each document is marked by a specific bookmark from where it should start the page.

Originally the merged document is from 2 document files.

The merged document is manually being opened and edited by a user.

At any point in time that edits are completed, it should be split using the bookmarks by extracting the specific pages, so that they will be taken and saved separately.

I hope this answers your question.

Is the issue not reproducible even with the example file from the original post?

@nre No, unfortunately, your explanation does not answer my question. In your extractDocumentPages method you are creating a document from scratch:

Document splitDocument = new Document();     
splitDocument.removeAllChildren();  

Then you use extractPages method to extract pages from the source document:

Document extractedPages = sourceDoc.extractPages(startPage, count);     

And then you copy content from extractedPages document into the document created from scratch, i.e. splitDocument. This step is not clear and look like a redundant.

No, the problem is not reproducible with the document you have attached in the initial post.