Ok, now this is my current code:
Code: Select all
Option Explicit
Sub Main
Dim svg As String
svg = "<svg width=""100"" height=""100"" xmlns=""http://www.w3.org/2000/svg"">"
svg = svg & "<circle cx=""50"" cy=""50"" r=""50"" style=""fill:red;stroke:green;stroke-width=5px;""/>"
svg = svg & "</svg>"
dim imgUrl as string
imgUrl = SaveAsTemp(svg)
InsertGraphicObject(ThisComponent,imgUrl)
End Sub
Function SaveAsTemp(text As String) As String
Dim ps As Object
ps = CreateUnoService("com.sun.star.util.PathSettings")
Dim tempDir As String
tempDir = ps.Temp
Dim Path as String
Path=tempDir & "/" & "svg.svg"
Dim FileNo As Integer
FileNo = FreeFile()
Open convertfromUrl(Path) For Output As #FileNo
Print #FileNo, text
Close #FileNo
SaveAsTemp=Path
End Function
Sub InsertGraphicObject(oDoc, imgUrl)
Dim oText
oText = oDoc.getText()
Dim oCursor
oCursor = oText.createTextCursor()
oCursor.goToStart(FALSE)
Dim oGraph
oGraph = oDoc.createInstance("com.sun.star.text.GraphicObject")
With oGraph
.GraphicURL = imgUrl
.AnchorType = com.sun.star.text.TextContentAnchorType.AS_CHARACTER
End With
oText.insertTextContent( oCursor, oGraph, False )
End Sub
Works, but now the graphic is inserted as external linked item. How can I "unlink" this in the macro?
I also tried to insert the graphic as data url
Code: Select all
Option Explicit
Sub Main
Dim svg As String
svg="<svg width=""100"" height=""100"" xmlns=""http://www.w3.org/2000/svg"">"
svg=svg & "<circle cx=""50"" cy=""50"" r=""50"" style=""fill:red;stroke:green;stroke-width=5px;""/>"
svg=svg & "</svg>"
dim imgUrl as string
imgUrl="data:image/svg+xml;base64,"&EncodeBase64(svg)
InsertGraphicObject(ThisComponent,imgUrl)
End Sub
Function EncodeBase64(binData As String)
Dim siz As Integer : siz=Len(binData)
Dim b1,b2,b3,b123,r1,r2,r3,r4,i As Integer
Dim B64 As String : B64="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
Dim s As String
For i = 1 To siz Step 3
b1=ASC(Mid(binData,i+0,1))
If i+1 <= siz then
b2=ASC(Mid(binData,i+1,1))
Else
b2=0
End If
If i+2 <= siz then
b3=ASC(Mid(binData,i+2,1))
Else
b3=0
End If
b123=(b1*256+b2)*256+b3
r4 = b123 MOD 64 : b123 = (b123-r4)/64
r3 = b123 MOD 64 : b123 = (b123-r3)/64
r2 = b123 MOD 64
r1 = (b123-r2)/64
s = s & Mid(B64,R1+1,1)
s = s & Mid(B64,R2+1,1)
If i+1 <= siz then
s = s & Mid(B64,R3+1,1)
Else
s=s & "="
End If
If i+2 <= siz then
s = s & Mid(B64,R4+1,1)
Else
s=s & "="
End If
Next
EncodeBase64=s
End Function
Sub InsertGraphicObject(oDoc, imgUrl)
Dim oText
oText = oDoc.getText()
Dim oCursor
oCursor = oText.createTextCursor()
oCursor.goToStart(FALSE)
Dim oGraph
oGraph = oDoc.createInstance("com.sun.star.text.GraphicObject")
With oGraph
.GraphicURL = imgUrl
.AnchorType = com.sun.star.text.TextContentAnchorType.AS_CHARACTER
End With
oText.insertTextContent( oCursor, oGraph, False )
End Sub
But then the image is only displayed as an "inline image" placeholder. The Circle is only displayed in the property page. And it's lost, when I manually unink the graphic.
Any suggestions? Thanks for your help.