How can I get the meta-information of a bookmark?

@zwei The problem occurs because FIRMA_LOGO bookmark is in the document’s header. LayoutCollector and LayoutEnumerator classes does not work with content in the document’s headers/footers. They work only with content in the main document body. This is by design.

Thank you for the explain, but how can I rewrite the code, so that all Bookmarks (even if in header/footer) can be processed correctly.

If possible, we still need “isHeader()/isFooter()/isBody()” for all bookmarks.

@zwei You can use the following condition to skip bookmarks in header/footer:

if(bk.getBookmarkStart().getAncestor(NodeType.HEADER_FOOTER)!=null)
{
    // Bookmark start is in header/footer
}
1 Like

but how can I calculate its layout information? we still need all such layout information like pageIndex, x-/y- position, width/height for such bookmarks in footer/header

@zwei Unfortunately, there is no way to calculate layout information of nodes inside header/footer using Aspose.Words. As I have mentioned, LayoutCollector and LayoutEnumerator classes does not work with content in the document’s headers/footers.

so if a bookmark is in footer/header, then it is totally impossible to use Aspose to get its layout information, is it so?

If it is true, then I suppose that Aspose needs to add such a feature in next version.

@zwei

Yes, you absolutely right, there is no direct way to get layout information of nodes in header/footer using Aspose.Words.

No, there are no plans to add such feature in future versions of Aspose.Words.

As a temporary workaround, you can copy header/footer content into a separate document main body and get layout information of nodes. But moving content from header/footer to the document’s main body might affect its layout.

1 Like

we need the precise layout information, so I don’t think such a copy/moving can really solve our requirement.

@zwei

Unfortunately, this is the only possible workaround I can suggest to get layout information of nodes in the header/footer. There is no other way to achieve this and we do not have plans to add support of headers/footers in LayoutCollector and LayoutEnumenrator

1 Like

Why is there no plan to add support of headers/footers in Collector and Enumerator? Is there some kind of technical difficulty behind?

@zwei Headers/footers are repeated on each page of the section, so their layout is built separately from the main body, this makes it difficult to provide layout information for them. The same applied to the content inside shapes - shapes layout is build separately from the main body, so LayoutCollector and LayoutEnumenrator does not work with content inside shapes either.

1 Like

Thanks for your explanation. Our current task is: how to distinguish if a bookmark is for image or not. So far we use such a way: we check if it is set to a “ShapeRange” via MS-Office over JCOM.

So, I think that we may get the layout information of a bookmark, then we can decide if a bookmark is for image.

Is there a better way to get this task done within Aspose? Of course, our bookmark could be in body, could also be in header/footer.

@zwei You can check whether a bookmark is applied to a shape by checking whether there is a shape between bookmark start and end:

You can use code like this:

Document doc = new Document("C:\\Temp\\in.docx");
// Get bookmark
Bookmark bk = doc.getRange().getBookmarks().get("FIRMA_LOGO");
// Check whether bookmark wraps a shape.
boolean isShapeBookmark = (bk.getBookmarkStart().getNextSibling().getNodeType() == NodeType.SHAPE) &&
    (bk.getBookmarkStart().getNextSibling().getNextSibling() == bk.getBookmarkEnd());

Does this way also work for bookmarks in footer/header?

@zwei Yes, it will work for any bookmark in the document. Node structure is the same in the main body and in header/footer. Please see our documentation to learn more about Aspose.Words Document Object Model.

1 Like

Wow, that is exactly what I need! Thanks! I must read Aspose document more intensively!

1 Like