Date: prev next · Thread: first prev next last
2015 Archives by date, by thread · List index


If I were you I would set the Anchor before I insert the image into the document. So change the order of these lines:

    xText.insertTextContent(cursor, oGraphic, False)
    # Setting the anchor type - must be done first, otherwise the graphic will be put after the //
    oGraphic.AnchorType = AS_CHARACTER

Once the image is inserted into the document you won't always be able to change the anchor as you try to do in the code above. For some reason it works in your first example but as you can see it won't work in your second example.

So remember to always change the anchor of the graphic before you insert it otherwise you'll get more of these nice surprises. If I remember correctly there are a few more settings that should be done to the graphic object before it's inserted and some that needs to be set after, but I don't remember the details at the moment.

It seems that what is happening in the second case is that the image gets inserted before the anchor-change and when the re-anchoring occurs the image is already in the wrong position.

Regards,
Niklas Johansson

Piet van Oostrum skrev den 2015-04-15 16:00:
Hello,

I am in the process of converting several Java LO programming examples to Python. My plan is to do 
all (or most) of them. Also I try to extend them. However I encountered some surprises.

I converted LibreOffice4.4_SDK/examples/java/Text/GraphicsInserter.java which inserts a graphic 
image in a new Writer document. Works without problems.

Then I decided to add some text also, and put the picture in the middle of the text.

The code goes like this:

     # Create a new Writer document, which will be automatically displayed
     doc = Desktop.loadComponentFromURL("private:factory/swriter", "_blank", 0, ())

     # Getting the text
     xText = doc.getText()

     # Getting a cursor on the document
     xTextCursor = xText.createTextCursor()

     # Inserting some text
     xText.insertString(xTextCursor, "This is the first paragraph.", False)
     xText.insertControlCharacter(xTextCursor, PARAGRAPH_BREAK, False)
     xText.insertString(xTextCursor, "Some text before the graphic. ", False)

     # Insert the graphic
     insertGraphic(doc, xText, xTextCursor, sUrl)

     # Inserting some more text
     xText.insertString(xTextCursor, "Some text after the graphic.", False)
     xText.insertControlCharacter(xTextCursor, PARAGRAPH_BREAK, False)
     xText.insertString(xTextCursor, "This is the second paragraph.", False)

def insertGraphic(doc, xText, cursor, sUrl):
     oGraphic = doc.createInstance("com.sun.star.text.TextGraphicObject")
     xText.insertString(cursor, "++", False)
     # Insert graphic between ++ and //
     xText.insertTextContent(cursor, oGraphic, False)
     # Setting the anchor type - must be done first, otherwise the graphic will be put after the //
     oGraphic.AnchorType = AS_CHARACTER
     xText.insertString(cursor, "//", False)
     # Setting the graphic url
     oGraphic.GraphicURL = sUrl

     # Setting the width and height
     oGraphic.Width = 2000
     oGraphic.Height = 2000

The image is inserted in the middle of a paragraph and surrounded by ++ and // to indicate where it 
should be. This also works without problems.

Then I decided to change the order. First I insert all text; then I go back to the insertion point 
and insert the image. To my surprise the image ended up a couple of words later in the text

The insertion code is then:

     # Inserting some text
     xText.insertString(xTextCursor, "This is the first paragraph.", False)
     xText.insertControlCharacter(xTextCursor, PARAGRAPH_BREAK, False)
     xText.insertString(xTextCursor, "Some text before the graphic. ", False)

     # Inserting some more text
     xText.insertString(xTextCursor, "Some text after the graphic.", False)
     xText.insertControlCharacter(xTextCursor, PARAGRAPH_BREAK, False)
     xText.insertString(xTextCursor, "This is the second paragraph.", False)

     # Move the cursor
     xTextCursor.gotoPreviousParagraph(False)
     xTextCursor.gotoNextSentence(False)
# Insert the graphic
     insertGraphic(doc, xText, xTextCursor, sUrl)

Now I would expect ++Image// to be found just before "Some text after the graphic." However the ++// is 
indeed inserted there, but the image appears after "Some text after the", 4 words later. The image is indeed 
anchored to this position, as I can see when I manually insert some more text before it. So it is not a matter of an 
offset.

I have also tried to create another cursor with the endpoint of xTextCursor between the two 
sentences, and then use this to insert the graphic but that doesn't help.

Does anybody know what is happening here?


Context


Privacy Policy | Impressum (Legal Info) | Copyright information: Unless otherwise specified, all text and images on this website are licensed under the Creative Commons Attribution-Share Alike 3.0 License. This does not include the source code of LibreOffice, which is licensed under the Mozilla Public License (MPLv2). "LibreOffice" and "The Document Foundation" are registered trademarks of their corresponding registered owners or are in actual use as trademarks in one or more countries. Their respective logos and icons are also subject to international copyright laws. Use thereof is explained in our trademark policy.