Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/274.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 打开XMLSDK。插入图片时无法显示它_C#_Openxml Sdk - Fatal编程技术网

C# 打开XMLSDK。插入图片时无法显示它

C# 打开XMLSDK。插入图片时无法显示它,c#,openxml-sdk,C#,Openxml Sdk,我刚开始使用OpenXMLSDK,我有以下问题。我正在尝试使用OpenXMLSDK将word文件中的文本替换为图像。 图像位于所需位置,但我得到了图标,图片无法显示。如果我将图像添加到文档末尾,一切正常。为什么呢 using (WordprocessingDocument wordDoc1 = WordprocessingDocument.Open(link, true)) { Text textPlaceHolder = wordDoc1.MainDocumentPart.Docume

我刚开始使用OpenXMLSDK,我有以下问题。我正在尝试使用OpenXMLSDK将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();
        }
    }  
}

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

将此部分添加到{}中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

供日后参考:

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


可以像OP一样将WordprocessingDocument包装在using块中,也可以调用WordprocessingDocument.Dispose,然后再将文档流写入文件。

鼓励使用指向外部资源的链接,但请在链接周围添加上下文,以便您的其他用户了解它是什么以及为什么存在。始终引用重要链接中最相关的部分,以防无法访问目标站点或永久脱机。看见
Embed = "C:\\Users\\Me\\Desktop\\Test.jpg",
Embed = relationshipId,