使用 Openoffice Java API (UNO API) 将整个 ODT (Openoffice Writer) 文档部分复制到其他文档

发布于 2024-09-14 18:26:17 字数 965 浏览 1 评论 0原文

我需要使用 OpenOffice Java API 复制文档部分并将其粘贴到另一个文档部分。到目前为止,我已成功复制源文档部分的文本并将其粘贴到目标文档的部分上(请参见下面的示例)。

然而,问题是非文本元素(图形、格式、表格等)不会粘贴到目标文档上。

我用来提取源部分文本的代码是:

// Read source file text
XComponent xComponentSource = this.ooHelper.loadDocument("file://" + fSource);
// Get sections
XTextSectionsSupplier textSectionsSupplierSource = (XTextSectionsSupplier)UnoRuntime.queryInterface(XTextSectionsSupplier.class, xComponentSource);
XNameAccess nameAccessSource = textSectionsSupplierOrigen.getTextSections();
// Get sections by name
XTextSection textSectionSource = (XTextSection)UnoRuntime.queryInterface(XTextSection.class, nameAccessOrigen.getByName("SeccEditable"));
//Get section text
String sectionSource = textSectionSource.getAnchor().getString();

将文本粘贴到目标部分上,选择该部分的代码是相同的,并且我设置了字符串:

textSectionDest.getAnchor().setString(sectionSource);

我已阅读 API Javadoc,并且我还没有没有找到任何方法来复制整个部分。有什么办法可以做到吗?

I need to use the OpenOffice Java API to copy a document section and paste it over another document section. So far I have managed to copy the text of the section of the source document and paste it over the section at the target document (see the example below).

However, the problem is that non-text elements (graphics, formats, tables, etc.) don't get pasted on the destination document.

The code I have used to extract the text of the source section is:

// Read source file text
XComponent xComponentSource = this.ooHelper.loadDocument("file://" + fSource);
// Get sections
XTextSectionsSupplier textSectionsSupplierSource = (XTextSectionsSupplier)UnoRuntime.queryInterface(XTextSectionsSupplier.class, xComponentSource);
XNameAccess nameAccessSource = textSectionsSupplierOrigen.getTextSections();
// Get sections by name
XTextSection textSectionSource = (XTextSection)UnoRuntime.queryInterface(XTextSection.class, nameAccessOrigen.getByName("SeccEditable"));
//Get section text
String sectionSource = textSectionSource.getAnchor().getString();

To paste the text over the target section, the code to select the section is the same, and I set the string:

textSectionDest.getAnchor().setString(sectionSource);

I have read the API Javadoc, and I haven't found any method to copy the entire section. Is there any way to do it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

风蛊 2024-09-21 18:26:17

我也遇到了同样的问题。我最终通过创建两个光标来解决问题,一个在我想要复制的内容的开头,然后另一个通过使用在内容的末尾,然后将第一个光标选择扩展到第二个。这在第一个光标上使用了 gotoRange 方法,传入第二个光标和 True 来告诉它扩大选择范围。

光标示例:
http://api.openoffice.org/docs/DevelopersGuide/Text/Text .xhtml#1_3_1_1_Editing_Text

然后我创建了一个包含所选内容的自动文本容器、组和元素。并使用自动图文集条目的 applyTo 方法在光标位置插入/粘贴内容。我使用了 guid 作为自动文本容器的名称,因此它是唯一的,然后在完成后删除了该容器。

自动图文集示例:
http://api.openoffice.org/docs/DevelopersGuide/Text/Text .xhtml#1_3_1_6_Auto_Text

如果您愿意,我可以发布我的代码,但它是用 Python 编写的。

I was having this same problem. I ended up solving by creating two cursors, one at the start of the content of what I wanted duplicated, then another at the end of the content by using, then extending the cursor selection of the first one to the second. This used the gotoRange method on the first cursor, passing in the second cursor and a True to tell it to expand selection.

Cursor Example:
http://api.openoffice.org/docs/DevelopersGuide/Text/Text.xhtml#1_3_1_1_Editing_Text

Then I created an autoText container, group and element containing the selection. and inserted /pasted the content at a cursor position using the applyTo method of the autotext entry. I used a guid for the name of the autoText container so it would be unique and then deleted the container when I was done.

AutoText Example:
http://api.openoffice.org/docs/DevelopersGuide/Text/Text.xhtml#1_3_1_6_Auto_Text

I can post my code if you want, however it's written in Python.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文