使用c#和openxml 2.0将图像从流添加到word文档

发布于 2024-12-18 18:49:12 字数 1855 浏览 1 评论 0原文

我一直在尝试一些新的东西,我需要从流中插入图像,但是每次我尝试插入图像时它都会损坏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));

    }

谢谢 Quandary

,但这也没有帮助,所以我将图像保存到文件夹中并使用 Interop 中的 AddPicture 方法

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.

i have the following code adding the images to the word document

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

but that didn't help as well, so i am saving the images to a folder and using the AddPicture Method in Interop

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

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

发布评论

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

评论(1

多像笑话 2024-12-25 18:49:12

为什么不使用 docx 库?

3) DocX also supports 
     a) Pictures, 

http://docx.codeplex.com/

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

Why don't you use the docx library ?

3) DocX also supports 
     a) Pictures, 

http://docx.codeplex.com/

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

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