Novacode.Paragraph.AppendPicture C# (CSharp) Method

AppendPicture() public method

Add an image to a document, create a custom view of that image (picture) and then insert it into a Paragraph using append.
public AppendPicture ( Picture p ) : Paragraph
p Picture The Picture to append.
return Paragraph
        public Paragraph AppendPicture(Picture p)
        {
            // Convert the path of this mainPart to its equilivant rels file path.
            string path = mainPart.Uri.OriginalString.Replace("/word/", "");
            Uri rels_path = new Uri("/word/_rels/" + path + ".rels", UriKind.Relative);

            // Check to see if the rels file exists and create it if not.
            if (!Document.package.PartExists(rels_path))
                HelperFunctions.CreateRelsPackagePart(Document, rels_path);

            // Check to see if a rel for this Picture exists, create it if not.
            var Id = GetOrGenerateRel(p);

            // Add the Picture Xml to the end of the Paragragraph Xml.
            Xml.Add(p.Xml);

            // Extract the attribute id from the Pictures Xml.
            XAttribute a_id =
            (
                from e in Xml.Elements().Last().Descendants()
                where e.Name.LocalName.Equals("blip")
                select e.Attribute(XName.Get("embed", "http://schemas.openxmlformats.org/officeDocument/2006/relationships"))
            ).Single();

            // Set its value to the Pictures relationships id.
            a_id.SetValue(Id);

            // For formatting such as .Bold()
            this.runs = Xml.Elements(XName.Get("r", DocX.w.NamespaceName)).Reverse().Take(p.Xml.Elements(XName.Get("r", DocX.w.NamespaceName)).Count()).ToList();

            return this;
        }

Usage Example

Example #1
0
        private void AddPicture(Paragraph pg, double width, double height, int?imageid)
        {
            const int oversizedpixelsinch = 310;
            const int pixelsinch          = 96;
            var       largewidth          = (oversizedpixelsinch * width).ToInt();
            var       largeheight         = (oversizedpixelsinch * height).ToInt();
            var       widthpixels         = (width * pixelsinch).ToInt();
            var       heightpixels        = (height * pixelsinch).ToInt();

            var img = ImageData.Image.ImageFromId(imageid);

            if (img != null)
            {
                using (var os = img.ResizeToStream(largewidth, largeheight, "pad"))
                {
                    var pic = docx.AddImage(os).CreatePicture(heightpixels, widthpixels);
                    pg.AppendPicture(pic);
                }
            }
        }
All Usage Examples Of Novacode.Paragraph::AppendPicture