插入图像会损坏Open XML SDK生成的Word文件

问题描述

我尝试修改https://docs.microsoft.com/en-us/office/open-xml/how-to-insert-a-picture-into-a-word-processing-document中的代码,以将图像插入到.Net Core MVC应用程序中的表格单元格中。但是,插入图像只会损坏Word文件。 我注意到,当我提取损坏的docx文件时,“ media”文件夹在“ word”文件夹之外。但是,当我手动将图像添加到docx文件时,“ word”文件夹中存在“ media”文件夹。

    ...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文档。

MSDN文档中的这一行:

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

无效,因为https应该为http

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

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...