Page 1 of 1

Adding image as embedded in Calc

Posted: Mon Jul 10, 2017 7:07 am
by jari
I try to insert image in the calc sheet with below macro:

Code: Select all

Dim aPos As New com.sun.star.awt.Point
Dim aSize As New com.sun.star.awt.Size

Sheet=Doc.Sheets.getByName("Sheet2")
oDrawPage=Sheet.DrawPage()

  oLinked=Doc.createInstance("com.sun.star.drawing.GraphicObjectShape")
  url$=sPath+"/"+sFile 
  aPos.X=(Sheet.getCellByPosition((Nx+2)*i+pX+1,(Ny+3)*j+pY+6).Position.X)+rX
  aPos.Y=(Sheet.getCellByPosition((Nx+2)*i+pX+1,(Ny+3)*j+pY+6).Position.Y)+rY

  oLinked.GraphicURL=converttourl(url$)
[b]  oLinked.Anchor=Sheet.getCellByPosition((Nx+2)*i+pX+1,(Ny+3)*j+pY+6)[/b]

  oLinked.RotateAngle=rot
  oLinked.ShearAngle=0
  If mirrorX="Y" Then
    oLinked.IsMirrored=True
  Else
    oLinked.IsMirrored=False
  End If
  oLinked.Position=aPos
  oLinked.Size=aSize
  oLinked.Name=sDie
[b]  oLinked.AsLink=False[/b]
  
  oDrawPage.add(oLinked)
but it seems that the "Anchor" and "AsLink" properties do not work in this context.

Can you inform what is the proper way to make the anchoring to a cell and to insert the image either as link or as embedded?

Re: Adding image as embedded

Posted: Mon Jul 10, 2017 7:36 am
by Zizi64

Re: Adding image as embedded

Posted: Mon Jul 10, 2017 8:01 am
by jari
The anchoring seems to work...
But the examples for the embedded image insertion might work for the unodispatcher case only.
I'll try to check in more detail. If there is sample for embedded image insertion without the unodispatcher, that would be more desirable.

Re: Adding image as embedded

Posted: Mon Jul 10, 2017 8:08 am
by Zizi64
Have you studied Andrew Pitonyak's free Macro books?

Re: Adding image as embedded

Posted: Mon Jul 10, 2017 8:12 am
by jari
I think I might not find the right one of these macrobooks for solving this issue. Maybe since it might happen by "breaking the link" rather than with the "AsLink".
I should try test little more with the below sample I found:
ExternalLinks = ThisComponent.ExternalDocLinks
Link = ExternalLinks.getByIndex(0)
Link.release()

Re: Adding image as embedded

Posted: Mon Jul 10, 2017 1:33 pm
by John_Ha
Have you checked the Macros and UNO API forum for ideas?

Re: Adding image as embedded in Calc

Posted: Tue Jul 11, 2017 2:09 am
by jari
I try to check that also. But it and some of the books look more machine readable. Some of the code did not work in my calc. Maybe that was written for different version of the tool. I'm using the 4.1.3.

Re: Adding image as embedded in Calc

Posted: Tue Jul 11, 2017 6:49 am
by Zizi64
Some of the code did not work in my calc.
What code did not work?

Note:
the code examles of this forum are often a part only of a whole function or of a whole subroutine. You must to complement them with the obvious parts (it is obvious thing for a programmer).

Re: Adding image as embedded in Calc

Posted: Wed Jul 12, 2017 4:03 am
by jari
It embedded the images twice (the document size in Gb become huge) if I inserted the images twice.
I found the bitmaptable is better to clean before repeating importing images with the below code:

oBitmaps=ThisComponent.createInstance("com.sun.star.drawing.BitmapTable")

Dim aElementNames() as String
aElementNames()=oBitmaps.getElementNames()
i=lBound(aElementNames())
While i<=uBound(aElementNames())
oBitmaps.removeByName(aElementNames(i))
i=i+1
Wend

1.) But I could not find how to anchor the images to a calc cell (or to "protect the position" as described in the menu).
2.) And also could not find why the removeByName operates only in the "Sheet2". The oBitmaps is defined for the whole document, if I did not mistake about the above definition.

In the file "content.xml" the protection of the image position (and size) can be seen as:
<style:graphic-properties draw:stroke="none" draw:fill="none" draw:textarea-horizontal-align="center" draw:textarea-vertical-align="middle" draw:color-mode="standard" draw:luminance="
0%" draw:contrast="0%" draw:gamma="100%" draw:red="0%" draw:green="0%" draw:blue="0%" fo:clip="rect(0cm, 0cm, 0cm, 0cm)" draw:image-opacity="100%" style:mirror="none" style:protect="positio
n size"
/>
This is is easy, if I operate by the menus. But to make the protect image position by the macro is hard to be found.

Re: Adding image as embedded in Calc

Posted: Wed Jul 12, 2017 6:09 am
by Zizi64
(the document size in Gb become huge) if I inserted the images twice.
What size of the pictures you want to embed? And what type they have?

Never use .bmp type pictures, and always optimize your pictures before you embed them into a document. You not needed a huge byte-size picture in an electronic or in a printed document.
Use the .png or .jpg or .gif filetype with max. 256 colors, and max. 100-200 dpi resolution. Reduce the phisical size of the picture to the necessary value before you embed a picture into a document. Use a third party software for this task (for example: the IrfanView).

Re: Adding image as embedded in Calc

Posted: Wed Jul 12, 2017 6:40 am
by jari
Using gif might help, but I could not see large difference of file size between bmp and gif. I need to keep the original data and image compression is not desirable.
Size: Name:
86123 WN174N63_S00_W01_X30Y30_VRESET_STD_FULL_IMAGE.bmp
85860 WN174N63_S00_W01_X30Y30_VRESET_STD_FULL_IMAGE.gif
86123 WN174N63_S00_W02_X30Y30_VRESET_STD_FULL_IMAGE.bmp
86831 WN174N63_S00_W02_X30Y30_VRESET_STD_FULL_IMAGE.gif
Above example did not have large difference of file size between bmp and gif.

Re: Adding image as embedded in Calc

Posted: Wed Jul 12, 2017 6:43 am
by Zizi64
But to make the protect image position by the macro is hard to be found.
Please download, install and use the MRI or the Xray exrension if you want study the controllable properties of the programming objects:
Xray shapes.png
This extension will inform you, what properties and methods a specific object have.

Code: Select all

Sub Images_ProtectSizeAndPosition

	oSheet = thiscomponent.getcurrentcontroller.activesheet
	oDrawPage = oSheet.drawPage   
	nNumShapes = oDrawPage.getCount()
	For i = 0 To nNumShapes - 1
		oShape = oDrawPage.getByIndex( i )

'The extension Xray will show all of the properties of the oShape object.
Xray oShape 

'and then, you can use the needed properties based on the displayed information.:
		oShape.MoveProtect = true
		oShape.SizeProtect = true
'((Finally you can delete the line "Xray" from the code))
	next i
	
End Sub


//Please use the Code TAG for your code snippets in this forum//

Re: Adding image as embedded in Calc

Posted: Wed Jul 12, 2017 6:51 am
by Zizi64
And please upload a real .ods type sample file with the embedded macro, (and with the picture if tit is necessary).

Re: Adding image as embedded in Calc

Posted: Wed Jul 12, 2017 6:56 am
by Zizi64
Size: Name:
86123 WN174N63_S00_W01_X30Y30_VRESET_STD_FULL_IMAGE.bmp
85860 WN174N63_S00_W01_X30Y30_VRESET_STD_FULL_IMAGE.gif
86123 WN174N63_S00_W02_X30Y30_VRESET_STD_FULL_IMAGE.bmp
86831 WN174N63_S00_W02_X30Y30_VRESET_STD_FULL_IMAGE.gif
Are the size values in Bytes?
80-90 KiB size is not a huge picture really.

Re: Adding image as embedded in Calc

Posted: Thu Jul 13, 2017 4:37 am
by jari
It seems to work now. What I was searching was:
MoveProtect=True

The XRAY-tool ran out of "stack memory". Not sure yet why this happens. The system has 64Gb of memory.
Probably something to do with the openoffice version (4.1.3) compatibility with this tool version or maybe the document has too many imges.
I'll check further.

Sometimes also the basic-macros seem slow.
I wonder if using python or java would give faster performance.