Bug 119519 - Basic: insert graphicobject via ThisComponent.createInstance("com.sun.star.text.GraphicObject") not working in 6.1.0.3
Summary: Basic: insert graphicobject via ThisComponent.createInstance("com.sun.star.te...
Status: RESOLVED WORKSFORME
Alias: None
Product: LibreOffice
Classification: Unclassified
Component: Writer (show other bugs)
Version:
(earliest affected)
6.1.0.3 release
Hardware: All All
: medium normal
Assignee: Not Assigned
URL:
Whiteboard:
Keywords:
Depends on:
Blocks: Regressions-imageHandling
  Show dependency treegraph
 
Reported: 2018-08-26 19:24 UTC by Kai Struck
Modified: 2025-05-14 19:10 UTC (History)
6 users (show)

See Also:
Crash report or crash signature:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Kai Struck 2018-08-26 19:24:54 UTC
Description:
Tying to insert a linked graphicobject via ThisComponent.createInstance("com.sun.star.text.GraphicObject")
and 
oText.insertTextContent(oCursor, oGraph, False)
leads to an empty image frame (loading error)

Steps to Reproduce:
Customize the image url and run this Basic code:
Sub InsertGraphicObject
 Dim oDoc
 Dim sURL
 Dim oCursor
 Dim oGraph
 Dim oText
 
 sURL= ConvertToUrl("C:\Users\username\image.jpg")
 oDoc = ThisComponent
 oText = oDoc.getText()
 oCursor = oText.createTextCursor()
 oCursor.gotoStart(False)
 oGraph = oDoc.createInstance("com.sun.star.text.GraphicObject")
 
 With oGraph
 .GraphicURL = sURL
 .Width = 6000
 .Height = 8000
 End With
 
 oText.insertTextContent(oCursor, oGraph, False)
End Sub

Actual Results:
An empty image frame appears saying "Loading Error"

Expected Results:
The image should be inserted correctly and appear.


Reproducible: Always


User Profile Reset: No



Additional Info:
Comment 1 Drew Jensen 2018-08-26 20:36:04 UTC
Confirmed with Ubuntu 18.04 and 
Version: 6.1.1.0.0+
Build ID: 30c178dcb3301527ad92bbd245d1525ab77e314e

The macro works as expected in 6.0.5 and 6.2Alpha
Comment 2 Drew Jensen 2018-08-26 20:42:31 UTC
This could be related to a resolved issue from the changes to how images are handled.

See: https://bugs.documentfoundation.org/show_bug.cgi?id=117427
Comment 3 Drew Jensen 2018-08-26 20:44:55 UTC
Will add Tomaž Vajngerl to CC, perhaps he would know on that.
Comment 4 Xisco Faulí 2018-08-27 14:23:55 UTC
@Raal, is it the same case you investigated with Miklos in the Hamburg Hackfest?
Comment 5 Xisco Faulí 2018-08-27 15:17:55 UTC
Kai Struck, Could you please read the comments in bug 117427 to help you adapt your code?
Comment 6 Kai Struck 2018-08-28 09:52:54 UTC
Xisco Faulí,

Ok, after reading it and using the function getGraphFromUrlAsLink
for oGraph.Graphic I can make the above example code work: 


Sub InsertGraphicObject
 Dim oDoc
 Dim sURL
 Dim oCursor
 Dim oGraph
 Dim oText
 
 sURL= ConvertToUrl("http://pmg.pmgroup.be/enews/deroulard/exploit1.jpg")
 oDoc = ThisComponent
 oText = oDoc.getText()
 oCursor = oText.createTextCursor()
 oCursor.gotoStart(False)
 oGraph = oDoc.createInstance("com.sun.star.text.GraphicObject")
 oGraph.Graphic = getgraphfromurlaslink(sURL)  
 With oGraph
rem .GraphicURL = sURL
 .Width = 6000
 .Height = 8000
 End With
 
 oText.insertTextContent(oCursor, oGraph, False)

End Sub

function getGraphFromUrlAsLink(sFileUrl as String ) as Object 
  oProvider = createUnoService("com.sun.star.graphic.GraphicProvider") 
  Dim oPropsIN(1)as new com.sun.star.beans.PropertyValue 
  oPropsIN(0).Name  = "URL" 
  oPropsIN(0).Value = converttoURL(sFileUrl)
  oPropsIN(1).Name  = "LoadAsLink" 
  oPropsIN(1).Value = TRUE
  getGraphFromUrlAsLink = oProvider.queryGraphic(oPropsIN()) 
 end function


To get the URL of the image we now must use
oGraph.Graphic.OriginURL instead of oGraph.GraphicURL

If that means that all older code has to be adapted (possibly with a version check to make it compatible with older versions) then I'm not too happy about that.
Comment 7 Kai Struck 2018-08-28 10:10:01 UTC
With simple version check for compatibility for older Versions (just for info, still hoping not being forced to use it):
Sub InsertGraphicObject
 Dim oDoc
 Dim sURL
 Dim oCursor
 Dim oGraph
 Dim oText
 
 sURL= ConvertToUrl("http://pmg.pmgroup.be/enews/deroulard/exploit1.jpg")
 oDoc = ThisComponent
 oText = oDoc.getText()
 oCursor = oText.createTextCursor()
 oCursor.gotoStart(False)
 oGraph = oDoc.createInstance("com.sun.star.text.GraphicObject")
 
if Officeversion="LibreOffice6.1" then 
oGraph.Graphic = getgraphfromurlaslink(sURL) 
else
oGraph.GraphicURL = sURL
end if 

 With oGraph
 .Width = 6000
 .Height = 8000
 End With
 
 oText.insertTextContent(oCursor, oGraph, False)
 
if Officeversion="LibreOffice6.1" then 
msgbox oGraph.Graphic.OriginURL
else
msgbox oGraph.GraphicURL
end if 

End Sub

function getGraphFromUrlAsLink(sFileUrl as String ) as Object 
  oProvider = createUnoService("com.sun.star.graphic.GraphicProvider") 
  Dim oPropsIN(1)as new com.sun.star.beans.PropertyValue 
  oPropsIN(0).Name  = "URL" 
  oPropsIN(0).Value = converttoURL(sFileUrl)
  oPropsIN(1).Name  = "LoadAsLink" 
  oPropsIN(1).Value = TRUE
  getGraphFromUrlAsLink = oProvider.queryGraphic(oPropsIN()) 
 end function

function Officeversion
GlobalScope.BasicLibraries.loadLibrary("Tools")
Officeversion =  GetProductName()
end function
Comment 8 Kai Struck 2018-08-30 12:36:15 UTC
Hallo,
will the api changes go into any version higher than 6.0.x.x?
If so then I would use a version check like:

if val(replace(Officeversion,"LibreOffice",""))>6 then
oGraph.Graphic = getgraphfromurlaslink(sURL) 
else
oGraph.GraphicURL = sURL
end if 

function Officeversion
If (Not GlobalScope.BasicLibraries.isLibraryLoaded("Tools")) Then GlobalScope.BasicLibraries.LoadLibrary("Tools")
Officeversion =  GetProductName()
end function
Comment 9 Mike Kaganski 2019-03-06 08:29:49 UTC
I have just tested the code from comment 6, as is, in Apache OpenOffice 3.4.1. And it just works. The API used there should be working down to OOo 3.3.0 (in 3.2.0, neither that, nor previous code would work). So - what is the reason for version checks suggested in comments 7 and 8?
Comment 10 Mike Kaganski 2019-03-06 08:40:27 UTC
Ah, sorry - not "as is"; just with uncommented ".GraphicURL = sURL" line, which also doesn't give problems here with LO 6.2.2.1 (and could be wrapped into On Error Resume Next ... On Error Goto 0).
Comment 11 Kai Struck 2020-02-01 20:21:22 UTC
@ Mike Kaganski

The Code from Comment 6 only "seems" to work without problems in older LO versions and OO. But it doesn't link the image. the image will be embedded.
At least on all my tests here. 

Therefore the version checks.
Comment 12 Kai Struck 2020-02-02 20:58:12 UTC
Ah, ok , forget my previous answer, I misread your comment 10.

You are right! It should work for all versions (LO and OO) with this code:


Sub InsertGraphicObject
 Dim oDoc
 Dim sURL
 Dim oCursor
 Dim oGraph
 Dim oText
 
 sURL= ConvertToUrl("http://pmg.pmgroup.be/enews/deroulard/exploit1.jpg")
 oDoc = ThisComponent
 oText = oDoc.getText()
 oCursor = oText.createTextCursor()
 oCursor.gotoStart(False)
 oGraph = oDoc.createInstance("com.sun.star.text.GraphicObject")
 oGraph.Graphic = getgraphfromurlaslink(sURL)  
 With oGraph
 .GraphicURL = sURL
 .Width = 6000
 .Height = 8000
 End With
 
 oText.insertTextContent(oCursor, oGraph, False)

End Sub

function getGraphFromUrlAsLink(sFileUrl as String ) as Object 
  oProvider = createUnoService("com.sun.star.graphic.GraphicProvider") 
  Dim oPropsIN(1)as new com.sun.star.beans.PropertyValue 
  oPropsIN(0).Name  = "URL" 
  oPropsIN(0).Value = converttoURL(sFileUrl)
  oPropsIN(1).Name  = "LoadAsLink" 
  oPropsIN(1).Value = TRUE
  getGraphFromUrlAsLink = oProvider.queryGraphic(oPropsIN()) 
 end function
Comment 13 QA Administrators 2022-02-02 03:40:19 UTC Comment hidden (obsolete)
Comment 14 QA Administrators 2024-02-03 03:15:43 UTC Comment hidden (obsolete)
Comment 15 fpy 2025-05-14 19:07:38 UTC
(In reply to Kai Struck from comment #11)
>  link the image

code of Comment 12 tested OK 

Version: 24.8.6.2 (X86_64) / LibreOffice Community
Build ID: 480(Build:2)
CPU threads: 4; OS: Linux 6.11; UI render: default; VCL: gtk3
Locale: en-US (en_US.UTF-8); UI: en-US
Ubuntu package version: 4:24.8.6-0ubuntu0.24.10.2
Calc: threaded