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


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?
-- 
Piet van Oostrum <piet@vanoostrum.org>
WWW: http://pietvanoostrum.com/
PGP key: [8DAE142BE17999C4]

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.