简体   繁体   English

使用C#和OpenXML 2.0将流中的图像添加到Word文档中

[英]add image from stream to a word document using c# and openxml 2.0

i've been trying something new to me, i need to insert images from a stream but everytime i try to insert an image it corrupts the word document, i am using office 2010. 我一直在尝试一些新手,我需要从流中插入图像,但是每次尝试插入图像都会损坏Word文档时,我正在使用Office 2010。

i have the following code adding the images to the word document 我有以下代码将图像添加到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));

    }

Thanks Quandary 感谢Quandary

but that didn't help as well, so i am saving the images to a folder and using the AddPicture Method in Interop 但这也没有帮助,所以我将图像保存到文件夹中并在Interop中使用AddPicture方法

Why don't you use the docx library ? 为什么不使用docx库?

3) DocX also supports 
     a) Pictures, 

http://docx.codeplex.com/ http://docx.codeplex.com/

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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