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

Insert a Picture into the Picture/Image Content Control using Open XML SDK 2.0

$
0
0

I have created a word template as follows

Here PizzaPicture is a Picture Content Control, PizzaName is a Plain Text Content Control and PizzaDescription is a Rich Text Content Control

 

I am able to assign text to PizzaName and PizzaDescription Easily but not able to even locate the PizzaPicture. Here is my code

 

using (WordprocessingDocument document = WordprocessingDocument.Open(@".\test.docx", true)) {

                MainDocumentPart mainPart = document.MainDocumentPart;
                DocumentFormat.OpenXml.Wordprocessing.Text text = null;
                SdtContentBlock pizzaNameBlock = null;
                SdtContentBlock pizzaDescriptionBlock = null;//SdtContentBlock pizzaPictureBlock = null;  

                List<SdtBlock> sdtList = mainPart.Document.Descendants<SdtBlock>().ToList();foreach (SdtBlock sdt in sdtList)
                {
                    Console.WriteLine(sdt.SdtProperties.GetFirstChild<Tag>().Val.Value);
                }

                SdtBlock s1 = mainPart.Document.Body.Descendants<SdtBlock>().Where(r => r.SdtProperties.GetFirstChild<Tag>().Val == "PizzaName").Single();
                SdtBlock s2 = mainPart.Document.Body.Descendants<SdtBlock>().Where(r => r.SdtProperties.GetFirstChild<Tag>().Val == "PizzaDescription").Single();
                SdtBlock s3 = mainPart.Document.Body.Descendants<SdtBlock>().FirstOrDefault(r =>
                {
                    SdtProperties p = r.Elements<SdtProperties>().FirstOrDefault();if (p != null)
                    {
                        Console.WriteLine("P is not null");// Is it a picture content control?
                        SdtContentPicture pict =
                            p.Elements<SdtContentPicture>().FirstOrDefault();if (pict != null) Console.WriteLine("Pict is not null");// Get the alias.
                        SdtAlias a = p.Elements<SdtAlias>().FirstOrDefault();if (pict != null&& a.Val == "PizzaPicture")returntrue;
                    }returnfalse;
                });if (s3 == null) {
                    Console.Write(" s3 is Null");
                } else {
                    Console.WriteLine("s3 not null");
                }if (s1 != null) {
                    pizzaNameBlock = s1.Descendants<SdtContentBlock>().FirstOrDefault();
                    text = pizzaNameBlock.Descendants<DocumentFormat.OpenXml.Wordprocessing.Text>().FirstOrDefault();// here you can set the current time
                    text.Text = "Peperoni";
                    Console.WriteLine(text.Text);
                }string embed = null;if (s3 != null)
                {
                    Drawing dr = s3.Descendants<Drawing>().FirstOrDefault();if (dr != null)
                    {
                        Blip blip = dr.Descendants<Blip>().FirstOrDefault();if (blip != null)
                            embed = blip.Embed;
                    }
                }if (embed != null)
                {
                    IdPartPair idpp = document.MainDocumentPart.Parts
                        .Where(pa => pa.RelationshipId == embed).FirstOrDefault();if (idpp != null)
                    {
                        ImagePart ip = (ImagePart)idpp.OpenXmlPart;using (FileStream fileStream =
                            File.Open(@"c:\temp\pepperoni.jpg", FileMode.Open))
                            ip.FeedData(fileStream);
                    }
                }if (s2 != null) {
                    pizzaDescriptionBlock = s2.Descendants<SdtContentBlock>().FirstOrDefault();string altChunkId = "AltChunkId12345";
                    AlternativeFormatImportPart chunk = mainPart.AddAlternativeFormatImportPart(AlternativeFormatImportPartType.Xhtml, altChunkId);
                    chunk.FeedData(File.Open(@".\html.txt", FileMode.Open));
                    AltChunk altChunk = new AltChunk();
                    altChunk.Id = altChunkId;////Replace content control with altChunk information                
                    OpenXmlElement parent = pizzaDescriptionBlock.Parent;
                    parent.InsertAfter(altChunk, pizzaDescriptionBlock);
                    pizzaDescriptionBlock.Remove();
                }

                mainPart.Document.Save();
                document.Close();
            }


Here the call mainPart.Document.Descendants<SdtBlock>().ToList(); returns me only 2 items PizzaName, PizzaDescription butno PizzaPicture.

 

Secondly the line   SdtContentPicture pict =  p.Elements<SdtContentPicture>().FirstOrDefault();returns NULL. So it seems that there is no element of type SdtContentPicture.

What am I doing wrong? I am pretty sure that I have put PizzaPicture as a Picture content control.

 

My objective is to locate this control in the document and then insert an image into it programmatically.

 

 


MSDNStudent Knows not much!

Viewing all articles
Browse latest Browse all 1288

Trending Articles



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