Inserting Graphics in a Writer document

SOS sos at pmg.be
Wed Apr 15 07:40:33 PDT 2015


On 15/04/2015 16:00, Piet van Oostrum wrote:
> 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)
did you sure you are here where you think you are ? and not at the end 
off the wro,g paragraph
trye
# Move the cursor xTextCursor.gotoPreviousParagraph(False)

  xTextCursor.gotoPreviousParagraph(False)
xTextCursor.gotoNextSentence(False)

hope it helps

Fernand

>      
>      # 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?



More information about the LibreOffice mailing list