小编sam*_*leh的帖子

使用 OpenXML 将图像添加到 Word 文档

我开发了一个应用程序,该应用程序应该将图像添加到Word文档中。它从 Word 文档中获取副本并将图片添加到副本中。我尝试添加文本,效果很好,但是通过添加图像,word 文档想要打开一个文件,并给出错误(无法打开文件“名称”,因为内容存在问题。)

我的代码如下所示:

File.Copy(file, newFile, true);
     WordprocessingDocument wordFile = WordprocessingDocument.Open(newFile, true);
     Body body = wordFile.MainDocumentPart.Document.Body;

     var picture = new Picture();
     var shape = new Shape() { Style = "width: 20px; height: 20px" };
     var imageData = new ImageData() { RelationshipId = "img" };

     shape.Append(imageData);
     picture.Append(shape);
     wordFile.MainDocumentPart.AddExternalRelationship(
          "http://schemas.openxmlformats.org/officeDocument/2006/relationships/image",
           new Uri(link_of_image, UriKind.Absolute),"img");

     Paragraph para = body.AppendChild(new Paragraph());
     Run run = para.AppendChild(new Run());
     run.AppendChild(new Picture(picture));

     wordFile.Close();
Run Code Online (Sandbox Code Playgroud)

可能出什么问题了?

c# openxml

5
推荐指数
1
解决办法
9867
查看次数

标签 统计

c# ×1

openxml ×1