Search code examples
office-jsofficedev

Getting Word.InlinePicture Height and Width


Background
I am developing an Office add in using the Word Javascript APIs to insert some charts in a document and later on redraw them with fresh data. I am hitting an issue around getting the size of the inline picture object

Current approach is as follows:
Create Image at specified size => Wrap inside a content control => Add image size to content control tag => Insert Content Control in document => When refreshing get CC tag redraw image with size from that tag =>

The reason I've done it like this is because when getting the Word.InlinePicture object I always seem to get width and height smaller than the values shown in the Layout window of the image emebbed in the document - there is some rounding up happening but seeing a difference of ~ 100 pixels which is about an inch which is quite a lot

Ideally I would like to be able to get the size from that object using code similar to this:

return Word.run((context: Word.RequestContext): Promise < void > => {

      var contentControls = context.document.contentControls;
      context.load(contentControls, 'tag');

      return context.sync().then(() => {

          let contentControlToRefresh: Word.ContentControl;

          // Find the content control containing imageId
          for (var contentControl of contentControls.items) {
            if (Utils.contains(contentControl.tag, imageId)) {

              // Add control to tracked objects
              context.trackedObjects.add(contentControl);
              contentControlToRefresh = contentControl;
            }
          }

          // Get the image from the content control -- need to get the size
          var pics = contentControlToRefresh.inlinePictures;
          context.load(pics);

          return context.sync().then(() => {
            let pic: Word.InlinePicture = pics.items[0];
            context.trackedObjects.add(pics.items[0]);

            // Set size 					
            let size: Dimensions = {
              height: pic.height,
              width: pic.width
            }; // These two values pic.height and pic.width are slightly smaller than original image

          })
        }
      });

Question
Word.InlinePicture values lower than actual layout values
Am I missing something obvious or is this an actual bug in the Word API ?

Some Notes

  • Absolute sizes on all my images are equal to their Original Sizes and Images are generated successfully at the specific resolution

  • Dragging and Dropping images in content controls doesn't play nice with Office js - _inlinePictureID not set correctly


Solution

  • Thanks Todor. I think your main issue is not getting the image dimensions, but more about the units used for width and height.

    Something to keep in mind when working with inlinePictures in Word (and btw this is true for VBA, VSTO and Office.js ) is that widths and heights units are expressed in points (which is different of the units shown in the UI, in the us-en locale is inches). This is true when setting and getting the values.

    FYI

    • 1 inch = 71.942 points.
    • points = pixels * 72 / 96

    Hope this helps.