2016-01-22 30 views
0

我有一个Word 2016加载项,它使用graphviz创建图像并将相应的点代码存储在图像的描述中。 我可以选择图像并将点代码加载回加载项中的编辑器。但是这只适用于我当前的inlinePictures实现。如何获取所选图像

 function getDataFromSelection() { 


      Word.run(function (context) { 

       var range = context.document.getSelection(); 
       var paragraphs = range.paragraphs; 
       context.load(paragraphs); 

       return context.sync().then(function() { 

        var pictures = paragraphs.items[0].inlinePictures; 
        context.load(pictures); 

        return context.sync().then(function() { 
         var picture = pictures.items[0]; 

         editor.getSession().getDocument().setValue(picture.altTextDescription); 
        }); 
       }); 

      }) 
     } 

如何获取所选图片,如果它是自由浮动在文档中?

添加的目标是使用点创建和编辑已创建的图。但编辑部分目前是问题。

回答

2

很好的问题。因为您正确地推断出inlinePictures集合只包含了,请原谅冗余,inlinePicture图片。浮动图像不包含在该集合中,我们将在未来的API迭代中添加。

有一种解决方法,我建议您执行以检查选择中是否存在浮动图像。

您可以从选择中获取并分析OOXML,并找出选择中的浮动图像。

这段代码演示了如何从选择获得OOXML:

// Run a batch operation against the Word object model. 
     Word.run(function (context) { 

    // Queue a command to get the current selection and then 
    // create a proxy range object with the results. 
     var range = context.document.getSelection(); 

    // Queue a commmand to get the OOXML of the current selection. 
     var ooxml = range.getOoxml(); 

    // Synchronize the document state by executing the queued-up commands, 
    // and return a promise to indicate task completion. 
     return context.sync().then(function() { 
     console.log('The OOXML read from the document was: ' + ooxml.value); 
     }); 
}).catch(function (error) { 
console.log('Error: ' + JSON.stringify(error)); 
if (error instanceof OfficeExtension.Error) { 
    console.log('Debug info: ' + JSON.stringify(error.debugInfo)); 
    } 
}); 

一旦你的OOXML其实你可以得到(与搜索字符串或XML编辑器)的范围内有什么,如果找类似的这个元素,你会知道那里有一个漂浮的图像。您可以看到替代文字和说明。如果需要,可以使用该字符串替换所需的替代文本属性,并可以写回OOXML以更新该浮动图像。它有点痛苦但可行,希望我们可以很快发布浮动图像集合!感谢和快乐编码!

<w:drawing> 
    <wp:anchor allowOverlap="1" layoutInCell="1" locked="0" behindDoc="0" relativeHeight="251659264" simplePos="1" distR="114300" distL="114300" distB="0" distT="0"> 
<wp:simplePos y="3710305" x="1729105"/> 
<wp:positionH relativeFrom="margin"> 
    <wp:posOffset>1729105</wp:posOffset> 
</wp:positionH> 
<wp:positionV relativeFrom="margin"> 
    <wp:posOffset>3710305</wp:posOffset> 
</wp:positionV> 
<wp:extent cx="2847975" cy="2133600"/> 
<wp:effectExtent r="9525" b="0" t="0" l="0"/> 
<wp:wrapSquare wrapText="bothSides"/> 
<wp:docPr title="alt text sample" name="Picture 1" descr="alt test description!!" id="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 name="stratus2.jpg" id="1"/> 
     <pic:cNvPicPr/> 
     </pic:nvPicPr> 
     <pic:blipFill> 
     <a:blip r:embed="rId4"> 
      <a:extLst> 
      <a:ext uri="{28A0092B-C50C-407E-A947-70E740481C1C}"> 
       <a14:useLocalDpi val="0" xmlns:a14="http://schemas.microsoft.com/office/drawing/2010/main"/> 
      </a:ext> 
      </a:extLst> 
     </a:blip> 
     <a:stretch> 
      <a:fillRect/> 
     </a:stretch> 
     </pic:blipFill> 
     <pic:spPr> 
     <a:xfrm> 
      <a:off y="0" x="0"/> 
      <a:ext cx="2847975" cy="2133600"/> 
     </a:xfrm> 
     <a:prstGeom prst="rect"> 
      <a:avLst/> 
     </a:prstGeom> 
     </pic:spPr> 
    </pic:pic> 
    </a:graphicData> 
    </a:graphic> 
    </wp:anchor> 
    </w:drawing> 
相关问题