Beispiel #1
0
        /// <summary>
        /// Add Image object to Word document
        /// </summary>
        /// <param name="stream">Image stream</param>
        /// <param name="type">Image format</param>
        /// <returns></returns>
        public TImgResult AddImage(Stream stream, ImagePartType type)
        {
            TImgResult res = new TImgResult();

            res.ID = "";

            ImagePart imagePart;
            Bitmap    bitmap;

            try
            {
                imagePart       = doc.MainDocumentPart.AddImagePart(type);
                bitmap          = new Bitmap(stream);
                stream.Position = 0;
                imagePart.FeedData(stream);
                stream.Dispose();
            }
            catch (Exception)
            {
                stream.Dispose();
                return(res);
            }

            res.ID     = doc.MainDocumentPart.GetIdOfPart(imagePart);
            res.width  = (Int64Value)((bitmap.Width / bitmap.VerticalResolution) * 914400L);
            res.height = (Int64Value)((bitmap.Height / bitmap.HorizontalResolution) * 914400L);

            bitmap.Dispose();
            return(res);
        }
Beispiel #2
0
        public void FillTableCell(int tableIndex, int colIndex, int rowIndex, TImgResult img)
        {
            Table table = doc.MainDocumentPart.Document.Body.Elements <Table>().ElementAt(tableIndex);

            // Find the second row in the table.
            TableRow row = table.Elements <TableRow>().ElementAt(rowIndex);

            // Find the third cell in the row.
            TableCell cell = row.Elements <TableCell>().ElementAt(colIndex);

            // Find the first paragraph in the table cell.
            Paragraph p = cell.Elements <Paragraph>().First();


            // Define the reference of the image.
            var element =
                new Drawing(
                    new DW.Inline(
                        new DW.Extent()
            {
                Cx = img.width, Cy = img.height
            },
                        new DW.EffectExtent()
            {
                LeftEdge   = 0L,
                TopEdge    = 0L,
                RightEdge  = 0L,
                BottomEdge = 0L
            },
                        new DW.DocProperties()
            {
                Id   = (UInt32Value)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.png"
            },
                                        new PIC.NonVisualPictureDrawingProperties()),
                                    new PIC.BlipFill(
                                        new A.Blip(
                                            new A.BlipExtensionList(
                                                new A.BlipExtension()
            {
                Uri =
                    "{28A0092B-C50C-407E-A947-70E740481C1C}"
            })
                                            )
            {
                Embed            = img.ID,
                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 = img.width, Cy = img.height
            }),
                                        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"
            });



            // Find the first run in the paragraph.
            Run r = p.Elements <Run>().First();

            r.Append(element);

            // Set the text for the run.
            //Text t = r.Elements<Text>().First();
            //t.Text = txt;
        }