Add custom metadata using InContentMetadata

Java, C++, C#, Delphi... - Using the UNO bridges
Post Reply
ellisdp
Posts: 2
Joined: Tue Nov 19, 2024 4:50 pm

Add custom metadata using InContentMetadata

Post by ellisdp »

LibreOffice 24.2.4.2 on Windows, using the Java SDK.

I am trying to find a way to associate custom metadata with individual text paragraphs in a LibreOffice document, which I can construct or manipulate using the SDK. So when adding each paragraph I want the ability to add a custom ID, e.g. 101, 102 or whatever, which I can cross-reference with content in a database, and which is preserved if the document is edited and the paragraphs reordered.

I have found the class com.sun.star.text.InContentMetadata which sounds like it might do what I want, and is documented here:

https://api.libreoffice.org/docs/idl/re ... adata.html

However I cannot seem to find this in the SDK from my Java environment. My Java IDE is picking up "libreoffice.jar" from C:\Program Files\LibreOffice\program\classes, and if I open that Jar it does not contain this class (nor a number of other documented classes).

Does that class exist, if so do you know why I am not picking that up in the SDK - are there additional Jars I need to import to access the full set of classes? And if it does exist, are there any code examples or documentation of how to use this?

Thanks.
LibreOffice 24.2 on Windows
ms777
Volunteer
Posts: 193
Joined: Mon Oct 08, 2007 1:33 am

Re: Add Custom Metadata using InContentMetadata

Post by ms777 »

Hi, the easiest way to get support is to prepare a minimum sample in OO Basic and then ask for help with translating to Java
JeJe
Volunteer
Posts: 2917
Joined: Wed Mar 09, 2016 2:40 pm

Re: Add Custom Metadata using InContentMetadata

Post by JeJe »

What kind of LibreOffice document? Writer?

Each paragraph already has a metadata reference which uniquely identifies it though I don't know how persistent that reference is.

Edit:

to make it your own reference number you'd just need a lookup table somewhere with your number stored beside it.
Edits can easily remove a paragraph though.
Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
ellisdp
Posts: 2
Joined: Tue Nov 19, 2024 4:50 pm

Re: Add Custom Metadata using InContentMetadata

Post by ellisdp »

JeJe wrote: Wed Nov 20, 2024 3:13 pm What kind of LibreOffice document? Writer?

Each paragraph already has a metadata reference which uniquely identifies it though I don't know how persistent that reference is.
Thanks, yes, Writer.

If I open the saved document content in content.xml within the .odt, I don't see a unique reference, I see things like this:

Code: Select all

<text:p text:style-name="Text_20_body">It was the best of times, it was the worst of times ...</text:p>
If there is a metadata reference for that paragraph, where is it stored or how can I access it?
LibreOffice 24.2 on Windows
JeJe
Volunteer
Posts: 2917
Joined: Wed Mar 09, 2016 2:40 pm

Re: Add Custom Metadata using InContentMetadata

Post by JeJe »

You're right, its lost on saving so that's no use.

You could try ParaUserDefinedAttributes but my past experience suggests this may be prone to problems. Other methods would be bookmarks, or hidden text perhaps.


ParaUserDefinedAttributes, based on Zizi64's code from
viewtopic.php?t=111531

Code: Select all

sub TestParaUserDefinedAttributes
p= thiscomponent.text.createenumeration.nextelement
  Dim oUserData   'A copy of the UserDefinedAttributes
  Dim oMyAttribute As New com.sun.star.xml.AttributeData
  Dim oAttribute
  Dim oDoc
  
  'oDoc = createNewCalcDoc() REM-ed by Zizi64
  
  oDoc = Thiscomponent 'Inserted by Zizi64
  
  
 oMyAttribute.Type = "CDATA"
  oMyAttribute.Value = "Andrew Pitonyak"
 REM Here is where things usually go wrong with a statement like 
  REM oCell.UserDefinedAttributes.insertByName("Andy", oMyAttribute)
  REM This fails every time. Instead, first make a copy and then
  REM operate on the copy.
  oUserData = p.ParaUserDefinedAttributes
  If NOT oUserData.hasByName("Andy") Then
    oUserData.insertByName("Andy", oMyAttribute)
    p.ParaUserDefinedAttributes = oUserData
  End If
  oAttribute = p.ParaUserDefinedAttributes.getByName("Andy")
  'Xray oCell
  Print oAttribute.Value
End Sub

Saved in content.xml
<style:style style:name="P1" style:family="paragraph" style:parent-style-name="Heading_20_3">
<style:paragraph-properties Andy="Andrew Pitonyak"/>
</style:style>
Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
Post Reply