我开发了一个应用程序,旨在将图像添加到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();
可能出了什么问题?