SDK C# how to add anchor to inserted image

I decided to start a new thread as the old one was getting too long to be useful, and this is kind of a new, but related, problem.

I’m writing a C# program using the LibreOffice SDK that inserts, scales, and centers images “in” spreadsheet cells. It’s working (!) but there are a few things I want to clean up. When I’m done I will post the program for others to use as a resource.

If I insert an image “into” a cell using the LibreOffice GUI, using “Insert->Image…”, it displays a small “Anchor” symbol in the upper left corner of the cell. However, if I insert an image with my program, it doesn’t show the “Anchor” symbol. After some exploring, I am starting to think that this difference is the tip of a very big iceberg.

I used MRI to look at the two different cells (the manually-inserted one and the programmatically-inserted one), along with the shapes containing the images, and what I think is the anchor (what I get if I click on the “Anchor” property in the shape). The cells and shapes are pretty much identical, but the anchors are very different, as if they’ve been created by entirely different things.

I don’t actually create anchors in my program; I think that what’s there gets generated when I create a new shape containing a new image. Maybe there is a way to specify the type of anchor to create, or to create the right kind of anchor explicitly, but endless searching in the documentation has not yielded anything.

I may not be able to do what I want, which is to have my program insert an image that is identical to one created with “Insert->Image…”. I realize I’m working outside the bounds of what the SDK can reasonably do, so maybe I should not worry about this. My program does insert images and they display properly in the spreadsheet (or at least appear to). They just don’t display the anchor symbol, and probably have other non-obvious differences that would cause trouble if one were to try to do something fancy.

So what I’m asking here is whether anyone knows about anchors and how to generate a proper one when I insert an image. If not, I’ll just have to not worry about it.

Thanks, as always…


The way set the anchor in to cell is simple:

image.Anchor = cell

but, show we your code.

Thank you for your reply!

Here is the code I use to add a shape containing an image to a DrawPage:

          // get a graphic provider
          // (used for creating graphic objects which contain images)
          object oGP = XMCF.createInstanceWithContext(
          XGraphicProvider XGP = (XGraphicProvider)oGP;

          // create graphic object containing image
          pPV = new PropertyValue[1];
          pPV[0] = new PropertyValue();
          pPV[0].Name = "URL";
          string pqURL = pqimp1+Im5.pqnam+".jpg";
          pPV[0].Value = new uno.Any(pqURL);
          XGraphic XG = XGP.queryGraphic(pPV);

          // create empty graphic shape object
          XShape XS = (XShape)XMSF2.createInstance(

          // add shape to the cell

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

The shape doesn’t have any association with the cell that it is “in”. It ends up looking like it is in the cell because I (later) set the shape’s position and size to fit it into the cell.

I’m not sure I understand “image.Anchor = cell”. Does that translate into?:

      // XS is the XShape
      // XC is the XCell
      // XPS is the shape's property set
      XPS.setPropertyValue("Anchor",new uno.Any(typeof(XCell),XC));

Anyhow, I put that line into my program, and now it works!!! So I guess I did understand what you suggested.

Many, many thanks!

[Note to anyone who finds this post by searching… I will soon post a complete sample program to insert/scale/center images in spreadsheet cells. Look for it as what’s posted here is just an excerpt.]