Search code examples
apilibreofficeopenoffice.orgunolibreoffice-basic

LibreOffice API/UNO: How to horizontal right-align text in table cell in writer


I'm using C++ to control LibreOffice/OpenOffice from another application, but I guess you can help me if you know the java-bridge as well. So basically I want to load a document (works), set the text of a cell (works) and set a table-cell to horizontal align right (which I got no idea how to do it):

I do:

// Load Document
Reference <XInterface> rDoc = myLoader->loadComponentFromURL(...); 

// Get Table
Reference <XTextTablesSupplier> rTablesSuppl(rDocument, UNO_QUERY);
Any any = rTablesSuppl->getTextTables()->getByName("Table1");
Reference<XTextTable> rTable(any, UNO_QUERY);

// Set Text in cell
Reference<XCellRange> rRange (rTable, UNO_QUERY);
Reference<XCell> rCell = rRange->getCellByPosition(x, y);
Reference<XTextRange> rTextRange(rCell, UNO_QUERY);
rTextRange->setString("MyNewText");

// Align "MyNewText" right
????

Any idea how to continue?


Solution

  • Caveat... While I have experience with C++, I use Java for LO API programming, so the following may be a bit off. You'll probably have to tweak a bit to get things going.

    In Java and using the cell name to get the cell, I right-justified text in a cell like this:

    XCell xCell = xTextTable.getCellByName(cellname);
    XText xText = UnoRuntime.queryInterface(XText.class, xCell);
    XPropertySet xPropertySet = UnoRuntime.queryInterface(XPropertySet.class, xText.getStart());
    xPropertySet.setPropertyValue("ParaAdjust", com.sun.star.style.ParagraphAdjust.RIGHT);
    

    In C++ and using the cell position to get the cell, I'm thinking that a rough translation would be:

    Reference<XCell> rCell = rRange->getCellByPosition(x, y);
    Reference<XText> rText(rCell, UNO_QUERY);
    Reference< XPropertySet > xPropSet( rText->getStart(), UNO_QUERY );
    xPropSet->getPropertyValue("ParaAdjust") >>= com::sun::star::style::ParagraphAdjust.RIGHT;
    

    Given what you've already got, it looks like you might be able to just replace your ???? with something like this:

    Reference< XPropertySet > xPropSet( rTextRange, UNO_QUERY );
    xPropSet->getPropertyValue("ParaAdjust") >>= com::sun::star::style::ParagraphAdjust.RIGHT;