2012-07-03 28 views
0

我有各種各樣的文本文檔(.odt,.doc)等,我使用這些文檔作爲模板使用Java爲文本填充它們。爲了實現這一點,我已經添加TextFields到每個我想要插入文本的位置上的文檔,並且我列舉了TextFields集併爲它們賦值。然而,我真正想做的,因爲這些文件只用於打印,是能夠使用書籤,而不是TextFields(當沒有填充時仍然有emtpy,看起來很有趣)。然而不管我有多少書籤插入用手上的文檔模板,每當我試圖檢索文檔的XBookmarksSupplier我得到即無法從文本文檔中檢索XBookmarksSupplier

XBookmarksSupplier bookmarksSupplier =(XBookmarksSupplier) UnoRuntime.queryInterface(XBookmarksSupplier.class,文檔空值);

爲空。參數document是我得到的,我在下面的方式模板使用文檔的內存拷貝創建的XComponent:

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); 

回答

1

好了,我終於想通了。通過使用Eclipse自動建議,我誤導了com.sun.star.sdb.XBookmarksSupplier而不是com.sun.star.text.XBookmarksSupplier這是正確的類。由於各種複製粘貼我所有使用相同的錯誤類的測試。