【问题标题】:Relationship error when trying to embed and image into a Word document尝试将图像嵌入 Word 文档时出现关系错误
【发布时间】:2012-06-26 08:48:22
【问题描述】:

我将 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();

我找到一个空的图片内容控件的 blip 元素并将其引用属性更改为指向新图像:

var blip = image.Descendants<Blip>().Single();
blip.Embed = report.MainDocumentPart.GetIdOfPart(imagePart);

我保存生成的文档,并使用 Open XML Productivity Tool 对其进行验证。我收到此错误:

The relationship 'Ra4d8ccdc5256bb1' referenced by attribute 'http://schemas.openxmlformats.org/officeDocument/2006/relationships:embed' does not exist.

什么是关系?为什么AddImagePart 不创建一个?我该如何解决这个错误?当我在 Word 中打开生成的文档时,图像不显示。

【问题讨论】:

    标签: c# openxml openxml-sdk wordml wordprocessingml


    【解决方案1】:

    我找到了解决方案。我不知道为什么,但我必须附上

    WordprocessingDocument report = WordprocessingDocument.Open(path, true)
    

    使用这样的using 声明:

    using(WordprocessingDocument report = WordprocessingDocument.Open(path, true)) {
        //embed the image
    }
    

    withot using 文档未正确保存:未创建关系。

    【讨论】:

    • 我试过这个,但它并没有解决我的问题。其他人找到任何其他修复了吗?
    【解决方案2】:

    【讨论】:

    • 这篇文章解释了如何添加一个全新的图像,而不是如何在现有的图片内容控件中引用它
    • 对于图片内容控件,您可以阅读在 Open XML WordprocessingML 文档中替换图片内容控件中的图片@ericwhite.com/blog/2011/03/27/…
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-13
    • 1970-01-01
    • 2011-02-02
    • 1970-01-01
    • 2015-10-08
    相关资源
    最近更新 更多