[Solved] Insert Picture with API Writer

Creating a macro - Writing a Script - Using the API

[Solved] Insert Picture with API Writer

Postby Touf2638 » Tue Aug 20, 2019 4:26 pm

Hi,
I try to insert image with .net in a writer document. I have sucess to insert a document odt in an other document with LoadDocumentFromUrl but it doesn't work with picture file.
I try severals examples but unsucess. My last try is this but doesn't work. Can you help me or give me link with documentation on this.

Code: Select all   Expand viewCollapse view
        ' Variables
        Dim serviceManager As Object, oText As Object, oCursor As Object
        Dim Desktop As Object, Document As Object
        Dim Fichier As String
        Dim args(2) As Object

        'Création d'une instance Open Office
        serviceManager = CreateObject("com.sun.star.serviceManager")
        Desktop = serviceManager.createInstance("com.sun.star.frame.Desktop")

        'Ouverture du fichier
        Fichier = "file:///" & "C:/Poubelle/test.odt"
        Fichier = Replace(Fichier, "\", "/")
        Document = Desktop.loadComponentFromURL(Fichier, "_blank", 0, args)
        oText = Document.getText()

        'Création du curseur d'écriture
        oCursor = oText.createTextCursor
        oCursor.gotoStart(False)




        Dim dispatcher As Object = serviceManager.createInstance("com.sun.star.frame.DispatchHelper")

        Dim arg1(2) As Object
        arg1(0) = MakePropertyValue("FileName", "file:///" & "C:/Poubelle/Test.jpg")
        arg1(1) = MakePropertyValue("FilterName", "JPG")
        arg1(2) = MakePropertyValue("AsLink", False)

        dispatcher.executeDispatch(Document, ".uno:InsertGraphic", "", 0, arg1)


Thanks a lot
Last edited by Hagar Delest on Sun Sep 08, 2019 9:34 pm, edited 1 time in total.
Reason: tagged solved
OpenOffice 6.2 on Windows 10
Touf2638
 
Posts: 14
Joined: Tue Aug 20, 2019 4:21 pm

Re: Insert Picture with API writer

Postby UnklDonald418 » Tue Aug 20, 2019 7:08 pm

Go to
http://www.pitonyak.org/oo.php
download the document named "PDF English Macro Document"
On page 48 you should find Listing 5.29: Embed an image in a document.
If your problem has been solved, please edit this topic's initial post and add "[Solved]" to the beginning of the subject line
Apache OpenOffice 4.1.6 & LibreOffice 6.1.5.2 - Windows 10 Professional
UnklDonald418
Volunteer
 
Posts: 1225
Joined: Wed Jun 24, 2015 12:56 am
Location: Colorado, USA

Re: Insert Picture with API writer

Postby Touf2638 » Wed Aug 21, 2019 10:39 am

Thanks, it start to insert image but I have some problems.
I don't find equivalent to anchor type in vb.net

Code: Select all   Expand viewCollapse view
oGraph.AnchorType = com.sun.star.text.TextContentAnchorType.AS_CHARACTER


I find this in vb6

Code: Select all   Expand viewCollapse view
Dim xPropertySetFrame As unoidl.com.sun.star.beans.XPropertySet
xPropertySetFrame = DirectCast(xTextFrame, unoidl.com.sun.star.beans.XPropertySet)
xPropertySetFrame.setPropertyValue("AnchorType", New uno.Any( _
    GetType(unoidl.com.sun.star.text.TextContentAnchorType), _
    unoidl.com.sun.star.text.TextContentAnchorType.AS_CHARACTER))


but in .net ... I have start with this but uno. I block

Code: Select all   Expand viewCollapse view
        Dim oSM As Object
        oSM = CreateObject("com.sun.star.ServiceManager")

        Dim xPropertySetFrame As Object = oSM.Bridge_GetStruct("unoidl.com.sun.star.beans.XPropertySet")
        xPropertySetFrame = DirectCast(xTextFrame, unoidl.com.sun.star.beans.XPropertySet)
        xPropertySetFrame.setPropertyValue("AnchorType", New uno.Any(
    GetType(unoidl.com.sun.star.text.TextContentAnchorType),
    unoidl.com.sun.star.text.TextContentAnchorType.AS_CHARACTER))
OpenOffice 6.2 on Windows 10
Touf2638
 
Posts: 14
Joined: Tue Aug 20, 2019 4:21 pm

Re: Insert Picture with API writer

Postby Touf2638 » Wed Aug 21, 2019 11:14 am

I think with I see on net I need to imports somme dll in reference of project to use types. I don't find theses references... like (cli_basetypes.dll, cli_cppuihelper.dll, cli_oootypes.dll, cli_uno.dll, cli_ure.dll, cli_uretypes.dll
I have only cli_uno
OpenOffice 6.2 on Windows 10
Touf2638
 
Posts: 14
Joined: Tue Aug 20, 2019 4:21 pm

Re: Insert Picture with API writer

Postby Touf2638 » Wed Aug 21, 2019 11:31 am

I find it, I continue...
OpenOffice 6.2 on Windows 10
Touf2638
 
Posts: 14
Joined: Tue Aug 20, 2019 4:21 pm

Re: Insert Picture with API writer

Postby Touf2638 » Wed Aug 21, 2019 11:41 am

Ok so I success but I don't understand :D

I see my picture but when it is copy from shape to graphic, it don't works. So I have work only on shape and it works.
- First question why copy shape to graphic then remove shape
- Second question, if I understand link is done by "oGraph.graphicurl = oShape.graphicurl" and So for me it don't works

Code working

Code: Select all   Expand viewCollapse view
    Sub InsertImage(ByRef oDoc As Object, ByRef oCurs As Object, sURL As Object, sParStyle As Object)


        Dim serviceManager = CreateObject("com.sun.star.serviceManager")
        Dim oShape
        Dim oGraph
        Dim oProvider

        oShape = oDoc.createInstance("com.sun.star.drawing.GraphicObjectShape")
        oGraph = oDoc.createInstance("com.sun.star.text.GraphicObject")
        oDoc.getDrawPage().add(oShape)
        oProvider = CreateUnoService("com.sun.star.graphic.GraphicProvider")

        Dim oProps(0) As Object
        oProps(0) = MakePropertyValue("URL", sURL)

        REM Save the original size.
        Dim oSize100thMM
        Dim lHeight As Long
        Dim lWidth As Long
        oSize100thMM = RecommendGraphSize(oProvider.queryGraphicDescriptor(oProps))
        If Not IsNothing(oSize100thMM) Then
            lHeight = oSize100thMM.Height
            lWidth = oSize100thMM.Width
        End If
        oShape.Graphic = oProvider.queryGraphic(oProps)
        oGraph.graphicurl = oShape.graphicurl
        oGraph.AnchorType = unoidl.com.sun.star.text.TextContentAnchorType.AS_CHARACTER

        'oDoc.getDrawPage().remove(oShape)
        If lHeight > 0 And lWidth > 0 Then
            Dim oSize
            oSize = oGraph.Size
            oSize.Height = lHeight
            oSize.Width = lWidth
            oShape.Size = oSize
        End If

        ' Set the paragraph style if it is in the document.
        Dim oStyles
        oStyles = oDoc.StyleFamilies.getByName("ParagraphStyles")
        If oStyles.hasByName(sParStyle) Then
            oCurs.ParaStyleName = sParStyle
        End If

    End Sub


code not working

Code: Select all   Expand viewCollapse view
    Sub InsertImage(ByRef oDoc As Object, ByRef oCurs As Object, sURL As Object, sParStyle As Object)


        Dim serviceManager = CreateObject("com.sun.star.serviceManager")
        Dim oShape
        Dim oGraph
        Dim oProvider

        oShape = oDoc.createInstance("com.sun.star.drawing.GraphicObjectShape")
        oGraph = oDoc.createInstance("com.sun.star.text.GraphicObject")
        oDoc.getDrawPage().add(oShape)
        oProvider = CreateUnoService("com.sun.star.graphic.GraphicProvider")

        Dim oProps(0) As Object
        oProps(0) = MakePropertyValue("URL", sURL)

        REM Save the original size.
        Dim oSize100thMM
        Dim lHeight As Long
        Dim lWidth As Long
        oSize100thMM = RecommendGraphSize(oProvider.queryGraphicDescriptor(oProps))
        If Not IsNothing(oSize100thMM) Then
            lHeight = oSize100thMM.Height
            lWidth = oSize100thMM.Width
        End If
        oShape.Graphic = oProvider.queryGraphic(oProps)
        oGraph.graphicurl = oShape.graphicurl
        oGraph.AnchorType = unoidl.com.sun.star.text.TextContentAnchorType.AS_CHARACTER

       oDoc.getDrawPage().remove(oShape)
        If lHeight > 0 And lWidth > 0 Then
            Dim oSize
            oSize = oGraph.Size
            oSize.Height = lHeight
            oSize.Width = lWidth
            oGraph.Size = oSize
        End If

        ' Set the paragraph style if it is in the document.
        Dim oStyles
        oStyles = oDoc.StyleFamilies.getByName("ParagraphStyles")
        If oStyles.hasByName(sParStyle) Then
            oCurs.ParaStyleName = sParStyle
        End If

    End Sub
OpenOffice 6.2 on Windows 10
Touf2638
 
Posts: 14
Joined: Tue Aug 20, 2019 4:21 pm

Re: Insert Picture with API writer

Postby Touf2638 » Wed Aug 21, 2019 12:09 pm

Ok, do .graphicURL is no longer works. It's replace by .graphic

code working

Code: Select all   Expand viewCollapse view
    Sub InsertImage(ByRef oDoc As Object, ByRef oCurs As Object, sURL As Object, sParStyle As Object)


         ' Init variables and instance object
        Dim serviceManager As Object = CreateObject("com.sun.star.serviceManager")
        Dim oShape As Object
        Dim oGraph As Object
        Dim oProvider As Object
        oShape = oDoc.createInstance("com.sun.star.drawing.GraphicObjectShape")
        oGraph = oDoc.createInstance("com.sun.star.text.GraphicObject")
        oProvider = CreateUnoService("com.sun.star.graphic.GraphicProvider")

        ' Add shape to document
        oDoc.getDrawPage().add(oShape)

        ' Set property path of picture
        Dim oProps(0) As Object
        oProps(0) = MakePropertyValue("URL", sURL)

        ' Get size from picture to load
        Dim oSize100thMM
        Dim lHeight As Long
        Dim lWidth As Long
        oSize100thMM = RecommendGraphSize(oProvider.queryGraphicDescriptor(oProps))
        If Not IsNothing(oSize100thMM) Then
            lHeight = oSize100thMM.Height
            lWidth = oSize100thMM.Width
        End If

        ' Set size and path property to shape
        oShape.Graphic = oProvider.queryGraphic(oProps)

        ' Copy shape in graphic object and set anchor type
        oGraph.graphic = oShape.graphic
        oGraph.AnchorType = unoidl.com.sun.star.text.TextContentAnchorType.AS_CHARACTER

        ' Remove shape and resize graphix
        Dim oText = oCurs.getText()
        oText.insertTextContent(oCurs, oGraph, False)
        oDoc.getDrawPage().remove(oShape)
        If lHeight > 0 And lWidth > 0 Then
            Dim oSize
            oSize = oGraph.Size
            oSize.Height = lHeight
            oSize.Width = lWidth
            oGraph.Size = oSize
        End If

    End Sub
OpenOffice 6.2 on Windows 10
Touf2638
 
Posts: 14
Joined: Tue Aug 20, 2019 4:21 pm


Return to Macros and UNO API

Who is online

Users browsing this forum: Lupp and 5 guests