【问题标题】:Access to com.sun.star.text.textfield.Annotation in {Libre,Open}Office Writer using Java使用 Java 访问 {Libre,Open}Office Writer 中的 com.sun.star.text.textfield.Annotation
【发布时间】:2014-11-17 16:30:57
【问题描述】:

我正在使用 Java 遍历 XText 对象中的所有段落和 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
}

遇到AnnotationAnnotationEnd,我想访问对应的Annotation(以后自己创建一些)。

我知道com.sun.star.text.textfield.Annotation 服务,但是Annotation 通过XServiceInfo 表示它不支持它。如何从我在 TextPortion 中遇到的注释中获取对 Annotation 的引用?

我如何自己创建注释?

我使用的是 OpenOffice 4.1.1。

【问题讨论】:

    标签: java annotations libreoffice openoffice-writer


    【解决方案1】:

    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
        }
      }
    }
    

    【讨论】:

      猜你喜欢
      • 2022-08-10
      • 2021-07-29
      • 1970-01-01
      • 2011-08-19
      • 2013-03-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多