Insert an image into word document using open xml and C#

╄→尐↘猪︶ㄣ 提交于 2019-12-05 22:33:00

Try msdn "How to: Insert a Picture into a Word Processing Document" at http://msdn.microsoft.com/en-us/library/bb497430.aspx. I'm able to add image using this code.

I don't hard code any value but I pass them as to the method adding the image as shown in the code below.

private static void AddImageToRun(Run run, string imageFileName, long imageWidthEMU, long imageHeightEMU, string imageID)
        {
            try
            {
                string GraphicDataUri =http://schemas.openxmlformats.org/drawingml/2006/picture";

                var element =
                     new Drawing(
                         new DW.Inline(
                             new DW.Extent() { Cx = imageWidthEMU, Cy = imageHeightEMU },

                             new DW.EffectExtent()
                             {
                                 LeftEdge = 0L,
                                 TopEdge = 0L,
                                 RightEdge = 0L,
                                 BottomEdge = 0L
                             },
                             new DW.DocProperties()
                             {
                                 Id = (UInt32Value)1U,
                                 Name = imageFileName,
                                 Description = imageFileName
                             },
                             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 = imageFileName
                                             },
                                             new PIC.NonVisualPictureDrawingProperties()),

                                         new PIC.BlipFill(
                                             new A.Blip()
                                             {
                                                 Embed = imageID
                                             },
                                             new A.Stretch(
                                                 new A.FillRectangle())),
                                         new PIC.ShapeProperties(
                                             new A.Transform2D(
                                                 new A.Offset() { X = 0L, Y = 0L },

                                                 new A.Extents()
                                                 {
                                                     Cx = imageWidthEMU,
                                                     Cy = imageHeightEMU
                                                 }),

                                             new A.PresetGeometry(
                                                 new A.AdjustValueList()
                                             ) { Preset = A.ShapeTypeValues.Rectangle }))
                                 ) { Uri = GraphicDataUri })
                         )
                         {
                             DistanceFromTop = (UInt32Value)0U,
                             DistanceFromBottom = (UInt32Value)0U,
                             DistanceFromLeft = (UInt32Value)0U,
                             DistanceFromRight = (UInt32Value)0U,
                         });

                // Append the reference to body, the element should be in a Run.
                run.AppendChild(element);
            }
            catch (Exception ex)
            {
                throw;
            }
        }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!