Page 1 of 1
[Solved] Insert Picture with API Writer
Posted: Tue Aug 20, 2019 4:26 pm
by Touf2638
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
' 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
Re: Insert Picture with API writer
Posted: Tue Aug 20, 2019 7:08 pm
by UnklDonald418
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.
Re: Insert Picture with API writer
Posted: Wed Aug 21, 2019 10:39 am
by Touf2638
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
oGraph.AnchorType = com.sun.star.text.TextContentAnchorType.AS_CHARACTER
I find this in vb6
Code: Select all
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
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))
Re: Insert Picture with API writer
Posted: Wed Aug 21, 2019 11:14 am
by Touf2638
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
Re: Insert Picture with API writer
Posted: Wed Aug 21, 2019 11:31 am
by Touf2638
I find it, I continue...
Re: Insert Picture with API writer
Posted: Wed Aug 21, 2019 11:41 am
by Touf2638
Ok so I success but I don't understand
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
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
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
Re: Insert Picture with API writer
Posted: Wed Aug 21, 2019 12:09 pm
by Touf2638
Ok, do .graphicURL is no longer works. It's replace by .graphic
code working
Code: Select all
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