Word JS加载项-从文档中检索和处理“文本包装”图像

问题描述

我有一个文档,其中使用Word JS API通过加载项插入图像。该插件在此图像上添加了一个alt文本标签,以便以后可以找到,因为我无法使用control.track()方法对其进行跟踪。

然后,用户应将该图像作为占位符放置在文档中(文档随后被送入系统,该系统将使用此图像的位置作为参考点)。作为此过程的一部分,我希望用户希望使用Word提供的“布局选项”将图像放置在不与文本共线的位置:

Layout Options dialog

Image with a custom position and text wrapping

但是,这在尝试从文档中检索图像时会产生一些问题。如果图像与第一个图像一样保持内联,则可以从context.document.body.inlinePictures集合中获取图像,并确定添加的图像以及所有图像。但是,如果我使用“文本换行”布局选项来移动图像,该图像将不再出现在inlinePictures集合中,并且在寻找其他获取图像的方法之后,我将陷入困境。 >

这是我用来将图像插入文档中的代码:

let image = context.document.body.insertInlinePictureFromBase64(placeholder1B64,"End");
image.width = 200;
image.height = 60;
image.altTextTitle = "MyCustomTrackingAltTitle";
await context.sync();

当前,这是我必须检索和处理(在本例中为替换)占位符图像的代码:

let inlinePictures = context.document.body.inlinePictures;
context.load(inlinePictures);
context.sync().then(function () {
    for (let item of inlinePictures.items) {
        if (item.altTextTitle == "MyCustomTrackingAltTitle") {
            //this replaces the image placeholder with a different one
            item.insertInlinePictureFromBase64(placeholder2B64,"Replace")
            context.sync();
        }
    }
});

此代码对于内联图像效果很好,但会与换行符中断。

我还尝试从文档中获取所有作为“图片”的内容-当我在测试文档中为图片打开文本换行时,该图片似乎也仅返回嵌入式图片,该图片不再出现在数组中

let pictures = context.document.contentControls.getByTypes([Word.ContentControlType.picture])
context.load(pictures)
context.sync().then(function () {
    for (let pic of pictures.items) {
        //do the same checks and replacement here
    }
});

在这一点上,我认为我剩下的唯一选择是直接使用OOXML(如this post from a few years ago所示),但这是我一点都不熟悉的东西,因此我想看看是否Office JS API已更新,或者在我走这条路之前是否有替代方法。还有其他方法可以从Word文档中获取文本包装的图片并进行处理吗?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)