Ask Your Question

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

asked 2015-08-05 09:47:38 +0200

pingu gravatar image

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 image description.

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

edit retag flag offensive close merge delete

2 Answers

Sort by » oldest newest most voted

answered 2015-08-05 23:28:35 +0200

doug gravatar image

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.

edit flag offensive delete link more

answered 2015-08-06 08:53:52 +0200

pingu gravatar image

updated 2015-08-06 08:54:40 +0200

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();
edit flag offensive delete link more



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().


pierre-yves samyn gravatar imagepierre-yves samyn ( 2015-08-06 09:17:12 +0200 )edit

Thank you Pierre. getDrawPage() is working :)

pingu gravatar imagepingu ( 2015-08-12 17:13:30 +0200 )edit
Login/Signup to Answer

Question Tools

1 follower


Asked: 2015-08-05 09:47:38 +0200

Seen: 266 times

Last updated: Aug 06 '15