Question

I have various text documents (.odt, .doc) etc. which i use as templates for populating them with text using Java. In order to achieve that i have added TextFields to the documents on every position that i would like to insert text and i enumerate the TextFields set and assign values to them. However what i really want to do, since these documents are only for printing, is to be able to use bookmarks instead of TextFields (which when not populated are still there emtpy and looking funny). However no matter how many bookmarks i insert by hand on the document templates whenever i try to retrieve the document's XBookmarksSupplier i get a null value i.e.

XBookmarksSupplier bookmarksSupplier = (XBookmarksSupplier) UnoRuntime.queryInterface(XBookmarksSupplier.class, document);

is null. The parameter document is the XComponent that i get by creating an in memory copy of the document that i use as a template in the following way:

XComponentLoader loader = (XComponentLoader) UnoRuntime.queryInterface(XComponentLoader.class, desktop); 

List<PropertyValue> props = new ArrayList<PropertyValue>(); 
PropertyValue p = null; 

p = new PropertyValue(); 
p.Name = "AsTemplate"; 
p.Value = new Boolean (true); 
props.add(p); 

p = new PropertyValue(); 
p.Name = "DocumentTitle"; 
p.Value = "New doc"; 
props.add(p); 

p = new PropertyValue(); 
p.Name = "Hidden"; 
p.Value = new Boolean(true); 
props.add(p);

PropertyValue[] properties = new PropertyValue[props.size()]; 
props.toArray(properties); 

XComponent document = null;

String templateFileURL = filePathToURL(templateFile); 
document = loader.loadComponentFromURL(templateFileURL, "_blank", 0, properties);
Was it helpful?

Solution

Well, finally i figured it out. By Using Eclipse auto-suggest i had imported mistakingly com.sun.star.sdb.XBookmarksSupplier instead of com.sun.star.text.XBookmarksSupplier which is the correct Class. Due to various copy-pasting all my tests where using the same wrong Class.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top