Libreoffice API(UNO):来自xTextField的文本和数据 [英] Libreoffice API (UNO) : text and data from xTextField

查看:171
本文介绍了Libreoffice API(UNO):来自xTextField的文本和数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何从.odt文档中正确获取xTextFields?

How can I get xTextFields from .odt document properly?

我尝试了类似的方法,但是它不起作用(任何人都返回nullptr地址):

I tried something like that, but it doesn't work (Any returns a nullptr address):

Reference <XTextFieldsSupplier> xTextFieldsSupplier (xTextDoc, UNO_QUERY);
if (!xTextFieldsSupplier.is())
    return { };

Reference<XNameAccess> xTextFieldsInfo = xTextFieldsSupplier->getTextFieldMasters();
if (!xTextFieldsInfo.is())
    return { };

Sequence<OUString> xTextFieldsNames = xTextFieldsInfo->getElementNames();

Any any;
for (::rtl::OUString* field = xTextFieldsNames.begin();
        field != xTextFieldsNames.end();
        field++) {
    std::stringstream field_string;
    field_string << *field;
    QString fieldName = QString::fromStdString(field_string.str());
    any = xTextFieldsInfo->getByName(*field);

    Reference< XTextField > xField(any, UNO_QUERY);

    // other code to work with xField
}

推荐答案

XTextFieldsSupplier has two methods, and it looks like you chose the wrong one. The method to get text fields is getTextFields().

示例代码:

Reference< XEnumerationAccess > xFieldsEnumAccess = xTextFieldsSupplier->getTextFields();
Reference< XEnumeration > xFieldsEnum = xFieldsEnumAccess->createEnumeration();
Reference< XTextRange > xTextRange;
while ( xFieldsEnum->hasMoreElements() )
{
    Any aNextElement = xFieldsEnum->nextElement();
    Reference< XTextField > xField(aNextElement, UNO_QUERY);
    OUString presentation = xField->getPresentation(true);
    xTextRange = xText->getEnd();
    xTextRange->setString(presentation + OUString::createFromAscii("\n"));
}

如果您想处理文本字段母版,那么您的代码基本上是正确的.

If you want to deal with text field masters instead, then your code is mostly correct.

Any aFieldMaster;
aFieldMaster = xNamedFieldMasters->getByName(*field);

编辑:

这是xText的来源.

Reference < XTextDocument > xTextDocument (xComponent,UNO_QUERY);
Reference< XText > xText = xTextDocument->getText();

编辑2 :

这里是更改文本字段的示例.从一个新的Writer文档开始,然后转到 Insert->.字段->更多字段.在功能标签下,双击输入字段.输入"hello"在文本框区域中,然后按确定.

Here is an example of changing a text field. Start with a new Writer document and go to Insert -> Field -> More Fields. Under the Functions tab, double-click Input Field. Enter "hello" in the text box area and press OK.

然后,运行以下代码.

Reference< XServiceInfo > xInfo (xField, UNO_QUERY);
OUString sContent;
if (xInfo->supportsService("com.sun.star.text.TextField.Input"))
{
    Reference< XPropertySet > xProps (xField, UNO_QUERY);
    Any aContent = xProps->getPropertyValue(OUString::createFromAscii("Content"));
    aContent >>= sContent;
    sContent += OUString::createFromAscii(" there");
    aContent <<= sContent;
    xProps->setPropertyValue(OUString::createFromAscii("Content"), aContent);
    Reference< XRefreshable > xRefreshable (xFieldsEnumAccess, UNO_QUERY);
    xRefreshable->refresh();
}

现在,该字段包含"hello there".

Now, the field contains "hello there".

有关更多信息,请查看安德鲁的宏文档部分 5.18用户字段.

For more information, please review Andrew's Macro Document section 5.18 User Fields.

这篇关于Libreoffice API(UNO):来自xTextField的文本和数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆