Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/278.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#中心的OpenXML图像_C#_Wpf_Openxml_Docx - Fatal编程技术网

C#中心的OpenXML图像

C#中心的OpenXML图像,c#,wpf,openxml,docx,C#,Wpf,Openxml,Docx,我正在尝试生成一个DocX文档,文档中心有一个图像,但是我尝试了一些方法,但都没有成功。图像正在显示,但位于左上角。addImageToBody函数来自MS网站()。我尝试使用HorizontalPosition类(),但它对我不起作用 添加图像和调用功能: MainDocumentPart mainPart = document.MainDocumentPart; ImagePart imagePart = mainPart.AddImagePart(Ima

我正在尝试生成一个DocX文档,文档中心有一个图像,但是我尝试了一些方法,但都没有成功。图像正在显示,但位于左上角。addImageToBody函数来自MS网站()。我尝试使用HorizontalPosition类(),但它对我不起作用

添加图像和调用功能:

 MainDocumentPart mainPart = document.MainDocumentPart;

                ImagePart imagePart = mainPart.AddImagePart(ImagePartType.Png);


                using (FileStream stream = new FileStream(@"C:...\Logo.png", FileMode.Open))
                {
                    imagePart.FeedData(stream);

                }

                AddImageToBody(document, mainPart.GetIdOfPart(imagePart));
 private static void AddImageToBody(WordprocessingDocument wordDoc, string relationshipId)
    {
        int defX = 854;
        int defY = 350;
        int size = 3000;
        // Define the reference of the image.
        var element =
             new DocumentFormat.OpenXml.Wordprocessing.Drawing(
                 new DW.Inline(
                     new DW.Extent() { Cx = defX * size, Cy = defY * size },
                     new DW.EffectExtent()
                     {
                         LeftEdge = 0L,
                         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 = "New Bitmap Image.jpg"
                                     },
                                     new PIC.NonVisualPictureDrawingProperties()),
                                 new PIC.BlipFill(
                                     new A.Blip(
                                         new A.BlipExtensionList(
                                             new A.BlipExtension()
                                             {
                                                 Uri =
                                                   "{28A0092B-C50C-407E-A947-70E740481C1C}"
                                             })
                                     )
                                     {
                                         Embed = relationshipId,
                                         CompressionState =
                                         A.BlipCompressionValues.Print
                                     },
                                     new A.Stretch(

                                         new A.FillRectangle() { })),
                                 new PIC.ShapeProperties(
                                     new A.Transform2D(
                                         new A.Offset() { X = 1000L, Y = 500L, },
                                         new A.Extents() { Cx = defX * size, Cy = defY * size }),
                                     new A.PresetGeometry(
                                         new A.AdjustValueList()
                                     ) { Preset = A.ShapeTypeValues.Rectangle }))
                         ) { Uri = "http://schemas.openxmlformats.org/drawingml/2006/picture" })
                 )
                 {
                     DistanceFromTop = (UInt32Value)999999,
                     DistanceFromBottom = (UInt32Value)10000,
                     DistanceFromLeft = (UInt32Value)10000,
                     DistanceFromRight = (UInt32Value)10000,
                     EditId = "50D07946"
                 });

        // Append the reference to body, the element should be in a Run.
        wordDoc.MainDocumentPart.Document.Body.AppendChild(new DocumentFormat.OpenXml.Wordprocessing.Paragraph(new DocumentFormat.OpenXml.Wordprocessing.Run(element)));
    }
和功能:

 MainDocumentPart mainPart = document.MainDocumentPart;

                ImagePart imagePart = mainPart.AddImagePart(ImagePartType.Png);


                using (FileStream stream = new FileStream(@"C:...\Logo.png", FileMode.Open))
                {
                    imagePart.FeedData(stream);

                }

                AddImageToBody(document, mainPart.GetIdOfPart(imagePart));
 private static void AddImageToBody(WordprocessingDocument wordDoc, string relationshipId)
    {
        int defX = 854;
        int defY = 350;
        int size = 3000;
        // Define the reference of the image.
        var element =
             new DocumentFormat.OpenXml.Wordprocessing.Drawing(
                 new DW.Inline(
                     new DW.Extent() { Cx = defX * size, Cy = defY * size },
                     new DW.EffectExtent()
                     {
                         LeftEdge = 0L,
                         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 = "New Bitmap Image.jpg"
                                     },
                                     new PIC.NonVisualPictureDrawingProperties()),
                                 new PIC.BlipFill(
                                     new A.Blip(
                                         new A.BlipExtensionList(
                                             new A.BlipExtension()
                                             {
                                                 Uri =
                                                   "{28A0092B-C50C-407E-A947-70E740481C1C}"
                                             })
                                     )
                                     {
                                         Embed = relationshipId,
                                         CompressionState =
                                         A.BlipCompressionValues.Print
                                     },
                                     new A.Stretch(

                                         new A.FillRectangle() { })),
                                 new PIC.ShapeProperties(
                                     new A.Transform2D(
                                         new A.Offset() { X = 1000L, Y = 500L, },
                                         new A.Extents() { Cx = defX * size, Cy = defY * size }),
                                     new A.PresetGeometry(
                                         new A.AdjustValueList()
                                     ) { Preset = A.ShapeTypeValues.Rectangle }))
                         ) { Uri = "http://schemas.openxmlformats.org/drawingml/2006/picture" })
                 )
                 {
                     DistanceFromTop = (UInt32Value)999999,
                     DistanceFromBottom = (UInt32Value)10000,
                     DistanceFromLeft = (UInt32Value)10000,
                     DistanceFromRight = (UInt32Value)10000,
                     EditId = "50D07946"
                 });

        // Append the reference to body, the element should be in a Run.
        wordDoc.MainDocumentPart.Document.Body.AppendChild(new DocumentFormat.OpenXml.Wordprocessing.Paragraph(new DocumentFormat.OpenXml.Wordprocessing.Run(element)));
    }

您需要将保存图像的段落居中。您可以使用段落属性(代码的最后一行)执行此操作:


您需要将保存图像的段落居中。您可以使用段落属性(代码的最后一行)执行此操作:


谢谢你太棒了!谢谢你太棒了!