我正在使用Java遍历XText对象中的所有段落和文本部分。
当我检查文本部分类型时:
XPropertySet props= UnoRuntime.queryInferface(XPropertySet.class, portion);
String portionType= (String)props.getPropertyValue("TextPortionType");
if(portionType.equals("Annotation"))
{
// get com.sun.star.text.textfield.Annotation
}
并遇到Annotation
或AnnotationEnd
,我想访问相应的注释(稍后自己创建一些)。
我知道服务com.sun.star.text.textfield.Annotation,但是Annotation
通过XServiceInfo
表示它不支持它。如何从文本部分中遇到的注释中获取对Annotation
的引用?
如何自己创建注释?
我正在使用OpenOffice 4.1.1。
LibreOffice API 文档显示,服务com.sun.star.text.textfield.Annotation
提供了一个单一的接口:XTextField
。虽然"注释"没有被记录为TextPortionType
的可能值,但属性"TextField"的值是注释服务。
要访问注释属性,请执行以下操作:
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");
// ...
}
服务注释的 Ohter 属性是:
- 作者
- 内容
- 名字
- 初始值
- 日期时间值
- 日期
- 文本范围
- IsFieldUse
- IsFieldDisplay(英语:IsFieldDisplay)
- 文本自动换行
- 锚点类型
- 锚点类型
属性值"文本字段"未设置为文本部分类型"注释结束"。也没有服务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 上引发运行时异常。同样由于某种原因我还没有调试,删除在我的 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
}
}
}