我正在尝试在Word文档中收集图像。此页面的文档:https://dev.office.com/reference/add-ins/word/inlinepicture从字面上看是示例的剪切粘贴,而实际上并未显示如何获取图像-仅显示第一个。
每张图像我需要以下物品:
getBase64ImageSrc
方法-
会做到的。image_{n}
其中{n}是图像索引来构建它,但是我看不到一种获取扩展名的方法-是在数据中以data:image/jpeg;blahblah
的形式出现吗????我不知道文档
没有此类信息。到目前为止,我具有以下代码,但实际上不确定是否会起作用:
Word.run(
async (context) =>
{
// Create a proxy object for the pictures.
const allPictures = context.document.body.inlinePictures;
// Queue a command to load the pictures
context.load(allPictures);
// Synchronize the document state by executing the queued commands,
// and return a promise to indicate task completion.
return context.sync().then(() => allPictures);
})
.then((allPictures) =>
{
const images: IFileData[] = [];
let picture: Word.InlinePicture | undefined;
let imageCount = 0;
while (undefined !== (picture = allPictures.items.pop()))
{
const data = picture.getBase64ImageSrc();
const extension = ""; // TODO: no idea how to find this
const filename =
(
Strings.isNullOrEmpty(picture.altTextTitle)
? `image_${imageCount++}`
: Path.toFriendlyUrl(picture.altTextTitle)
)
images.push({
filename: filename + extension,
data: data
});
}
resolve(images);
})
.catch((e) => reject(e));
我在这里使用一些自定义帮助程序,他们执行以下操作:
-
和其他一些改进我当前的方法正确吗?
答案 0 :(得分:1)
请签出您正在执行的t his sample。我认为您的做法正确。
以下是一些示例代码:
async function run() {
await Word.run(async (context) => {
let myImages = context.document.body.inlinePictures;
myImages.load("imageFormat");
await context.sync();
if (myImages.items.length >0)
console.log(myImages.items[0].imageFormat);
else
console.log("no image found.")
});
}
请注意,我们具有imageFormat属性,问题是我们将其放入预览CDN中。 (使用https://appsforoffice.microsoft.com/lib/beta/hosted/office.js)。我们没有图片名称,但是您可以使用替代文字来存储它。
答案 1 :(得分:0)
“正确”是有效的...我可以解决一个特定的问题:获取图像类型-您所说的“文件名”。由于时间有点长,答案是:可以,但是您必须为此付出一点努力。
Word并不总是这样为文档中的图像存储文件名,除非该图像链接到外部源。但是,它存储的是图像本身以及在Word Open XML文档中进行管理所需的信息。存储的信息的一部分是图形图像 type ,它是文档和图像的二进制代码之间的内部关系的一部分。
对象模型(无论是JS还是COM)都不提供对该信息的任何直接访问。但是,可以从文档的Word Open XML中读取它。此代码可以以OPC平面文件格式获取InlineShape的特定Word Open XML字符串:
"styles": [
"../node_modules/materialize-css/dist/css/materialize.css",
"../node_modules/font-awesome/css/font-awesome.min.css",
"styles.css"
],
"scripts": [
"../node_modules/jquery/dist/jquery.js",
"../node_modules/jqueryui/jquery-ui.js",
"../node_modules/materialize-css/dist/js/materialize.js",
"./assets/RTCMultiConnection.min.js",
"../node_modules/hammerjs/hammer.js",
"./assets/popup.js"
],
在Open XML的document.xml部分中,按如下方式(部分)引用了InlineShape-请参阅具有属性 const range = context.document.body.inlinePictures.getFirst();
var sXML = range.getRange("Whole").getOoxml();
range.load("Ooxml");
await context.sync();
console.log(sXML.value);
的最后一个元素。
r:embed="rId6"
<w:p><w:r><w:drawing><wp:inline distT="0" distB="0" distL="0"
distR="0"><wp:extent cx="2944608" cy="1753392"/><wp:effectExtent l="0"
t="0" r="8255" b="0"/><wp:docPr id="1" name="Picture 1"/>
<wp:cNvGraphicFramePr><a:graphicFrameLocks noChangeAspect="1"
xmlns:a="http://schemas.openxmlformats.org/drawingml/2006/main"/>
</wp:cNvGraphicFramePr><a:graphic xmlns:a="http://schemas.openxmlformats.org/drawingml/2006/main">
<a:graphicData uri="http://schemas.openxmlformats.org/drawingml/2006/picture">
<pic:pic xmlns:pic="http://schemas.openxmlformats.org/drawingml/2006/picture">
<pic:nvPicPr><pic:cNvPr id="0" name="Schweiz.png"/><pic:cNvPicPr/></pic:nvPicPr>
<pic:blipFill><a:blip r:embed="rId6">...
是关系ID-它告诉Word在哪里查找有关嵌入式图像的详细信息。可以在rId6
中找到,就像这样:
<pkg:part pkg:name="/word/_rels/document.xml.rels"
如您所见,文件类型在此处可用。如果使用标准XML工具来解析XML字符串,则可以获得类似的信息。
使用标准XML技术的替代方法是使用标准Microsoft Open XML SDK(C#或VB.NET)或使用针对JavaScript的Open XML SDK(http://www.ericwhite.com/blog/open-xml-sdk-for-javascript/)来分析Word Open XML。在这种情况下,您将无法直接读取“相关”。而是,“工具”查找相应的“包”(在本例中为“ media / image1.png”)并返回该信息。如您所见,其中包括属性pkg:contentType,该属性为您提供文件扩展名。
<Relationship Id="rId6"
Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/image"
Target="media/image1.png"/>