C# SDK: trying to write a program to insert an image into a cell

Please post the code

Sorry. It’s getting pretty long so I’m just posting the part that doesn’t work:

    // set image XGraphic
    XPropertySet XPS = (XPropertySet)XS;
    XPS.setPropertyValue("Graphic",XG);
    XPS.setPropertyValue("Name",new uno.Any(pqpic));

It’s the line that is trying to set the “Graphic” property that fails. It gives a compiler error “addpic.cs(145,36): error CS1503: Argument 2: cannot convert from ‘unoidl.com.sun.star.graphic.XGraphic’ to ‘uno.Any’”. I also tried using “new uno.Any(XG)” and it gives a similar error.

This is a very simple question: how do I pass an object of type XGraphic in an argument that requires a uno.Any type.

I’ve tried everything I could think of. There must be something similar to uno.Any() that accepts an XGraphic object or something that an XGraphic object can be cast into. The specification for that property says its value should be an XGraphic, so there must be a way to pass an XGraphic as a uno.Any.

Thanks!

I also asked my question on stackoverflow.com (per a suggestion made earlier) and someone there gave me the answer. The second argument of the call to setPropertyValue (“Graphic”,uno.Any type) needs to include another argument to uno.Any() specifying the type of XGraphic as follows: “new uno.Any(typeof(XGraphic),XG)”. That’s how to get an argument of type uno.Any that specifies an XGraphic instance.

After making that change, my program compiled, ran, and inserted an image! I am hopeful that this is the last roadblock in getting my program working, and I look forward to posting a complete, running sample program soon.

Thanks to everyone who helped me! Every suggestion helped in some way, usually by pointing me in a new direction that led me to a solution to one of my many difficulties.

Well it’s five months later and I’m still plugging away at this in my spare time. Yes it’s turning out to be possible to do, and I’m very close to having a working program. I’m now stuck on cleaning up the details of getting the sizes and positions associated with the new shapes I’m adding to be correct.

[problem solved - see next post…]

Where I’m stuck now is something that is probably simple: finding out the size and position of a cell with nothing in it. These values are undefined, and putting text in the cell doesn’t seem to define them. I’m referring to these values:

 // get cell size and position
      XC = (XCell)XCR.getCellByPosition(Im6.ncol,Im6.nrow);
      XTR = (XTextRange)XC;
      Im6.Szc = (unoidl.com.sun.star.awt.Size)
                       ((XPropertySet)XC).getPropertyValue("Size").Value;
      Im6.Ptc = (unoidl.com.sun.star.awt.Point)
                   ((XPropertySet)XC).getPropertyValue("Position").Value;

For cells which have images inserted manually using LibreOffice (“Insert->Image…”) the values are correct. Cells which don’t have anything have Size and Position values which are garbage like (-1073710966,-1073551852). Note that I’m trying to get the values which are properties of the XCell, not of the XShape (there isn’t one) or of an Anchor (ditto).

Where I’m going with this is that I want to set these values in the graphic image shapes (XShape) I am inserting in the DrawPage, and probably also in Anchors I need to create and add, I think to the image’s XShape. I’m getting there.

As I’ve mentioned before, what I’m going to end up with is a working sample C# program that inserts images into cells the same way as ("Insert->Image…) does, and which scales and positions all images so they fit and are centered in the associated cells (something you can’t do directly with LibreOffice). The sample program will be well commented to make it easy for others to pull out what they need to do similar things.

As always, thanks!

-jimc

I found that one. A really dumb mistake. The casts in the last two statements were being applied to the wrong thing. I needed another set of parentheses around everything after the casts. Like I said, really dumb.

Sorry for the bother. I’ll update again soon…

-jimc

It’s working!!!

Lots of cleanup and probably a few minor bugs, but with C# when a program stops producing garbage output it’s usually pretty much done.

Thanks to everyone for all the help, and I hope my work proves useful to others.

-jimc