C# 尝试将和图像嵌入Word文档时出现关系错误
我将PNG图像添加到word 2010文档中,如下所示:C# 尝试将和图像嵌入Word文档时出现关系错误,c#,openxml,openxml-sdk,wordml,wordprocessingml,C#,Openxml,Openxml Sdk,Wordml,Wordprocessingml,我将PNG图像添加到word 2010文档中,如下所示: var imagePart = report.MainDocumentPart.AddImagePart(ImagePartType.Png); var imagePath = Path.Combine(imageFolder, "1.png"); var stream = new FileStream(imagePath, FileMode.Open); imagePart.FeedData(stream); stream.Close(
var imagePart = report.MainDocumentPart.AddImagePart(ImagePartType.Png);
var imagePath = Path.Combine(imageFolder, "1.png");
var stream = new FileStream(imagePath, FileMode.Open);
imagePart.FeedData(stream);
stream.Close();
using(WordprocessingDocument report = WordprocessingDocument.Open(path, true)) {
//embed the image
}
我找到空图片内容控件的blip元素,并更改其reference属性以指向新图像:
var blip = image.Descendants<Blip>().Single();
blip.Embed = report.MainDocumentPart.GetIdOfPart(imagePart);
什么是关系?为什么不创建一个?如何修复此错误?当我在Word中打开生成的文档时,图像不会显示。您可以找到一个示例@我找到了一个解决方案。我不知道为什么,但我不得不
WordprocessingDocument report = WordprocessingDocument.Open(path, true)
使用这样的语句:
var imagePart = report.MainDocumentPart.AddImagePart(ImagePartType.Png);
var imagePath = Path.Combine(imageFolder, "1.png");
var stream = new FileStream(imagePath, FileMode.Open);
imagePart.FeedData(stream);
stream.Close();
using(WordprocessingDocument report = WordprocessingDocument.Open(path, true)) {
//embed the image
}
没有使用文档保存不正确:没有创建关系。这篇文章介绍了如何添加一个全新的图像,不是如何在现有图片内容控件中引用它对于图片内容控件,您可以阅读在打开的XML WordprocessingML文档中替换图片内容控件中的图片@我尝试了这个,但没有为我解决问题。还有其他人找到其他修复方法吗?