【问题标题】:Getting Word.InlinePicture Height and Width获取 Word.InlinePicture 的高度和宽度
【发布时间】:2016-10-05 03:30:12
【问题描述】:

背景
我正在开发一个 Office 插件,使用 Word Javascript API 在文档中插入一些图表,然后用新数据重绘它们。我遇到了一个关于获取内联图片对象大小的问题

目前的做法如下:
创建指定大小的图像 => 包裹在内容控件中 => 将图像大小添加到内容控件标记 => 在文档中插入内容控件 => 刷新时获取 CC 标记重绘图像,该图像的大小来自该标记 =>

我这样做的原因是因为在获取 Word.InlinePicture 对象时,我似乎总是得到 widthheight小于文档中嵌入图像的 Layout 窗口中显示的值 - 发生了一些四舍五入,但看到的差异约为 100 像素,大约为 1 英寸,这是相当多的 p>

理想情况下,我希望能够使用类似以下的代码从该对象获取大小:

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

          })
        }
      });

问题
Word.InlinePicture 值低于实际布局值
我是否遗漏了一些明显的东西,或者这是 Word API 中的实际错误?

一些注意事项

  • 我所有图像的绝对尺寸都等于它们的原始尺寸,并且 以特定分辨率成功生成图像

  • 在内容控件中拖放图像效果不佳 使用 Office js - _inlinePictureID 设置不正确

【问题讨论】:

    标签: office-js officedev


    【解决方案1】:

    谢谢托多。我认为您的主要问题不是获取图像尺寸,而是更多关于用于宽度和高度的单位。

    在 Word 中使用 inlinePictures 时要记住的一点是(顺便说一句,这对于 VBA、VSTO 和 Office.js 也是如此)是宽度和高度单位以点表示(这与 UI 中显示的单位不同) , 在 us-en 语言环境中是英寸)。设置和获取值时确实如此。

    仅供参考

    • 1 英寸 = 71.942 点。
    • 点 = 像素 * 72 / 96

    希望这会有所帮助。

    【讨论】:

    • 感觉我错过了一些非常明显的东西,非常感谢! - 当我将点转换为像素时效果很好
    猜你喜欢
    • 2017-12-26
    • 1970-01-01
    • 2012-10-10
    • 2021-06-30
    • 1970-01-01
    • 2010-11-18
    • 2023-04-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多