插入图像会损坏Open XML SDK生成的Word文件 [英] Inserting image corrupts Open XML SDK generated Word file

查看:92
本文介绍了插入图像会损坏Open XML SDK生成的Word文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试修改 https://docs.microsoft.com/zh-CN/office/open-xml/how-to-insert-a-picture-into-a-word-processing-document 将图像插入到.Net Core MVC应用程序中的表格单元格中.但是,插入图像只会损坏Word文件.我已经注意到,当我提取损坏的docx文件时,媒体"就会被删除.文件夹在单词"之外.文件夹.但是,当我手动将图像添加到docx文件时,媒体"就会被删除.文件夹位于单词"内部.文件夹.

I have tried to adapt the code in https://docs.microsoft.com/en-us/office/open-xml/how-to-insert-a-picture-into-a-word-processing-document to insert an image to a table cell in my .Net Core MVC app. However, inserting the image just corrupts the word file. I have noticed that when I extract the corrupt docx file, the "media" folder is outside the "word" folder. However, when I manually add an image to a docx file, the "media" folder is present inside the "word" folder.

    ...code here...
    ImagePart imagePart = wordDoc.MainDocumentPart.AddImagePart(ImagePartType.Jpeg);

    using (FileStream stream = new FileStream(@$"{_env.WebRootPath}\images\mono.jpg", FileMode.Open))
    {
        imagePart.FeedData(stream);
    }

    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)11U, 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)10U, Name = "New Bitmap Image.jpg" },
            new PIC.NonVisualPictureDrawingProperties()),
            new PIC.BlipFill(
            new A.Blip(new A.BlipExtensionList(new A.BlipExtension(){ Uri = "{28A0092B-C50C-407E-A947-70E740481C1C}" })) { Embed = wordDoc.MainDocumentPart.GetIdOfPart(imagePart), 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 }))){ Uri = "https://schemas.openxmlformats.org/drawingml/2006/picture" })) { DistanceFromTop = (UInt32Value)0U, DistanceFromBottom = (UInt32Value)0U, DistanceFromLeft = (UInt32Value)0U, DistanceFromRight = (UInt32Value)0U, EditId = "50D07946" });

    Table table0 = new Table();

    TableProperties props0 = new TableProperties(...code here...);

    table0.AppendChild<TableProperties>(props0);

    var tr0 = new TableRow();
    var tc0 = new TableCell();
    var pp0 = new ParagraphProperties(new SpacingBetweenLines() { After = "0" }, new Justification() { Val = JustificationValues.Center });
    var rn0 = new Run(element);
    var pg0 = new Paragraph(pp0, rn0);
    tc0.Append(new TableCellProperties(new TableCellWidth { Type = TableWidthUnitValues.Dxa, Width = "1792" })), new HorizontalMerge { Val = MergedCellValues.Restart });
    tc0.Append(pg0);
    tr0.Append(tc0);
    table0.Append(tr0);
    body.Append(table0);
    doc.Append(body);
    ...code here...

推荐答案

我已经弄清楚了.应该责怪MSDN文档.

I have figured it out. The MSDN docs are to blame.

MSDN文档中的这一行:

This line in the MSDN docs:

                     ) { Uri = "https://schemas.openxmlformats.org/drawingml/2006/picture" })

无效,因为 https 应该是 http .

更改使文档可以正常打开.

Changing that makes the document open without issue.

这篇关于插入图像会损坏Open XML SDK生成的Word文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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