Libreoffice API(UNO):需要更改用户的xTextField文本 [英] Libreoffice API (UNO): need to change user's xTextField text
问题描述
是否存在使用C ++ UNO更改用户创建的xTextField中的文本的正确方法? 这些字段名称为com.sun.star.text.fieldmaster.User.[FIELD NAME]
Is there any proper way to change text in the user's created xTextField using C++ UNO? These fields names are com.sun.star.text.fieldmaster.User.[FIELD NAME]
我以前尝试过此方法,但没有帮助: Libreoffice API(UNO):来自xTextField的文本和数据
I tried this before, but it didn't help: Libreoffice API (UNO) : text and data from xTextField
我也尝试过类似的方法,但是仍然没有帮助:
Also I've tried something like this but still didn't help:
// 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的第5.18节我在其他答案中建议使用宏文档?这是翻译成C ++的清单5.49 .该清单似乎有一个错误,因为我必须添加"."
才能使其正常工作.
Did you read section 5.18 of Andrew's Macro Document as recommended in my other answer? Here is Listing 5.49 translated into C++. It seems there is a bug in that listing because I had to add "."
to make it work.
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->查看新创建的字段.字段->更多字段,变量,用户字段.
If this code is run on a blank document, the newly created field can be seen by going to Insert -> Fields -> More Fields, Variables, User Field.
这篇关于Libreoffice API(UNO):需要更改用户的xTextField文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!