Adding image as embedded in Calc

Creating a macro - Writing a Script - Using the API

Adding image as embedded in Calc

Postby jari » Mon Jul 10, 2017 7:07 am

I try to insert image in the calc sheet with below macro:

Code: Select all   Expand viewCollapse view
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
jari
 
Posts: 11
Joined: Wed Sep 02, 2015 11:25 am

Re: Adding image as embedded

Postby Zizi64 » Mon Jul 10, 2017 7:36 am

Tibor Kovacs, Hungary; LibreOffice4.4.7 on Win7x64Prof.
And the portable versions: LO3.3.0-LO5.3.4 and AOO4.1.3
Please, edit the topic's initial post, and add the word "[Solved]" at the beginning of the subject line - if your problem has been solved.
User avatar
Zizi64
Volunteer
 
Posts: 5624
Joined: Wed May 26, 2010 7:55 am
Location: Budapest, Hungary

Re: Adding image as embedded

Postby jari » Mon Jul 10, 2017 8:01 am

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
jari
 
Posts: 11
Joined: Wed Sep 02, 2015 11:25 am

Re: Adding image as embedded

Postby Zizi64 » Mon Jul 10, 2017 8:08 am

Have you studied Andrew Pitonyak's free Macro books?
Tibor Kovacs, Hungary; LibreOffice4.4.7 on Win7x64Prof.
And the portable versions: LO3.3.0-LO5.3.4 and AOO4.1.3
Please, edit the topic's initial post, and add the word "[Solved]" at the beginning of the subject line - if your problem has been solved.
User avatar
Zizi64
Volunteer
 
Posts: 5624
Joined: Wed May 26, 2010 7:55 am
Location: Budapest, Hungary

Re: Adding image as embedded

Postby jari » Mon Jul 10, 2017 8:12 am

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
jari
 
Posts: 11
Joined: Wed Sep 02, 2015 11:25 am

Re: Adding image as embedded

Postby John_Ha » Mon Jul 10, 2017 1:33 pm

Have you checked the Macros and UNO API forum for ideas?
AOO 4.1.3, Windows 7 Home 64 bit

See the Writer Manual, the Writer FAQ, the Writer Tutorials and the up to date Writer guide for information. Click the Help button on a pop-up window for extensive help on that function.
John_Ha
Volunteer
 
Posts: 4179
Joined: Fri Sep 18, 2009 5:51 pm
Location: UK

Re: Adding image as embedded in Calc

Postby jari » Tue Jul 11, 2017 2:09 am

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
jari
 
Posts: 11
Joined: Wed Sep 02, 2015 11:25 am

Re: Adding image as embedded in Calc

Postby Zizi64 » Tue Jul 11, 2017 6:49 am

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; LibreOffice4.4.7 on Win7x64Prof.
And the portable versions: LO3.3.0-LO5.3.4 and AOO4.1.3
Please, edit the topic's initial post, and add the word "[Solved]" at the beginning of the subject line - if your problem has been solved.
User avatar
Zizi64
Volunteer
 
Posts: 5624
Joined: Wed May 26, 2010 7:55 am
Location: Budapest, Hungary

Re: Adding image as embedded in Calc

Postby jari » Wed Jul 12, 2017 4:03 am

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
jari
 
Posts: 11
Joined: Wed Sep 02, 2015 11:25 am

Re: Adding image as embedded in Calc

Postby Zizi64 » Wed Jul 12, 2017 6:09 am

(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; LibreOffice4.4.7 on Win7x64Prof.
And the portable versions: LO3.3.0-LO5.3.4 and AOO4.1.3
Please, edit the topic's initial post, and add the word "[Solved]" at the beginning of the subject line - if your problem has been solved.
User avatar
Zizi64
Volunteer
 
Posts: 5624
Joined: Wed May 26, 2010 7:55 am
Location: Budapest, Hungary

Re: Adding image as embedded in Calc

Postby jari » Wed Jul 12, 2017 6:40 am

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
jari
 
Posts: 11
Joined: Wed Sep 02, 2015 11:25 am

Re: Adding image as embedded in Calc

Postby Zizi64 » Wed Jul 12, 2017 6:43 am

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   Expand viewCollapse view
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; LibreOffice4.4.7 on Win7x64Prof.
And the portable versions: LO3.3.0-LO5.3.4 and AOO4.1.3
Please, edit the topic's initial post, and add the word "[Solved]" at the beginning of the subject line - if your problem has been solved.
User avatar
Zizi64
Volunteer
 
Posts: 5624
Joined: Wed May 26, 2010 7:55 am
Location: Budapest, Hungary

Re: Adding image as embedded in Calc

Postby Zizi64 » Wed Jul 12, 2017 6:51 am

And please upload a real .ods type sample file with the embedded macro, (and with the picture if tit is necessary).
Tibor Kovacs, Hungary; LibreOffice4.4.7 on Win7x64Prof.
And the portable versions: LO3.3.0-LO5.3.4 and AOO4.1.3
Please, edit the topic's initial post, and add the word "[Solved]" at the beginning of the subject line - if your problem has been solved.
User avatar
Zizi64
Volunteer
 
Posts: 5624
Joined: Wed May 26, 2010 7:55 am
Location: Budapest, Hungary

Re: Adding image as embedded in Calc

Postby Zizi64 » Wed Jul 12, 2017 6:56 am

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; LibreOffice4.4.7 on Win7x64Prof.
And the portable versions: LO3.3.0-LO5.3.4 and AOO4.1.3
Please, edit the topic's initial post, and add the word "[Solved]" at the beginning of the subject line - if your problem has been solved.
User avatar
Zizi64
Volunteer
 
Posts: 5624
Joined: Wed May 26, 2010 7:55 am
Location: Budapest, Hungary

Re: Adding image as embedded in Calc

Postby jari » Thu Jul 13, 2017 4:37 am

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
XRAY1.gif
The "XRAY -tool" runs out of stack memory
XRAY1.gif (5.72 KiB) Viewed 351 times
Red Hat Enterprise Linux Workstation release 6.6 (Santiago)
LibreOffice 4.0.4.2
jari
 
Posts: 11
Joined: Wed Sep 02, 2015 11:25 am


Return to Macros and UNO API

Who is online

Users browsing this forum: No registered users and 12 guests