其实UNO的编程都是一个套路,通过文档工厂获得实例,然后看看该实例的类成员函数有哪些功能,设置可用的属性。当然,如果其中涉及到和其他组件或者实例交互的时候就需要自己仔细研究了。
处理段落的代码很简单,直接贴下来。
void manipulateParagraph(Reference <XTextDocument> xTextDocument)
{
Reference <XText> xText = xTextDocument->getText();
Reference<XTextCursor> xTextCursor(xText->createTextCursorByRange(xText->getEnd()),UNO_QUERY);
Reference <XPropertySet> xProps(xTextCursor,UNO_QUERY);
/// 设置左右间距
sal_Int32 margin;
margin = 3000;
Any any_margin;
any_margin <<= margin;
xProps->setPropertyValue("ParaLeftMargin",any_margin);
/// 设置字体
Any fontname;
fontname <<= OUString::createFromAscii("Arial");
xProps->setPropertyValue("CharFontName",fontname);
///设置字体颜色
sal_Int32 charcolor = 0x000099;
Any any_charcolor;
any_charcolor <<= charcolor;
xProps->setPropertyValue("CharColor",any_charcolor);
///设置粗体
Any fontweight;
fontweight <<=::com::sun::star::awt::FontWeight::BOLD;
xProps->setPropertyValue("CharWeight",fontweight);
///设置字体大小
sal_Int32 height;
height = 20;
Any any_height;
any_height <<= height;
xProps->setPropertyValue("CharHeight",any_height);
xText->insertString(xText->getEnd(),"This is a paragraph.We write words here and set paragraph style later.The effect is easy to see.Some atrributes are setted!",false);
///居中显示
sal_Int16 adjust = ::com::sun::star::style::ParagraphAdjust::ParagraphAdjust_CENTER;
Any any_adjust;
any_adjust <<= adjust;
xProps->setPropertyValue("ParaAdjust",any_adjust);
xText->insertControlCharacter(xText->getEnd(),::com::sun::star::text::ControlCharacter::PARAGRAPH_BREAK,false);
}