Page 1 of 1

Position of graphics and paragraph in a Writer document

Posted: Thu Apr 14, 2016 7:34 am
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?

Re: Position of graphics and paragraph in a Writer document

Posted: Thu Apr 14, 2016 10:15 pm
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