简体   繁体   中英

C# OpenXML image in center

I'm trying to generate a DocX document with an image in the center of the document, but i have tried several things but nothing has worked out. The image is showing but in the top left corner. The function addImageToBody is from the MS website ( http://msdn.microsoft.com/en-us/library/office/bb497430(v=office.15).aspx ). I've tried to use the HorizontalPosition Class ( http://msdn.microsoft.com/en-us/library/documentformat.openxml.drawing.wordprocessing.horizontalposition(v=office.14).aspx ) but is has not worked for me.

add image and call function:

 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));

and function:

 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)));
    }

You need to center the paragraph that is holding the image. You can do it using paragraph properties (last line of your code):

wordDoc.MainDocumentPart.Document.Body.AppendChild(
    new DocumentFormat.OpenXml.Wordprocessing.Paragraph(new DocumentFormat.OpenXml.Wordprocessing.Run(element)) 
    { 
        ParagraphProperties = new DocumentFormat.OpenXml.Wordprocessing.ParagraphProperties() 
        { 
            Justification = new DocumentFormat.OpenXml.Wordprocessing.Justification() 
            { 
                Val = DocumentFormat.OpenXml.Wordprocessing.JustificationValues.Center 
            } 
        } 
    });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM