Association between ToC index object and its paragraph content

Jens Tröger jens.troeger at
Mon Dec 11 22:32:49 UTC 2017

Hi Mikos,

Thank you for your emails and suggestions thus far.  I've played around
with this some more and one question remains.

If I get the actual index object:

  indeces = document.getDocumentIndexes()
  toc = indeces.getByName('Table of Content1') # Assume it exists!

and then I create a text range of the paragraphs that make up the ToC:

  index_pars = toc.Anchor 
  index_cursor = document.Text.createTextCursorByRange(index_pars)                            
  index_parenum = index_cursor.createEnumeration()                                            
  while index_parenum.hasMoreElements():                                                      
      index_par = index_parenum.nextElement()                                                 
      # index_par is a paragraph (XTextContext) that's a single in in the ToC 

How can I correlate these index_par objects with the paragraphs that I
visit using the document's paragraph enumeration:

  parenum = document.Text.createEnumeration()                                                     
  while parenum.hasMoreElements():                                                                
      par = parenum.nextElement()                                                                  
      # par is a paragraph (XTextContext) in the document

At some point a given `par` and `index_par` represent the same
paragraph in the document, but how do I know?  Or does such an
association not or very loosely exist?


On Thu, Dec 07, 2017 at 09:58:24AM +0100, Miklos Vajna wrote:
> Hi Jens,
> On Thu, Dec 07, 2017 at 06:04:08AM +0100, Jens Tröger <jens.troeger at> wrote:
> > While walking all paragraphs of a Writer document and visiting their text
> > content, I come across text portions (the Heading N) which have Bookmarks
> > whose name is something like “_Toc263250771”. However, when I walk the 
> > paragraphs and text portions that actually make up the ToC of the document
> > itself, I see no indication or reference to these bookmarks.
> Let's say you have a ToC, then a Heading 1 paragraphs, Foo.
> Then the ToC refers to hidden RefHeading bookmark for Foo. You can see
> this reference at an UNO API level. Basic code for this:
> oParas = ThisComponent.Text.createEnumeration
> oPara = oParas.nextElement ' Table of Contents
> oPara = oParas.nextElement ' Foo
> oPortions = oPara.createEnumeration
> oPortion = oPortions.nextElement
> xray oPortion.HyperlinkURL ' gives #__RefHeading___Toc...
> If you want to see the bookmarks the ToC refers to, it's similar:
> ...
> oPara = oParas.nextElement ' Foo
> oPortions = oPara.createEnumeration
> oPortion = oPortions.nextElement
> xray oPortion.TextPortionType ' gives Bookmark
> xray oPortion.Bookmark.Name ' gives #__RefHeading___Toc...
> In most cases if you see some information serialized into ODT, then you
> can assume the same information is available via the UNO API. You can
> always read (most of) the ODT filter in xmloff/ to see what exact UNO
> API is used to write a given ODT markup.
> Regards,
> Miklos

Jens Tröger

More information about the LibreOffice mailing list