背景
我正在开发一个 Office 插件,使用 Word Javascript API 在文档中插入一些图表,然后用新数据重绘它们。我遇到了一个关于获取内联图片对象大小的问题
当前方法如下:
以指定大小创建图像 => 包装在内容控件中 => 将图像大小添加到内容控件标签 => 在文档中插入内容控件 => 刷新时获取 CC 标签重绘图像,该标签的大小 =>
我这样做的原因是因为在获取Word.InlinePicture对象时,我的宽度和高度似乎总是小于文档中嵌入图像的布局窗口中显示的值 - 发生了一些舍入但看到大约 100 像素的差异,大约 1 英寸,这是相当多的
理想情况下,我希望能够使用类似于此的代码从该对象获取大小:
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 设置不正确