【问题标题】:add image from stream to a word document using c# and openxml 2.0使用 c# 和 openxml 2.0 将图像从流添加到 word 文档
【发布时间】:2011-11-29 09:48:00
【问题描述】:

我一直在尝试一些新的东西,我需要从流中插入图像,但每次我尝试插入图像时都会破坏 word 文档,我使用的是 office 2010。

我有以下代码将图像添加到 word 文档中

private void AddImageToBody(WordprocessingDocument wordDoc, string relationshipId, string documentName)
    {
        var element = new Drawing(new DW.Inline(new DW.Extent { Cx = 990000L, Cy = 792000L }, 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 = documentName
            }, new PIC.NonVisualPictureDrawingProperties()), new PIC.BlipFill(new A.Blip
            {
                Embed = relationshipId,
                CompressionState =
                A.BlipCompressionValues.Print
            }, new A.Stretch(new A.FillRectangle())), new PIC.ShapeProperties(new A.Transform2D(new A.Offset { X = 0L, Y = 0L }, new A.Extents { Cx = 990000L, Cy = 792000L }), new A.PresetGeometry(
                new A.AdjustValueList()) { Preset = A.ShapeTypeValues.Rectangle })))))
                {
                    DistanceFromTop = (UInt32Value)0U,
                    DistanceFromBottom = (UInt32Value)0U,
                    DistanceFromLeft = (UInt32Value)0U,
                    DistanceFromRight = (UInt32Value)0U
                });
        wordDoc.MainDocumentPart.Document.Body.AppendChild(new Run(element));

    }

感谢困惑

但这也无济于事,所以我将图像保存到一个文件夹并使用 Interop 中的 AddPicture 方法

【问题讨论】:

    标签: c# asp.net openxml


    【解决方案1】:

    为什么不使用 docx 库?

    3) DocX also supports 
         a) Pictures, 
    

    http://docx.codeplex.com/

    像这样: http://cathalscorner.blogspot.com/2009/04/docx-version-1002-released.html

    【讨论】:

    • 没听说过,我试试看
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-22
    • 2012-07-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多