1
votes

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...
1
I have the same issue, currently trying to find a solution. I believe that the folder where the image is stored is not super-important, because in the path to the image is saved in the .rel fileGerino

1 Answers

2
votes

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

This line in the MSDN docs:

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

is invalid, because of the https that should be http.

Changing that makes the document open without issue.