Can LayoutCollector use the same document during the use of this document, and why does the ‘This instance is not attached to a document.’ exception sometimes appear
this.document = document;
this.layoutCollector = new LayoutCollector(document);
Can LayoutCollector use the same document during the use of this document, and why does the ‘This instance is not attached to a document.’ exception sometimes appear
this.document = document;
this.layoutCollector = new LayoutCollector(document);
The exception “This instance is not attached to a document” typically occurs when the LayoutCollector
instance is created with a Document
object that is not properly initialized or has been modified after the LayoutCollector
was created.
When you create a LayoutCollector
instance, it builds the document layout and caches it. If you make any modifications to the document after creating the LayoutCollector
, those changes will not be reflected in the cached layout. This can lead to inconsistencies and exceptions when you try to access layout-related information.
To avoid this issue, you should ensure that the document is fully initialized and that you call the UpdatePageLayout
method on the Document
before performing any layout-related operations. This method updates the layout cache to reflect any changes made to the document.
Here’s a code example demonstrating how to properly use LayoutCollector
:
Document doc = new Document();
LayoutCollector layoutCollector = new LayoutCollector(doc);
// Populate the document with content
DocumentBuilder builder = new DocumentBuilder(doc);
builder.write("Section 1");
builder.insertBreak(BreakType.PAGE_BREAK);
builder.write("Section 2");
// Update the layout before using the LayoutCollector
doc.updatePageLayout();
// Now you can safely use the LayoutCollector
int numPages = layoutCollector.getNumPagesSpanned(doc);
System.out.println("Number of pages spanned: " + numPages);
In this example, the updatePageLayout
method is called after modifying the document, ensuring that the LayoutCollector
has the correct layout information to work with. This should help prevent the exception you are encountering.
LayoutCollector| Aspose. Java 语言词汇 — LayoutCollector | Aspose.Words for Java
If you need to access page indexes of the document nodes you need to set this property to point to a document instance, before page layout of the document is built. It is best to set this property to null afterwards, otherwise the collector continues to accumulate information from subsequent rebuilds of the document’s page layout.
But doesn’t it say that it will be updated, the function I need to do is to find the corresponding node every time, output the corresponding page number and line number, sometimes there will be an error, if I ‘new LayoutCollector(document)’ every time the execution efficiency will be reduced
@Crane If you do not update document and use LayoutCollector
to simply return nodes layout information, then it is not required to create LayoutCollector
for each such operation. Simply create LayoutCollector
once and use it to get layout information of all nodes in the Document
instance.
Could you please provide your input document and code that will allow us to reproduce the problem?