使用 open xml 和 C# 将图像插入到 word 文档中

发布于 2025-01-02 23:27:13 字数 3337 浏览 0 评论 0原文

我有以下代码尝试使用打开的 xml 将图像插入到 Word 文档中:

private void AddImageToBody(ImagePart imagePart)
{
    // Define the reference of the image.
    var element =
         new Drawing(
             new Inline(
                 new Extent() { Cx = ConvertToEmu(640), Cy = ConvertToEmu(480) },
                 new EffectExtent()
                 {
                     LeftEdge = 0L,
                     TopEdge = 0L,
                     RightEdge = 0L,
                     BottomEdge = 0L
                 },
                 new DocProperties()
                 {
                     Id = (UInt32Value)1U,
                     Name = "Picture 1"
                 },
                 new NonVisualGraphicFrameDrawingProperties(
                     new GraphicFrameLocks() { NoChangeAspect = true }
                 ),
                 new Graphic(
                     new GraphicData(
                         new Picture(
                             new NonVisualPictureProperties(
                                 new NonVisualDrawingProperties()
                                 {
                                     Id = (UInt32Value)0U,
                                     Name = "New Bitmap Image.jpg"
                                 },
                                 new NonVisualPictureDrawingProperties()),
                             new BlipFill(
                                 new Blip(
                                     new BlipExtensionList(
                                         new BlipExtension()
                                         {
                                             Uri = "{28A0092B-C50C-407E-A947-70E740481C1C}"
                                         }
                                     )
                                 )
                                 {
                                     Embed = mainpart.GetIdOfPart(imagePart),
                                     CompressionState = BlipCompressionValues.Print
                                 },
                                 new Stretch(
                                     new FillRectangle()
                                 )
                             ),
                             new ShapeProperties(
                                 new Transform2D(
                                     new Offset() { X = 0L, Y = 0L },
                                     new Extents() { Cx = ConvertToEmu(640), Cy = ConvertToEmu(480) }
                                 ),
                                 new PresetGeometry(
                                     new AdjustValueList()
                                 ) { Preset = ShapeTypeValues.Rectangle }
                             )
                         )
                     ) { Uri = "http://schemas.openxmlformats.org/drawingml/2006/picture" }
                 )
             )
             {
                 DistanceFromTop = (UInt32Value)0U,
                 DistanceFromBottom = (UInt32Value)0U,
                 DistanceFromLeft = (UInt32Value)0U,
                 DistanceFromRight = (UInt32Value)0U
             });

            // Append the reference to body, the element should be in a Run.
            body.AppendChild(new Paragraph(new Run(element)));
        }

虽然我正在插入图像,但插入的图像显示为空白,并且宽度和高度不正确。

有什么建议吗?

I have the following code trying to insert an image into a word document using open xml:

private void AddImageToBody(ImagePart imagePart)
{
    // Define the reference of the image.
    var element =
         new Drawing(
             new Inline(
                 new Extent() { Cx = ConvertToEmu(640), Cy = ConvertToEmu(480) },
                 new EffectExtent()
                 {
                     LeftEdge = 0L,
                     TopEdge = 0L,
                     RightEdge = 0L,
                     BottomEdge = 0L
                 },
                 new DocProperties()
                 {
                     Id = (UInt32Value)1U,
                     Name = "Picture 1"
                 },
                 new NonVisualGraphicFrameDrawingProperties(
                     new GraphicFrameLocks() { NoChangeAspect = true }
                 ),
                 new Graphic(
                     new GraphicData(
                         new Picture(
                             new NonVisualPictureProperties(
                                 new NonVisualDrawingProperties()
                                 {
                                     Id = (UInt32Value)0U,
                                     Name = "New Bitmap Image.jpg"
                                 },
                                 new NonVisualPictureDrawingProperties()),
                             new BlipFill(
                                 new Blip(
                                     new BlipExtensionList(
                                         new BlipExtension()
                                         {
                                             Uri = "{28A0092B-C50C-407E-A947-70E740481C1C}"
                                         }
                                     )
                                 )
                                 {
                                     Embed = mainpart.GetIdOfPart(imagePart),
                                     CompressionState = BlipCompressionValues.Print
                                 },
                                 new Stretch(
                                     new FillRectangle()
                                 )
                             ),
                             new ShapeProperties(
                                 new Transform2D(
                                     new Offset() { X = 0L, Y = 0L },
                                     new Extents() { Cx = ConvertToEmu(640), Cy = ConvertToEmu(480) }
                                 ),
                                 new PresetGeometry(
                                     new AdjustValueList()
                                 ) { Preset = ShapeTypeValues.Rectangle }
                             )
                         )
                     ) { Uri = "http://schemas.openxmlformats.org/drawingml/2006/picture" }
                 )
             )
             {
                 DistanceFromTop = (UInt32Value)0U,
                 DistanceFromBottom = (UInt32Value)0U,
                 DistanceFromLeft = (UInt32Value)0U,
                 DistanceFromRight = (UInt32Value)0U
             });

            // Append the reference to body, the element should be in a Run.
            body.AppendChild(new Paragraph(new Run(element)));
        }

Although I am inserting an image, the image being inserted appears as blank and not of the correct width and height.

Any suggestions?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

雪落纷纷 2025-01-09 23:27:13

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.

终止放荡 2025-01-09 23:27:13

我没有硬编码任何值,但我将它们传递给添加图像的方法,如下面的代码所示。

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

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;
            }
        }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文