c++ - Libreoffice API (UNO):需要更改用户的 xTextField 文本
问题描述
是否有任何适当的方法可以使用 C++ UNO 更改用户创建的 xTextField 中的文本?这些字段名称是 com.sun.star.text.fieldmaster.User.[FIELD NAME]
我之前尝试过,但没有帮助: Libreoffice API (UNO) : text and data from xTextField
我也尝试过这样的事情,但仍然没有帮助:
// current_field - xTextField I got before
Reference<XText> xText = Reference<XText>(current_field, UNO_QUERY);
if (!xText.is())
{
qDebug() << "XText FROM xTextField IS NULL!";
return;
}
OUStringBuffer bufText;
bufText.append( new_value.utf16() );
std::stringstream textStr;
textStr << bufText.toString();
xText->setString( bufText.toString() );
有什么建议么?
解决方案
您是否按照我的其他答案中的建议阅读了Andrew's Macro Document 的第 5.18 节?下面是代码清单 5.49翻译成 C++ 的代码。该列表中似乎存在错误,因为我必须添加"."
才能使其正常工作。
OUString sName = OUString::createFromAscii("Author Name");
OUString sServ = OUString::createFromAscii("com.sun.star.text.FieldMaster.User");
OUString sFieldName = sServ + OUString::createFromAscii(".") + sName;
Reference< XMultiServiceFactory > xDocFactory (xTextDocument, UNO_QUERY);
if (xNamedFieldMasters->hasByName(sFieldName))
{
fieldMaster = xNamedFieldMasters->getByName(sFieldName);
Reference< XPropertySet> xProps (fieldMaster, UNO_QUERY);
Any aContent;
aContent <<= OUString::createFromAscii("Andrew Pitonyak");
xProps->setPropertyValue(OUString::createFromAscii("Content"), aContent);
}
else
{
fieldMaster <<= xDocFactory->createInstance(sServ);
Reference< XPropertySet> xProps (fieldMaster, UNO_QUERY);
Any aName;
aName <<= sName;
xProps->setPropertyValue(OUString::createFromAscii("Name"), aName);
Any aContent;
aContent <<= OUString::createFromAscii("Andrew Pitonyak");
xProps->setPropertyValue(OUString::createFromAscii("Content"), aContent);
}
如果此代码在空白文档上运行,则可以通过转到Insert -> Fields -> More Fields, Variables, User Field看到新创建的字段。
推荐阅读
- php - 无法在 PHPMailer 中邮寄多个正文行
- gcloud - gcloud storage mkdocs 和 index.html 不太好用
- c# - 无法将我的组合框项目保存到 appSettings 中的 MyStringCollection
- javascript - Chart.js 改变最大值栏的颜色
- angular - 将第三方库导入 Angular 库会出错
- css - 带有引导程序 4 的嵌套 flexbox 列在 Firefox 上显示错误,但在 chrome 上正常
- javascript - Javascript for循环填充对象
- java - 将我的 IP 数据包数据转换为字符串的正确方法是什么?
- angularjs - 如何在 angualrjs 中为动态创建的 SVG 元素添加工具提示?
- python - PyCharm 没有列出 python3.6