Position of graphics and paragraph in a Writer document

Creating a macro - Writing a Script - Using the API (OpenOffice Basic, Python, BeanShell, JavaScript)
Post Reply
_savage
Posts: 187
Joined: Sun Apr 21, 2013 12:55 am

Position of graphics and paragraph in a Writer document

Post by _savage »

As I iterate over the paragraphs of a document, I can access the paragraph's coordinates relative to the first page:

Code: Select all

controller = document.getCurrentController()
cursor = controller.getViewCursor()
par_enum = document.Text.createEnumeration()
while par_enum.hasMoreElements():
    par = par_enum.nextElement()
    cursor.gotoRange(par, False)
    pos = cursor.getPosition() # pos.X and pos.Y are the paragraph's coordinates
I can do the same with the graphics of a document, using the anchors to obtain the graphics' coordinates relative to the first page:

Code: Select all

shape_enum = document.DrawPage.createEnumeration()
while shape_enum.hasMoreElements():
    shape = shape_enum.nextElement()
    cursor.gotoRange(shape.Anchor, False)
    pos = cursor.getPosition() # pos.X and pos.Y are the paragraph's coordinates
However, these coordinates can be quite off from where I see the graphics, even on a different page! I assume that the anchor of a graphics doesn't necessarily represent the position of the graphics itself.

Question: How do I get the real coordinates of a graphics? Looks like using the anchor here is imprecise?
Mac 10.14 using LO 7.2.0.2, Gentoo Linux using LO 7.2.3.2 headless.
musikai
Volunteer
Posts: 294
Joined: Wed Nov 11, 2015 12:19 am

Re: Position of graphics and paragraph in a Writer document

Post by musikai »

You can get position infos of a graphic object with:

Code: Select all

posx = g.HoriOrientPosition
posy = g.VertOrientPosition
But the values depend on the anchor type and the HoriOrientRelation. If eg. the image is anchored AS_CHARACTER then the values aren't used at all.
If the image is set to be 3cm from left, it can be relative to the paragraph text area or to the entire page or.... There are at least 5 different relations possible.

So also this approach may become complex.

See also:
viewtopic.php?f=20&t=80905#p373220
viewtopic.php?f=7&t=82121
Win7 Pro, Lubuntu 15.10, LO 4.4.7, OO 4.1.3
Free Project: LibreOffice Songbook Architect (LOSA)
http://struckkai.blogspot.de/2015/04/li ... itect.html
Post Reply