c# - 将来自 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 的红色正方形
解决方案
您将需要图像在文件系统上可用,并且您需要使用对象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;
}
}
使用上述代码收到的输出:
希望这可以帮助!
推荐阅读
- google-apps-script - 如何使用脚本编辑器在谷歌表格中搜索具有相同列值的单元格?
- java - 如何根据以经纬度为界的形状确定面积?
- python-3.x - 在 Python 中构造类时如何初始化和声明 *optional* 变量?
- swift - NumberFormatter() - 可能有特定数量的空格?
- sql - 为什么我的 SQL case 表达式返回错误的结果?
- android - elements.getBoundingBox 输出是什么意思?(FirebaseVision,安卓)
- python - 解决。我们可以让 Python 决定最好的初始猜测吗?
- ios - 如何指定 pod 文件应使用的特定 Firebase Analytics 版本
- sql - Oracle - 触发器和过程编译错误
- vector - 指向向量的 STL 向量 pf 指针是我的最佳选择吗?