Exemplo n.º 1
0
        private static Drawing GetDrawingFromImage(this WordprocessingDocument wordDoc, BitmapSource image)
        {
            var imagePart = wordDoc.AddImagePart(image);
            var relationshipId = wordDoc.MainDocumentPart.GetIdOfPart(imagePart);

            var imageCx = CalculateImageCx(image);
            var imageCy = CalculateImageCy(image);

            var usablePageWidth = CalculateUsableWidthInEmus(wordDoc);

            if (imageCx > usablePageWidth)
            {
                var ratio = (double)usablePageWidth / (double)imageCx;
                imageCx = usablePageWidth;
                imageCy = (long)((double)imageCy * ratio);
            }

            var element = new Drawing(
                new DW.Inline(
                    new DW.Extent { Cx = imageCx, Cy = imageCy },
                    new DW.EffectExtent { LeftEdge = 0L, TopEdge = 0L, RightEdge = 0L, BottomEdge = 0L },
                    new DW.DocProperties { Id = GetNextId(), 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 = 0U, Name = "New Bitmap Image.png" },
                                    new PIC.NonVisualPictureDrawingProperties()),
                                new PIC.BlipFill(
                                    new A.Blip(
                                        new A.BlipExtensionList(
                                            new A.BlipExtension() { Uri = $"{{{Guid.NewGuid().ToString()}}}" }
                                            )
                                        )
                                    { 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 = imageCx, Cy = imageCy }
                                    ),
                                new A.PresetGeometry(
                                    new A.AdjustValueList()
                                    )
                                { Preset = A.ShapeTypeValues.Rectangle }
                                )
                                )
                            )
                        { Uri = "http://schemas.openxmlformats.org/drawingml/2006/picture" }
                        )
                    )
                {
                    DistanceFromTop = (UInt32Value)0U,
                    DistanceFromBottom = (UInt32Value)0U,
                    DistanceFromLeft = (UInt32Value)0U,
                    DistanceFromRight = (UInt32Value)0U,
                    EditId = "50D07946"
                }
            );
            return element;
        }