need advices and help about Bookmark API.

LORENZO Vincent vincent.lorenzo at
Mon Jan 14 14:49:47 UTC 2019

Hello everybody,
I thank you for your anwsers. I will continue with Bookmark which seem me quite easy to implement.

I found a way to avoid to search the text in the document:
             short value = ((Integer) text.length()).shortValue();

             cursor.goLeft(value, false);
             InsertBookmark(odtEditor.getXTextDocument(), cursor, "muBookmarkName");

It seems work find.


De : LibreOffice [mailto:libreoffice-bounces at] De la part de sos
Envoyé : vendredi 11 janvier 2019 10:36
À : libreoffice at
Objet : Re: need advices and help about Bookmark API.

We did some test to use RDF (also Miklos his advice) to store some information about images in a Writerdoc, before we stored the information in the ImageObject , what was not a good idea.

below the code who works well

const BASE_TYPE = "http://PMGopmaak.writer/Allgraphics"<http://PMGopmaak.writer/Allgraphics>
const TYPE_NAME = "GraphicURL"

const FILE_NAME = "PMGopmaak.writer/Allgraphics.rdf"

Sub StoreRDF

  ' "graph" staat hier niet voor een graphic maar voor een ellement die data kan omschrijven en stockeren
  ' een "litera" is een string die we stockeren en kunnen opvragen
  doc = ThisComponent
  repo = doc.getRDFRepository()
  type_uri = GetTypeURI()
  graph_name = doc.addMetadataFile(FILE_NAME, Array(type_uri))
  named_graph = repo.getGraph(graph_name)

  value_uri = CreateURI(

  if doc.supportsService("") then

   oDocGraphics = doc.GraphicObjects

    vAlleFotos = oDocGraphics.getElementNames()
            aantal = oDocGraphics.count
             For iG = 0 to aantal - 1
            iFotNr = ig + 1
             '   sFotMetNr = vAlleFotos(iG)
                oGraphic1 = oDocGraphics.GetbyIndex(ig)
  named_graph.addStatement(CreateURI(oGraphic1.Name &":", "URL"),value_uri,CreateLiteral(Ographic1.Title))
  named_graph.addStatement(CreateURI(oGraphic1.Name &":", "ONDERSCHRIFT"),value_uri,CreateLiteral(Ographic1.Hyperlinkname))

  next IG

  End If

End Sub

Sub ReadRDF()
zoek = "Image1:URL"
  doc = ThisComponent
  repo = doc.getRDFRepository()

  type_uri = GetTypeURI()

  graph_names = doc.getMetadataGraphsWithType(type_uri)

  If ubound(graph_names) = -1 then exit sub

  graph_name = graph_names(0)

  named_graph = repo.getGraph(graph_name)
  value_uri = CreateURI(

 ' named_graph.clear
  enume = named_graph.getStatements(Null, value_uri, Null)
 ' xray named_graph.getStatements(null, value_uri, Null)
  s = ""
   Do while Enume.hasMoreElements
    st = enume.nextElement()
     name = st.Subject.StringValue
       sts = st.Object.StringValue
    s = s & name & ": " & sts & chr(10)
    print name
   ' if name = zoek  then
   ' print sts
  ' named_graph.removeStatements(st.Subject, value_uri,st.Object)
   ' exit do
  ' endif

  msgbox s
End Sub

Sub RemoveRDF
  doc = ThisComponent
  repo = doc.getRDFRepository()

  type_uri = GetTypeURI()
  graph_names = doc.getMetadataGraphsWithType(type_uri)
  if ubound(graph_names) = -1 then exit sub
  graph_name = graph_names(0)
End Sub

Function GetTypeURI() As variant
  GetTypeURI = CreateURI(BASE_TYPE & "/" & TYPE_NAME)
End Function

Function CreateURI(n, Optional l) As Variant
  If IsMissing(l) Then
    a = Array(n)
    a = Array(n, l)
  End If
  uri = CreateUnoServiceWithArguments("", a)
  CreateURI = uri
End Function

Function CreateLiteral(v) As Variant
  a = Array(v)
  literal = CreateUnoServiceWithArguments("", a)
  CreateLiteral = literal
End Function

On 1/11/2019 9:27 AM, Miklos Vajna wrote:

Hi Vincent,

On Thu, Jan 10, 2019 at 02:13:58PM +0000, LORENZO Vincent <vincent.lorenzo at><mailto:vincent.lorenzo at> wrote:

Question 1:

Currently we are studying the possibility to use the Bookmark field's name to store these datas. We want to use the Bookmark with start and end tag, to get something like that.

<text:bookmark-start text:name="UML_element_qualified_name_and_others_useful_informations" />

              Some text

       <text:bookmark-end text:name=" UML_element_qualified_name_and_others_useful_informations " />

In reality, I would prefer to be able to use a more structured datas, extending the Bookmark object (or using another solution ? ) to get that, something like that:

       <text:bookmark-start text:name="Bookmark1" umlElementQualifiedName="Package1::Class1" umlElementKind="Class" umlElementfeature="name" id="xxx"/>

                           Some text

       <text:bookmark-end text:name="Bookmark1" />

but I have no idea if it is possible and how to do that. Please, does someone have an idea to structure my datas ?

Of course, if you have a better solution than Bookmark, fell free to propose it.

Bookmark is just a string; one hack you can do is to give it a unique

name, which refers to a per-document custom metadata, where the value

disturbs the user less. But that's still just a string, so you need to

serialize/load to/from string if you want to store key-value pairs.

An alternative is to use RDF annotations, which allows more complex


(Though you need to use or something like that to access it,

seems the Apache site is currently not available.)

One shortcoming of RDF is that it's ODF-only; while the per-doc custom

metadata + bookmarks works with most other formats as well.

So, in this example, I write text, I look for it, then I create a XTextRange and at the end, I can create the Bookmark. I'm not very have happy with this solution. I would prefer a solution without the search step, but I don't know how to get a valid XTextRange (I admit I don't yet well understand  the behavior of the API).

Please, do you have some pointer or code example ?

Some kind of iteration or search is necessary: if you take the user's

current selection or you iterate to the nth paragraph or you search for

text is up to you. An XTextRange is just a selection (where start and

end may point to the same document model position).

I thank you to read me until here.

Hope this helps. :-)




LibreOffice mailing list

LibreOffice at<mailto:LibreOffice at>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <>

More information about the LibreOffice mailing list