Example #1
0
        /// <summary>
        /// Replaces the text parts in the documents.
        /// </summary>
        /// <param name="mainPart">The document to search in.</param>
        /// <param name="imageCount">The number of images</param>
        private void ReplaceImage(MainDocumentPart mainPart, ref int imageCount)
        {
            Image image = Value as Image;

            if (image == null)
            {
                return;
            }

            //Find the tags in the body of the document. The tag value must correspond to the name provided in this object.
            //For images this is the SdtCell object.
            var sdtList = mainPart.Document.Descendants <SdtElement>().Where(s => s.SdtProperties.GetFirstChild <Tag>().Val.Value == Name).ToList();

            //Search the header part for the custom tag.
            foreach (var headerPart in mainPart.HeaderParts)
            {
                foreach (var element in  headerPart.Header.Descendants <SdtElement>().Where(s => s.SdtProperties.GetFirstChild <Tag>().Val.Value == Name).ToList())
                {
                    foreach (var run in element.Descendants <Run>().ToList().SelectMany(runs => runs))
                    {
                        //Find the image.
                        var images = run.Descendants <Drawing>().ToList();
                        if (images.Count != 1)
                        {
                            continue;
                        }
                        var drawing = images[0];
                        //When the image is found add the new image to the document.
                        string imageName;
                        imageCount++;
                        var rid = CommonDocumentFunctions.AddPictureToOOXMLDocument(headerPart, image, imageCount, out imageName);
                        //Replace the old image name and id wityh the new one.
                        var properties = drawing.Inline.DocProperties;
                        properties.Name = imageName;
                        //Find the picture object.
                        var picture = drawing.Inline.Graphic.GraphicData.Descendants <DocumentFormat.OpenXml.Drawing.Pictures.Picture>().ToList();
                        if (picture.Count != 1)
                        {
                            continue;
                        }
                        picture[0].BlipFill.Blip.Embed = rid;
                    }
                }
                //sdtList.AddRange(headerPart.Header.Descendants<SdtElement>().Where(s => s.SdtProperties.GetFirstChild<Tag>().Val.Value == Name).ToList());
            }
            //Search the footer for the custom tag.
            foreach (var footerPart in mainPart.FooterParts)
            {
                sdtList.AddRange(footerPart.Footer.Descendants <SdtElement>().Where(s => s.SdtProperties.GetFirstChild <Tag>().Val.Value == Name).ToList());
            }
            //Loop through the found items and replace the image. This will keep the formatting as is in the document.
            if (sdtList.Count <= 0)
            {
                return;
            }
            foreach (var run in sdtList.Select(block => block.Descendants <Run>().ToList()).SelectMany(runs => runs))
            {
                //Find the image.
                var images = run.Descendants <Drawing>().ToList();
                if (images.Count != 1)
                {
                    continue;
                }
                var drawing = images[0];
                //When the image is found add the new image to the document.
                string imageName;
                var    rid = CommonDocumentFunctions.AddPictureToOOXMLDocument(mainPart, image, 0, out imageName);
                //Replace the old image name and id wityh the new one.
                var properties = drawing.Inline.DocProperties;
                properties.Name = imageName;
                //Find the picture object.
                var picture = drawing.Inline.Graphic.GraphicData.Descendants <DocumentFormat.OpenXml.Drawing.Pictures.Picture>().ToList();
                if (picture.Count != 1)
                {
                    continue;
                }
                picture[0].BlipFill.Blip.Embed = rid;
            }
        }
Example #2
0
        /// <summary>
        /// Gets the picture in OOXMLFormat
        /// </summary>
        /// <param name="mainPart">The main part of the OOXML Document</param>
        /// <param name="index">The index of the image in the list of pargraphs.
        /// This is used for generating an identifier for the image</param>
        /// <param name="imageCount">The number of images in the document.
        ///  This is needed for the caption number</param>
        /// <returns>Picture in OOXML Document</returns>
        public List <OOXMLParagraph> GetOOXMLParagraph(OpenXmlPartContainer mainPart, int index, ref int imageCount)
        {
            var imageName = string.Empty;
            var rid       = CommonDocumentFunctions.AddPictureToOOXMLDocument(mainPart, _image, index, out imageName);

            var paragraph =
                new OOXMLParagraph(
                    new Run(
                        new RunProperties(
                            new NoProof()),
                        new Drawing(
                            new wp.Inline(
                                new wp.Extent {
                Cx = 5274945L, Cy = 3956050L
            },
                                new wp.EffectExtent {
                LeftEdge = 19050L, TopEdge = 0L, RightEdge = 1905L, BottomEdge = 0L
            },
                                new wp.DocProperties {
                Id = (UInt32Value)1U, Name = imageName, Description = _text
            },
                                new wp.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 = _text
            },
                                                new pic.NonVisualPictureDrawingProperties()),
                                            new pic.BlipFill(
                                                new a.Blip {
                Embed = rid, 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 = 5274945L, Cy = 3956050L
            }),
                                                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
            }))
                    );

            var paragraphs = new List <DocumentFormat.OpenXml.Wordprocessing.Paragraph> {
                paragraph
            };

            //Add the caption if there is one.
            if (!string.IsNullOrEmpty(_text))
            {
                paragraphs.Add(captionParagraph(_text, ref imageCount));
            }
            return(paragraphs);
        }