LibreOffice API 文档显示服务com.sun.star.text.textfield.Annotation 提供了一个接口:XTextField。尽管“注释”没有记录为TextPortionType 的可能值,但属性“TextField”的值是注释服务。
要访问 Annotation 属性,请执行以下操作:
XPropertySet portionProps= UnoRuntime.queryInferface(XPropertySet.class, portion);
String portionType= (String)portionProps.getPropertyValue("TextPortionType");
if(portionType.equals("Annotation"))
{
// get com.sun.star.text.textfield.Annotation
Object textField= portionProps.getPropertyValue("TextField");
XPropertySet annotProps= UnoRuntime.queryInterface(XPropertySet.class, textField);
String author= (String)annotProps.getPropertyValue("Author");
// ...
}
服务注解的其他属性有:
- 作者
- 内容
- 姓名
- 英文缩写
- 日期时间值
- 日期
- 文本范围
- IsFieldUsed
- IsFieldDisplayed
- TextWrap
- 锚类型
- 锚类型
没有为 TextPortionType“AnnotationEnd”设置 PropertyValue“TextField”。也没有服务AnnotationEnd。
可以按如下方式创建注释:
public static void createAnnotation( XComponentContext xContext, XTextRange xTextRange )
{
// per-document stuff
XMultiComponentFactory xServiceManager= xContext.getServiceManager();
Object desktop= xServiceManager.createInstanceWithContext("com.sun.star.frame.Desktop", xContext);
XComponent xComponent= xDesktop.getCurrentComponent();
XTextDocument xTextDocument= UnoRunime.queryInterface(XTextDocument.class, xComponent);
XText xText= xTextDocument.getText();
XMultiServiceFactory xWriterFactory= UnoRuntime.queryInterface(XMultiServiceFactory.class, xComponent);
// per-annotation stuff
Object annotation= xWriterFactory.createInstance("com.sun.star.text.textfield.nnotation");
XPropertySet annotProps= UnoRuntime.queryInterface(XPropertySet.class, annotation);
annotProps.setValue("Content", "It's a me!")
annotProps.setValue("Author", "Mario");
XTextField annotTextField= UnoRuntime.queryInterface(XTextfield.class, annotation);
xText.insertTextContent( xTextRange, annotTextField, true ); // "true" spans the range
}
注释也可以删除。在启用更改跟踪的情况下尝试删除注释时要小心:getPropertyValue("TextPortionType") 可能会在 ApacheOO 4.1.1 上引发 RuntimeException。同样由于某种原因我还没有调试,删除对我的 LibreOffice 4.2.7.2 没有任何作用
public static void removeAnnotations( XText xText ) throws Exception
{
// follow https://wiki.openoffice.org/wiki/Documentation/DevGuide/Text/Iterating_over_Text
final XEnumerationAccess xParaAccess= UnoRuntime.queryInterface(XEnumerationAccess.class, xText);
final XEnumeration xParaEnum= xParaAccess.createEnumeration();
for( int par_i=0; xParaEnum.hasMoreElements(); par_i++ )
{
final Object para= xParaEnum.nextElement();
final XServiceInfo xParaInfo= UnoRuntime.queryInterface(XServiceInfo.class, para);
final XEnumerationAccess xParamPortionsAccess= UnoRuntime.queryInterface(XEnumerationAccess.class, para);
final XEnumeration xParamPortionsEnum= xParamPortionsAccess.createEnumeration();
for( int port_i=0; xParamPortionsEnum.hasMoreElements(); port_i++ )
{
final Object portion= xParamPortionsEnum.nextElement();
final XPropertySet portProps= UnoRuntime.queryInterface(XPropertySet.class, portion);
// will cause RuntimeException when called with Change Tracking enabled
final String xTextPortionType= (String)portProps.getPropertyValue("TextPortionType");
if( xTextPortionType.equals("Annotation") )
{
final Object annotation= portProps.getPropertyValue("TextField");
final XPropertySet annotProps= UnoRuntime.queryInterface(XPropertySet.class, annotation);
final String annotAuthor= (String)annotProps.getPropertyValue("Author");
if( annotAuthor.equals("Mario")
{
final XTextField xTextField= UnoRuntime.queryInterface(XTextField.class, annotation);
xText.removeTextContent( xTextField );
}
}
// AnnotationEnd has no associated TextField, can't call removeTextContent on anything
}
}
}