Hello allirpa,

On Sunday 08 March 2009, 10:25, allirpa wrote:
> but what if the document is not really new? i mean, it already has contents
> and the image must be inserted from where the cursor is.

I assume you're talking about a Writer document, and the cursor is the 
blinking insertion point.

If so, some people use the ViewCursor. IMHO the following is the safer way if 
you do not want to insert anything when the user has selected something (code 
in OOo Basic, do yourself the Java translation):


Sub Main
        Dim oDoc as Object
        oDoc = ThisComponent
        
        Dim oController as Object
        oController = oDoc.getCurrentController()
        
        Dim oSelection as Object
        oSelection = oController.getSelection()
        
        If NOT IsNull(oSelection) and 
oSelection.supportsService("com.sun.star.text.TextRanges") Then
                If oSelection.getCount() = 1 Then
                        
                        Dim oTextRange as Object, oText as Object
                        oTextRange = oSelection.getByIndex(0)
                        oText = oTextRange.getText()
                        
                        Dim oTextCursor as Object
                        oTextCursor = oText.createTextCursorByRange(oTextRange)
                        
                        If oTextCursor.isCollapsed() Then
                                Dim oGraphic as Object
                                oGraphic = insertGraphic(oDoc, oTextCursor, _
                                        
convertToURL("/home/arielfed/Imágenes/Accelerators.xcu.assertion.png"))
                        End If
                End If
        End If
End Sub

Function insertGraphic( oDoc as com.sun.star.text.XTextDocument, _
                                                oCursor as 
com.sun.star.text.XTextCursor,_
                                                sURL$)
        On Error Resume Next
        Dim oGraphic as Object
        Dim oGraphicProvider as Object
        
        oGraphicProvider = 
createUnoService("com.sun.star.graphic.GraphicProvider")     
        Dim aMediaProperties (0)as new com.sun.star.beans.PropertyValue
        aMediaProperties(0).Name  = "URL"
        aMediaProperties(0).Value = sURL        
        oGraphic = oGraphicProvider.queryGraphic(aMediaProperties())
        
        Dim oGraphicShape as Object
        oGraphicShape = 
oDoc.createInstance("com.sun.star.drawing.GraphicObjectShape")
        Dim aSize as New com.sun.star.awt.Size
        aSize.Width = 3000
        aSize.Height = 2000     
        oGraphicShape.setSize( aSize )

        oCursor.getText().insertTextContent(oCursor, oGraphicShape, False)      
        oGraphicShape.setPropertyValue("Graphic", oGraphic)     
        
        insertGraphic = oGraphicShape
End Function


Regards
-- 
Ariel Constenla-Haile
La Plata, Argentina


"Aus der Kriegsschule des Lebens
                - Was mich nicht umbringt,
        macht mich härter."
                Nietzsche Götzendämmerung, Sprüche und Pfeile, 8.

---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to