Seite 1 von 1

Images in Calc einfügen

Verfasst: Fr 21. Okt 2011, 13:29
von Marco
Hallo zusammen!

Ich möchte mit der cli-API in C# ein Bild per URL in einem Spreadsheet hinzufügen. Hab dazu auch bereits im OOo-Forum gesucht und auch tatsächlich einige Codebeispiele gefunden, in denen die Images per GraphicObjectShape und XDrawPage eingebunden wurden.

Code: Alles auswählen

var graphicObjectShape = (GraphicObjectShape)_xMultiServiceFactory.createInstance("com.sun.star.drawing.GraphicObjectShape");
graphicObjectShape.Position = ...;
XDrawPage drawPage = (XDrawPage)spreadsheet;
drawPage.add(graphicObjectShape);
Leider fehlt mir aber das com.sun.star.drawing.GraphicObjectShape im LO-SDK 3.4.3, wobei es zumindest noch in der Doku enthalten ist :shock:

Was muss ich machen, um ein Image auf's Spreadsheet zu bekommen?

Re: Images in Calc einfügen

Verfasst: Sa 22. Okt 2011, 12:35
von F3K Total
Hallo,
mit C# kenne ich mich nicht aus, aber folgender Basic Code funktioniert bei mir unter LO 3.4.3, Windows XP

Code: Alles auswählen

sub insert_bitmap

Dim Point As New com.sun.star.awt.Point
Dim Size As New com.sun.star.awt.Size
Url_Bitmap=converttourl("C:\Users\Bitmap1.jpg")

size.width = 5000
size.Height =7000
point.Y=200
point.X=300

odoc = thiscomponent
oshape = odoc.createInstance("com.sun.star.drawing.GraphicObjectShape")

with oshape
 .name = "Shape1"
 .size = size
 .position = point()
 .GraphicURL = Url_Bitmap
end with

osheet=odoc.currentcontroller.activesheet
opage=osheet.drawpage
opage.add(oshape)

'Wenn das Bild eingebettet werden soll zusätzlich das Folgende:

'oBitmap = odoc.createInstance( "com.sun.star.drawing.BitmapTable" )
'oName = oshape.LinkDisplayName

'If oBitMap.hasByName(oName) Then
'oBitmap.replaceByName( oName, oshape.GraphicURL )
'else
'oBitmap.insertByName( oName, oshape.GraphicURL )
'End If

'oNewURL = oBitmap.getByName( oName )
'oshape.GraphicURL = oNewURL 'Embeddet

end sub
Vermutlich hast Du das entsprechende Sheet vorher nicht gewählt?
Auf alle Fälle gibt es "com.sun.star.drawing.GraphicObjectShape" unter LO 3.4.3

Gruß R

Re: Images in Calc einfügen

Verfasst: Sa 22. Okt 2011, 13:33
von Marco
Danke. Habs nach einigem Rumprobieren doch noch hinbekommen. Es war das SpreadsheetDocument, für das ich mir per XMultiServiceFactory das GraphicObjectShape holen muss.

Hier der Sourcecode mit dem ich's hinbekommen hab:

Code: Alles auswählen

XSpreadsheetDocument spreadsheetDoc = ...

// create a new shape for the image 
var graphicObjectShape = ((XMultiServiceFactory)spreadsheetDoc).createInstance("com.sun.star.drawing.GraphicObjectShape");
XPropertySet propertySet = (XPropertySet)graphicObjectShape;
propertySet.setPropertyValue("GraphicURL", new uno.Any("file:///c:/myimage.jpg")));
XShape shape = (XShape)graphicObjectShape;
shape.setSize(new unoidl.com.sun.star.awt.Size(800, 600);

// ...and add it on the last page in the spreadsheet document
XDrawPagesSupplier drawPageSupplier = (XDrawPagesSupplier)spreadsheetDoc;
XDrawPages drawPages = drawPageSupplier.getDrawPages();
drawPages.insertNewByIndex(drawPages.getCount()).add(shape);