Try this macro, it uses the coefficient coef to recalculate the size of inserted SVG.
Sub PasteSVG
dim sUrlSVG$
sUrlSVG=ConvertToUrl("d:\a.svg") 'path to your file
insertSVG(sUrlSVG)
End Sub
Sub insertSVG(sSVG$) 'sSVG is url to your file; or direct "body" of SVG
dim oDoc as object, oShape as object, oSize as new com.sun.star.awt.Size, oSVG as object, oDlg as object
oDoc=ThisComponent
oSVG=urlSVG(sSVG) 'object with SVG
rem object SVG
const coef=0.4896 'coefficient to reduce the size of SVG (simulation of change the DPI)
with oSize
.Width=oSVG.Size100thMM.Width * coef
.Height=oSVG.Size100thMM.Height * coef
end with
oShape=oDoc.createInstance("com.sun.star.drawing.GraphicObjectShape")
with oShape
.Graphic=oSVG
.AnchorType=com.sun.star.text.TextContentAnchorType.AS_CHARACTER
.VertOrient=com.sun.star.text.VertOrientation.CHAR_CENTER
.setSize(oSize)
end with
oDoc.DrawPage.add(oShape) 'paste to document
End Sub
Function urlSVG(sUrl$) as object 'graphic object from SVG file
dim oSfa as object, oTextStream as object, oStream as object, oProvider as object, oSVG as object, props(0) as new com.sun.star.beans.PropertyValue
sUrl=ConvertToURL(sUrl)
oSfa=CreateUNOService("com.sun.star.ucb.SimpleFileAccess")
oStream=oSfa.openFileRead(sUrl)
oTextStream=CreateUNOService("com.sun.star.io.TextInputStream")
with oTextStream
.InputStream=oStream
.Encoding="UTF-8"
end with
oProvider=createUnoService("com.sun.star.graphic.GraphicProvider")
props(0).Name="InputStream" : props(0).Value=oStream
oSVG=oProvider.queryGraphic(props) 'graphic object
oTextStream.closeInput
oStream.closeInput
urlSVG=oSVG
End Function
How I got the coef? If some image is inserted via menu Insert > Image then it is sized according to DPI. And my experience is the image with DPI 165 is for size 1:1 (on Windows10). But mostly I have images with DPI 110, so I simulate the change of DPI by the change of image-size in the ratio 110/165=2/3 → example.
So I inserted SVG via menu Insert > Image and decreased its size by 2/3 and read its property Size.Height. Then I inserted SVG via macro and also read its property Size.Height. And the coef is ratio of these two Heights.
For reading the Sizes of elements in document I use macro for Xray
Sub xrayEl 'xray the selected element
GlobalScope.BasicLibraries.LoadLibrary("XrayTool")
dim oDoc as object, oSel as object, o as object
oDoc=ThisComponent
oSel=oDoc.CurrentController.getSelection
if isEmpty(oSel) then exit sub
o=oSel.getByIndex(0)
xray o
End Sub