Search code examples
wpfms-wordopenxml-sdk

WPF Canvas to word documents using OpenXML


I'm currently working on a project that requires importing the image drawn on a WPF Canvas (Mostly Geometries like lines and ellipses inside Path) into a Word Document. I'm using Openxml to do the job. Here's what I did

Here's what I've got so far,

Canvas is converted into memory stream by the following piece of code

public static Stream CreateJPGStream(this Canvas canvas)
    {
        RenderTargetBitmap renderBitmap = new RenderTargetBitmap((int)canvas.ActualWidth, (int)canvas.ActualHeight, 96d, 96d, PixelFormats.Pbgra32);
        canvas.Measure(new Size((int)canvas.ActualWidth, (int)canvas.ActualHeight));
        canvas.Arrange(new Rect(new Size((int)canvas.ActualWidth, (int)canvas.ActualHeight)));
        renderBitmap.Render(canvas);
        var stream = new MemoryStream();
        var encoder = new JpgBitmapEncoder();
        encoder.Frames.Add(BitmapFrame.Create(renderBitmap));
        encoder.Save(stream);
        return stream;
    }

I believe this was not the cause of the problem since I have been using this code for a while now and I haven't had any issues so far

Then I just modified code from Microsoft Guide

    public static void InsertPicture(this WordprocessingDocument word, Stream stream)
    {
        MainDocumentPart mainPart = word.MainDocumentPart;
        if (mainPart == null)
        {
            mainPart = word.AddMainDocumentPart();
            mainPart.Document = new Document() { Body = new Body() };
        }
        ImagePart imagePart = mainPart.AddImagePart(ImagePartType.Jpeg);
        imagePart.FeedData(stream);
        AddImageToBody(word, mainPart.GetIdOfPart(imagePart));
    }

        private static void AddImageToBody(WordprocessingDocument wordDoc, string relationshipId)
    {
        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 = 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 = "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 = 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 }))
                         )
                         { Uri = "http://schemas.openxmlformats.org/drawingml/2006/picture" })
                 )
                 {
                     DistanceFromTop = 0U,
                     DistanceFromBottom = 0U,
                     DistanceFromLeft = 0U,
                     DistanceFromRight = 0U,
                     EditId = "50D07946"
                 });
        wordDoc.MainDocumentPart.Document.Body.AppendChild(new Paragraph(new Run(element)));
    }

Now to join this two methods together

    public void InsertPicture(Canvas c)
    {
          using (var word = OpenXmlHelper.Create(filePath))
                {
                var stream = c.CreateJPGStream();
                word.InsertPicture(stream);
                 }
   }

But when I open the document, I get thisenter image description here

Am I doing something wrong with the memorysteam or on the openxml side? Can someone enlighten me please.

PS: I have looked at similar questions already like Inserting Image into DocX using OpenXML and setting the size


Solution

  • After writing to a stream, the stream's position will be at the end of the stream. You must rewind the stream to the beginning before you read from it.

    stream.Position = 0;

    or

    stream.Seek(0, System.IO.SeekOrigin.Begin);