Quantcast
Channel: Open XML Format SDK forum
Viewing all articles
Browse latest Browse all 1288

Fill Picture content control in header of word doc using OpenXML

$
0
0

Hi,

I want to fill my Picture Content Control that is located in the headerof my word document with this code: (I have passed content control tag and the image stream via document parameter to this function)

       public void FillDocument(Stream stream, XDocument document)
        {                

using (WordprocessingDocument wordDocument =
                WordprocessingDocument.Open(stream, true))
            {
                List<SdtElement> descendants = wordDocument.MainDocumentPart.Document.Descendants<SdtElement>().ToList();

foreach (var headerPart in wordDocument.MainDocumentPart.HeaderParts)
                {
                    descendants.AddRange(headerPart.Header.Descendants<SdtElement>().ToList());
                }
                foreach (var footerPart in wordDocument.MainDocumentPart.FooterParts)
                {
                    descendants.AddRange(footerPart.Footer.Descendants<SdtElement>().ToList());
                }

                XDocument doc = document;

                foreach (SdtElement item in descendants)
                {
                    SdtAlias alias = item.Descendants<SdtAlias>().FirstOrDefault();

                    if (alias != null)
                    {                         
                        string sdtTitle = alias.Val.Value;


                        //if Sdt Content Control is Picture
                        string imageContent = (from xElement in doc.Descendants("Picture") where xElement.Attribute("Id").Value == sdtTitle select xElement.Value).FirstOrDefault();
                        if (imageContent != null)
                        {
                            MemoryStream result = (MemoryStream)StringToStream(imageContent);

                            SdtProperties p = item.Elements<SdtProperties>().FirstOrDefault();
                            if (p != null)
                            {
                                // Is it a picture content control?
                                SdtContentPicture pict = p.Elements<SdtContentPicture>().FirstOrDefault();
                                // Get the alias.
                                SdtAlias a = p.Elements<SdtAlias>().FirstOrDefault();
                                if (pict != null && a.Val.Value == sdtTitle)
                                {
                                    string embed = null;
                                    Drawing dr = item.Descendants<Drawing>().FirstOrDefault();
                                    if (dr != null)
                                    {
                                        D.Blip blip = dr.Descendants<D.Blip>().FirstOrDefault();
                                        if (blip != null)
                                            embed = blip.Embed;
                                        if (embed != null)
                                        {
                                            IdPartPair idpp = wordDocument.MainDocumentPart.Parts
                                                .Where(pa => pa.RelationshipId == embed).FirstOrDefault();
                                            if (idpp != null)
                                            {
                                                ImagePart ip = (ImagePart)idpp.OpenXmlPart;
                                                ip.FeedData(result);
                                            }
                                        }
                                    }
                                }
                            }



                            continue;
                        }

}

}

It finds the Pictire content control in word document but in this line:

 ImagePart ip = (ImagePart)idpp.OpenXmlPart;

I get this error:

Unable to cast object of type ‘DocumentFormat.OpenXml.Packaging.CustomXmlPart’ to type ‘DocumentFormat.OpenXml.Packaging.ImagePart’.

Could you please guide me?







Viewing all articles
Browse latest Browse all 1288

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>