使用open xml和C#将图像插入到word文档中 [英] Insert an image into word document using open xml and C#

查看:251
本文介绍了使用open xml和C#将图像插入到word文档中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码尝试使用open xml将图像插入到word文档中:

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.

有什么建议吗?

推荐答案

http:/上尝试msdn如何:将图片插入Word处理文档 /msdn.microsoft.com/en-us/library/bb497430.aspx 。我可以使用此代码添加图像。

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.

这篇关于使用open xml和C#将图像插入到word文档中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆