首页 > 解决方案 > 将来自 Rest API 的图像内容插入到 Microsoft Word 中当前打开的文档中

问题描述

编辑:此问题的文本已更改以反映利用开放 xml 代码和互操作。

我正在尝试通过功能区将 base 64 编码图像插入 Word 文档。以下代码用于复制目的:

   public partial class Ribbon1
    {
        private void Ribbon1_Load(object sender, RibbonUIEventArgs e)
        {
        }

        private void InsertPicture_Click(object sender, RibbonControlEventArgs e)
        {
            Word.Application wordApp = null;
            Word.Document currentDocument = null;
            Word.ContentControls controls = null;
            try
            {
                wordApp = (Word.Application) Marshal.GetActiveObject("Word.Application");
                currentDocument = wordApp.ActiveDocument;
                controls = currentDocument.ContentControls;
                
                currentDocument.Range().InsertXML(@"<pkg:package xmlns:pkg=""http://schemas.microsoft.com/office/2006/xmlPackage"">
  <pkg:part pkg:name=""/word/media/image1.png"" pkg:contentType=""image/png"" pkg:compression=""store"">
    <pkg:binaryData>iVBORw0KGgoAAAANSUhEUgAAABEAAAAKCAIA
      AADdHiL1AAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsMAAA7DAcdvqGQAAAAVSURBVChTY3gro0IqGtUz3PTIqAAAlO/H4+qBWxcAAAAASUVORK5CYII=</pkg:binaryData>
  </pkg:part></pkg:package>");
                object tr = true;
                object fa = false;
            }
            catch(Exception ex)
            {
                wordApp.ActiveDocument.Range().InsertAfter(ex.Message);
            }
            finally
            {
                if (controls != null) Marshal.ReleaseComObject(controls); controls = null;
                if (currentDocument != null) Marshal.ReleaseComObject(currentDocument); currentDocument = null;
                if (wordApp != null) Marshal.ReleaseComObject(wordApp); wordApp = null;
            }
        }
    }

但是,每当我执行此代码时,我都会遇到问题,错误是:

“无法在指定位置插入 XML 标记。”。

我知道这个错误具有误导性,因为如果我将 xml 更改为我在文档中<Test>Test</Text>看到的。"Test"任何人都可以对此有所了解吗?

请注意,使用的图像只是一个大约 10px x 10px 的红色正方形

标签: c#ms-wordopenxmloffice-interopadd-in

解决方案


您将需要图像在文件系统上可用,并且您需要使用对象Shapes.AddPicture方法ActiveDocument。您可以在调用此方法时设置图像位置及其大小。

currentDocument.Shapes.AddPicture (imagePath, Microsoft.Office.Core.MsoTriState.msoFalse, Microsoft.Office.Core.MsoTriState.msoCTrue, 10, 10, 250, 250);

有关详细信息,请参阅此 URL:

https://docs.microsoft.com/en-us/office/vba/api/word.shapes.addpicture

这是工作代码:

private void InsertPicture_Click(object sender, RibbonControlEventArgs e)
{
    Word.Application wordApp = null;
    Word.Document currentDocument = null;
    Word.ContentControls controls = null;
    try
    {
        wordApp = (Word.Application) Marshal.GetActiveObject ("Word.Application");
        currentDocument = wordApp.ActiveDocument;
        controls = currentDocument.ContentControls;
        string imagePath = @"D:\WordAddInTest\App_Data\Yay.jpg";
        currentDocument.Shapes.AddPicture (imagePath, Microsoft.Office.Core.MsoTriState.msoFalse, Microsoft.Office.Core.MsoTriState.msoCTrue, 10, 10, 250, 250);


        //              currentDocument.Range ().InsertXML (@"<pkg:package xmlns:pkg=""http://schemas.microsoft.com/office/2006/xmlPackage"">
        //<pkg:part pkg:name=""/word/media/image1.png"" pkg:contentType=""image/png"" pkg:compression=""store"">
        //  <pkg:binaryData>iVBORw0KGgoAAAANSUhEUgAAABEAAAAKCAIA
        //    AADdHiL1AAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsMAAA7DAcdvqGQAAAAVSURBVChTY3gro0IqGtUz3PTIqAAAlO/H4+qBWxcAAAAASUVORK5CYII=</pkg:binaryData>
        //</pkg:part></pkg:package>");
        object tr = true;
        object fa = false;
    }
    catch (Exception ex)
    {
        wordApp.ActiveDocument.Range ().InsertAfter (ex.Message);
    }
    finally
    {
        if (controls != null) Marshal.ReleaseComObject (controls); controls = null;
        if (currentDocument != null) Marshal.ReleaseComObject (currentDocument); currentDocument = null;
        if (wordApp != null) Marshal.ReleaseComObject (wordApp); wordApp = null;
    }
}

使用上述代码收到的输出:

在此处输入图像描述

希望这可以帮助!


推荐阅读