【问题标题】:Open xml SDK. The picture can't be displayed when I insert it打开 xml SDK。插入时图片无法显示
【发布时间】:2019-02-18 19:28:54
【问题描述】:

我刚开始使用 Open Xml SDK,但遇到以下问题。我正在尝试使用 Open xml SDK 将文本替换为 word 文件中的图像。 图像在所需位置,但我得到“图片无法显示”的图标。如果我将图像添加到文档的末尾,一切正常。这是为什么呢?

using (WordprocessingDocument wordDoc1 = WordprocessingDocument.Open(link, true))
{
    Text textPlaceHolder = wordDoc1.MainDocumentPart.Document.Body.Descendants<Text>().Where((x) => x.Text == "(Imageplaceholder)").FirstOrDefault();
    if (textPlaceHolder == null)
    {
        Console.WriteLine("Text holder not found!");
    }
    else
    {
        var parent = textPlaceHolder.Parent;

        if (!(parent is Run))  // Parent should be a run element.
        {
            Console.Out.WriteLine("Parent is not run");
        }
        else
        {
            var element =
        new DocumentFormat.OpenXml.Wordprocessing.Drawing(
            new DW.Inline(
                new DW.Extent() { Cx = 480000L, Cy = 792000L },
                new DW.EffectExtent()
                {
                    LeftEdge = 980000L,
                    TopEdge = 0L,
                    RightEdge = 0L,
                    BottomEdge = 0L
                },
                new DW.DocProperties()
                {
                    Id = (UInt32Value)1U,
                    Name = "Picture 1"
                },
                new DW.NonVisualGraphicFrameDrawingProperties(
                    new A.GraphicFrameLocks() { NoChangeAspect = true }),
                new A.Graphic(
                    new A.GraphicData(
                        new PIC.Picture(
                            new PIC.NonVisualPictureProperties(
                                new PIC.NonVisualDrawingProperties()
                                {
                                    Id = (UInt32Value)0U,
                                    Name = "Test.jpg"
                                },
                                new PIC.NonVisualPictureDrawingProperties()),
                            new PIC.BlipFill(new A.Blip(
                                    new A.BlipExtensionList(
                                        new A.BlipExtension()
                                        {
                                            Uri =
                                            "{28A0092B-C50C-407E-A947-70E740481C1C}"
                                        })
                                )
                                {
                                    Embed = "C:\\Users\\Me\\Desktop\\Test.jpg",
                                    CompressionState =
                                  A.BlipCompressionValues.Print
                                },
                                new A.Stretch(
                                    new A.FillRectangle())),
                            new PIC.ShapeProperties(
                                new A.Transform2D(
                                    new A.Offset() { X = 0L, Y = 0L },
                                    new A.Extents() { Cx = 990000L, Cy = 792000L }),
                                new A.PresetGeometry(
                                    new A.AdjustValueList()
                                )
                                { Preset = A.ShapeTypeValues.Rectangle }))
                    )
                    { Uri = "http://schemas.openxmlformats.org/drawingml/2006/picture" })
            )
            {
                DistanceFromTop = (UInt32Value)0U,
                DistanceFromBottom = (UInt32Value)0U,
                DistanceFromLeft = (UInt32Value)0U,
                DistanceFromRight = (UInt32Value)0U,
                EditId = "50D07946"
            });

            // Insert image (the image created with your function) after text place holder.        
            textPlaceHolder.Parent.InsertAfter<DocumentFormat.OpenXml.Wordprocessing.Drawing>(element, textPlaceHolder);
            // Remove text place holder.
            textPlaceHolder.Remove();
            wordDoc1.Close();
        }
    }  
}

【问题讨论】:

    标签: c# openxml-sdk


    【解决方案1】:

    您已经在 Embed 属性中放置了一个路径,但它应该是图像部件的 ID。您将在复制其余部分的同一 MSDN URL 中找到如何创建此部分。

    【讨论】:

    • 鼓励链接到外部资源,但请在链接周围添加上下文,以便您的其他用户了解它是什么以及为什么存在。始终引用重要链接中最相关的部分,以防目标站点无法访问或永久离线。见how to answer
    【解决方案2】:

    将此添加到 using 块的顶部(在 { } 内):

    using (WordprocessingDocument wordDoc1 = WordprocessingDocument.Open(link, true))
    {
        MainDocumentPart mainPart = wordprocessingDocument.MainDocumentPart;
    
        ImagePart imagePart = mainPart.AddImagePart(ImagePartType.Jpeg);
    
        using (FileStream stream = new FileStream(fileName, FileMode.Open))
        {
            imagePart.FeedData(stream);
        }
        var relationshipId = mainPart.GetIdOfPart(imagePart);
    
        # And then continue with the code you had:
        Text textPlaceHolder = wordDoc1.MainDocumentPart.Document.Body.Descendants<Text>().Where((x) => x.Text == "(Imageplaceholder)").FirstOrDefault();
        # etc
    
    

    然后替换你的行

    Embed = "C:\\Users\\Me\\Desktop\\Test.jpg",
    

    Embed = relationshipId,
    

    供将来参考:

    对我来说,问题是我在处理 WordprocessingDocument 对象之前将文档写入文件。

    在将文档流写入文件之前,将 WordprocessingDocument 包装在 using 块中(就像 OP 一样)或调用 wordprocessingDocument.Dispose()

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-07-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-03-26
      • 2019-08-14
      • 2021-02-17
      • 2013-10-02
      相关资源
      最近更新 更多