【发布时间】:2016-10-05 03:30:12
【问题描述】:
背景
我正在开发一个 Office 插件,使用 Word Javascript API 在文档中插入一些图表,然后用新数据重绘它们。我遇到了一个关于获取内联图片对象大小的问题
目前的做法如下:
创建指定大小的图像 => 包裹在内容控件中 => 将图像大小添加到内容控件标记 => 在文档中插入内容控件 => 刷新时获取 CC 标记重绘图像,该图像的大小来自该标记 =>
我这样做的原因是因为在获取 Word.InlinePicture 对象时,我似乎总是得到 width 和 height小于文档中嵌入图像的 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 设置不正确
【问题讨论】: