我使用Java迭代XText对象中的所有Paragraphs和TextPortions。
当我检查TextPortionType:
时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
,我想访问相应的Annotation(后来我自己创建了一些)。
我知道服务com.sun.star.text.textfield.Annotation,但是Annotation
通过XServiceInfo
表明它不支持它。如何从TextPortion中遇到的注释中获取Annotation
的引用?
如何自行创建注释?
我正在使用OpenOffice 4.1.1。
答案 0 :(得分:0)
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");
// ...
}
服务注释的Ohter属性是:
PropertyValue" TextField"未设置为TextPortionType" AnnotationEnd"。还没有服务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
}
}
}