gpt4 book ai didi

c# - 将图像内容从 Rest API 插入到 Microsoft Word 中当前打开的文档

转载 作者:可可西里 更新时间:2023-11-01 09:15:59 24 4
gpt4 key购买 nike

编辑:这个问题的文本已经更改以反射(reflect)使用开放 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 markup cannot be inserted in the specified location.".

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

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

最佳答案

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

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

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

https://learn.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;
}
}

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

enter image description here

希望这对您有所帮助!

关于c# - 将图像内容从 Rest API 插入到 Microsoft Word 中当前打开的文档,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53782621/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com