LibreOffice UNO Java - Edit MSWORD - Move Line position(to right)

I am trying to edit a MS word document programatically with libreoffice UNO java api and convert to it to pdf. It works fine. but the Line format seems to have gone wrong. I opened the docx in LibreOffice GUI. I see it as


If I right click - Postion and Size → Horizontal from left to 1.0 it gets corrected. I am not sure how I do it using UNO java APIs.

As I saw line is Anchored as a paragraph I thought it is a Graphic. I tried this but got not elements.

XNameAccess xNameAccess = xTextGraphicObjectsSupplier.getGraphicObjects(); String[] allImages = xNameAccess.getElementNames( );

Then I realized it may be a shape. I am not sure how to do this programatically Please help

I am unfamiliar with Java, but the UNO API is essentially the same as in Basic, where this can be accomplished by:

lineOne = ThisComponent.Drawpage.getByIndex(1)
posOne = lineOne.getPosition
posOne.X = 1

I would be guessing at what that looks like in Java; with this tip you probably can figure it out pretty fast. As you can see the lines indeed are shapes, which are elements in the DrawPage. You create a Position object-- preferably from the existing Position of the shape-- then alter the X property of the object, and then run the setPosition method with the Position object as an argument.

I realize this is a partial answer.

Thanks for your reply. I was indeed trying to do that. But in the code below I always get xDrawDoc as null so I could not get Drawpages. Not sure if I am missing something.

XComponentContext xContext = Bootstrap.bootstrap( );
XMultiComponentFactory xMCF = xContext.getServiceManager( );
Object oDesktop = xMCF.createInstanceWithContext( "", xContext );
 XDesktop xDesktop = ( XDesktop ) UnoRuntime.queryInterface( XDesktop.class, oDesktop );
// Load the Document
String workingDir = "C:/Projects/";
String myTemplate = workingDir + "test.docx";
if ( !new File( myTemplate ).canRead( ) ) {
    throw new RuntimeException( "Cannot load template:" + new File( myTemplate ) );
XComponentLoader xCompLoader = ( XComponentLoader ) UnoRuntime.queryInterface(, xDesktop );
String sUrl = "file:///" + myTemplate;
PropertyValue[ ] propertyValues = new PropertyValue[ 0 ];
propertyValues = new PropertyValue[ 1 ];
propertyValues[ 0 ] = new PropertyValue( );
propertyValues[ 0 ].Name = "Hidden";
propertyValues[ 0 ].Value = new Boolean( true );
XComponent xComp = xCompLoader.loadComponentFromURL( sUrl, "_blank", 0, propertyValues );

XModel xModel =(XModel) UnoRuntime.queryInterface( XModel.class, xComp );
XDrawPagesSupplier xDrawDoc = UnoRuntime.queryInterface(XDrawPagesSupplier.class, xModel);
XDrawPages xDrawPages = xDrawDoc.getDrawPages();

Unfamiliar with java too… but getDrawPages() provide access to a multi-page drawing-layer (e.g. draw document). For a writer document I would use getDrawPage().


Thank you Pierre. getDrawPage() is working :slight_smile: