【发布时间】:2020-12-11 00:15:53
【问题描述】:
我开发了一个应用程序,应该将图像添加到 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();
可能出了什么问题?
【问题讨论】:
-
这里的“link_of_image”是什么?它应该是有效的文件路径吗?图像是否存在?
-
@KerriBrown 该图像在计算机上不存在,它是一个上传到服务器的图像的链接作为示例(“something.com/Images/image.png”)
-
您还可以进行其他调查。例如,尝试存储在本地计算机上的图像,删除“wordFile.MainDocumentPart.AddExternalRelationship”,看看是否仍然出现错误等。