Adding image as embedded in Calc

Creating a macro - Writing a Script - Using the API (OpenOffice Basic, Python, BeanShell, JavaScript)
Post Reply
jari
Posts: 12
Joined: Wed Sep 02, 2015 11:25 am

Adding image as embedded in Calc

Post 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?
Red Hat Enterprise Linux Workstation release 6.6 (Santiago)
LibreOffice 4.0.4.2
User avatar
Zizi64
Volunteer
Posts: 11353
Joined: Wed May 26, 2010 7:55 am
Location: Budapest, Hungary

Re: Adding image as embedded

Post by Zizi64 »

Tibor Kovacs, Hungary; LO7.5.8 /Win7-10 x64Prof.
PortableApps/winPenPack: LO3.3.0-7.6.2;AOO4.1.14
Please, edit the initial post in the topic: add the word [Solved] at the beginning of the subject line - if your problem has been solved.
jari
Posts: 12
Joined: Wed Sep 02, 2015 11:25 am

Re: Adding image as embedded

Post 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.
Red Hat Enterprise Linux Workstation release 6.6 (Santiago)
LibreOffice 4.0.4.2
User avatar
Zizi64
Volunteer
Posts: 11353
Joined: Wed May 26, 2010 7:55 am
Location: Budapest, Hungary

Re: Adding image as embedded

Post by Zizi64 »

Have you studied Andrew Pitonyak's free Macro books?
Tibor Kovacs, Hungary; LO7.5.8 /Win7-10 x64Prof.
PortableApps/winPenPack: LO3.3.0-7.6.2;AOO4.1.14
Please, edit the initial post in the topic: add the word [Solved] at the beginning of the subject line - if your problem has been solved.
jari
Posts: 12
Joined: Wed Sep 02, 2015 11:25 am

Re: Adding image as embedded

Post 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()
Red Hat Enterprise Linux Workstation release 6.6 (Santiago)
LibreOffice 4.0.4.2
John_Ha
Volunteer
Posts: 9583
Joined: Fri Sep 18, 2009 5:51 pm
Location: UK

Re: Adding image as embedded

Post by John_Ha »

Have you checked the Macros and UNO API forum for ideas?
LO 6.4.4.2, Windows 10 Home 64 bit

See the Writer Guide, the Writer FAQ, the Writer Tutorials and Writer for students.

Remember: Always save your Writer files as .odt files. - see here for the many reasons why.
jari
Posts: 12
Joined: Wed Sep 02, 2015 11:25 am

Re: Adding image as embedded in Calc

Post 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.
Red Hat Enterprise Linux Workstation release 6.6 (Santiago)
LibreOffice 4.0.4.2
User avatar
Zizi64
Volunteer
Posts: 11353
Joined: Wed May 26, 2010 7:55 am
Location: Budapest, Hungary

Re: Adding image as embedded in Calc

Post 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).
Tibor Kovacs, Hungary; LO7.5.8 /Win7-10 x64Prof.
PortableApps/winPenPack: LO3.3.0-7.6.2;AOO4.1.14
Please, edit the initial post in the topic: add the word [Solved] at the beginning of the subject line - if your problem has been solved.
jari
Posts: 12
Joined: Wed Sep 02, 2015 11:25 am

Re: Adding image as embedded in Calc

Post 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.
Red Hat Enterprise Linux Workstation release 6.6 (Santiago)
LibreOffice 4.0.4.2
User avatar
Zizi64
Volunteer
Posts: 11353
Joined: Wed May 26, 2010 7:55 am
Location: Budapest, Hungary

Re: Adding image as embedded in Calc

Post 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).
Tibor Kovacs, Hungary; LO7.5.8 /Win7-10 x64Prof.
PortableApps/winPenPack: LO3.3.0-7.6.2;AOO4.1.14
Please, edit the initial post in the topic: add the word [Solved] at the beginning of the subject line - if your problem has been solved.
jari
Posts: 12
Joined: Wed Sep 02, 2015 11:25 am

Re: Adding image as embedded in Calc

Post 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.
Red Hat Enterprise Linux Workstation release 6.6 (Santiago)
LibreOffice 4.0.4.2
User avatar
Zizi64
Volunteer
Posts: 11353
Joined: Wed May 26, 2010 7:55 am
Location: Budapest, Hungary

Re: Adding image as embedded in Calc

Post 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//
Tibor Kovacs, Hungary; LO7.5.8 /Win7-10 x64Prof.
PortableApps/winPenPack: LO3.3.0-7.6.2;AOO4.1.14
Please, edit the initial post in the topic: add the word [Solved] at the beginning of the subject line - if your problem has been solved.
User avatar
Zizi64
Volunteer
Posts: 11353
Joined: Wed May 26, 2010 7:55 am
Location: Budapest, Hungary

Re: Adding image as embedded in Calc

Post by Zizi64 »

And please upload a real .ods type sample file with the embedded macro, (and with the picture if tit is necessary).
Tibor Kovacs, Hungary; LO7.5.8 /Win7-10 x64Prof.
PortableApps/winPenPack: LO3.3.0-7.6.2;AOO4.1.14
Please, edit the initial post in the topic: add the word [Solved] at the beginning of the subject line - if your problem has been solved.
User avatar
Zizi64
Volunteer
Posts: 11353
Joined: Wed May 26, 2010 7:55 am
Location: Budapest, Hungary

Re: Adding image as embedded in Calc

Post 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.
Tibor Kovacs, Hungary; LO7.5.8 /Win7-10 x64Prof.
PortableApps/winPenPack: LO3.3.0-7.6.2;AOO4.1.14
Please, edit the initial post in the topic: add the word [Solved] at the beginning of the subject line - if your problem has been solved.
jari
Posts: 12
Joined: Wed Sep 02, 2015 11:25 am

Re: Adding image as embedded in Calc

Post 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.
Attachments
The &quot;XRAY -tool&quot; runs out of stack memory
The "XRAY -tool" runs out of stack memory
XRAY1.gif (5.72 KiB) Viewed 5278 times
Red Hat Enterprise Linux Workstation release 6.6 (Santiago)
LibreOffice 4.0.4.2
Post Reply