Image on Pic Content Control

Mar 15, 2012 at 11:34 PM

Me again!

First, thank you for the wonderful code you shared.

 

Next, is there any example on how to substitute a content control of type picture with a string representation (or whatever else) of the picture you want to insert?

 

Thank you

Coordinator
Mar 16, 2012 at 7:59 AM

 

I have added images using msdn code "How to: Insert a Picture into a Word Processing Document" at http://msdn.microsoft.com/en-us/library/bb497430.aspx

Mar 18, 2012 at 11:30 PM

Thank you. Is there a way to use the content controls as in your examples to define where in the document to insert an image?

Mar 19, 2012 at 7:35 PM

Just to let followers know that following the link above I was able to enter an image into the document easily. Thank you for the add.

Suggestion: why not adding a PlaceHolderType.Image?

Coordinator
Mar 19, 2012 at 8:45 PM

Great suggestion. I'll try to incorporate this in the next version. I'm too busy with coding an application using Kinect for Windows SDK. Next version may take more than a month. Thanks.

 

Regards,

Atul

Oct 23, 2012 at 5:36 PM

Hi Atul, great code and has come in really helpful.  Did you have any joy with the Image Placeholder type? 

Mar 19 at 10:18 AM
Hi, here is a how you can insert / draw an image into Word documents. Hope it'll help!
Oct 30 at 6:21 AM
Edited Oct 31 at 3:29 AM
I have incorporated this function! see bellow code: (change OpenXmlHelper.cs code)
overload CreateRun function
private static Run CreateRun(OpenXmlCompositeElement openXmlCompositeElement, OpenXmlElement openXmlElement)
        {
            RunProperties runProperties = openXmlCompositeElement.Descendants<RunProperties>().FirstOrDefault();
            Run run = null;

            if (runProperties != null)
            {
                run = new Run(runProperties.CloneNode(true), openXmlElement);
            }
            else
            {
                run = new Run(openXmlElement);
            }

            return run;
        }
Add a new function to detect Picture Content Control:
private static bool IsContentControlPicture(SdtElement contentControl)
        {
            SdtContentPicture contentPicture = contentControl.SdtProperties.Elements<SdtContentPicture>().FirstOrDefault();
            
            return contentPicture != null;
        }
modify SetContentOfContentControl method:
public void SetContentOfContentControl(SdtElement contentControl, string content)
        {
            if (contentControl == null)
            {
                throw new ArgumentNullException("contentControl");
            }

            content = string.IsNullOrEmpty(content) ? string.Empty : content;
            bool isCombobox = contentControl.SdtProperties.Descendants<SdtContentDropDownList>().FirstOrDefault() != null;
            if (isCombobox)
            {
                OpenXmlCompositeElement openXmlCompositeElement = GetSdtContentOfContentControl(contentControl);
                Run run = CreateRun(openXmlCompositeElement, content);
                SetSdtContentKeepingPermissionElements(openXmlCompositeElement, run);
            }
            else
            {
                OpenXmlCompositeElement openXmlCompositeElement = GetSdtContentOfContentControl(contentControl);
                contentControl.SdtProperties.RemoveAllChildren<ShowingPlaceholder>();
                List<Run> runs = new List<Run>();

                if (IsContentControlMultiline(contentControl))
                {
                    List<string> textSplitted = content.Split(Environment.NewLine.ToCharArray()).ToList();
                    bool addBreak = false;

                    foreach (string textSplit in textSplitted)
                    {
                        Run run = CreateRun(openXmlCompositeElement, textSplit);

                        if (addBreak)
                        {
                            run.AppendChild<Break>(new Break());
                        }

                        if (!addBreak)
                        {
                            addBreak = true;
                        }

                        runs.Add(run);
                    }
                }
                else if (IsContentControlPicture(contentControl))
                {
                    content = string.IsNullOrEmpty(content) ? string.Empty : content;
                    if (!File.Exists(content)) return;
                    Bitmap image = new Bitmap(content);
                    MainDocumentPart mainDocumentPart = contentControl.Ancestors<Document>().First().MainDocumentPart;
                    ImagePart imagePart = mainDocumentPart.AddImagePart(ImagePartType.Jpeg);
                    using (MemoryStream stream = new MemoryStream())
                    {
                        image.Save(stream, ImageFormat.Jpeg);
                        stream.Position = 0;
                        imagePart.FeedData(stream);
                    }
                    
                    Inline inline = openXmlCompositeElement.Descendants<Inline>().FirstOrDefault();
                    Drawing drawing = null;
                    if (inline != null)
                    {
                        drawing= new Drawing(inline.CloneNode(true));
                    }
                    else
                    {

                        drawing = new Drawing();
                    }
                    Blip blip = drawing.Descendants<Blip>().FirstOrDefault();
                    blip.Embed = mainDocumentPart.GetIdOfPart(imagePart);

                    Run run = CreateRun(openXmlCompositeElement, drawing);
                    runs.Add(run);
                }
                else
                {
                    runs.Add(CreateRun(openXmlCompositeElement, content));
                }

                if (openXmlCompositeElement is SdtContentCell)
                {
                    AddRunsToSdtContentCell(openXmlCompositeElement as SdtContentCell, runs);
                }
                else if (openXmlCompositeElement is SdtContentBlock)
                {
                    Paragraph para = CreateParagraph(openXmlCompositeElement, runs);
                    SetSdtContentKeepingPermissionElements(openXmlCompositeElement, para);
                }
                else
                {
                    SetSdtContentKeepingPermissionElements(openXmlCompositeElement, runs);
                }
            }
        }
how to use:
add a new field to demo data "Order",
public class Order
    {
        public List<Vendor> vendors = null;
        public List<Item> items = null;

        public Guid Id = Guid.Empty;
        public string Name = string.Empty;
        public string Description = string.Empty;
        public string ImageUri = string.Empty;
    }
and initial Order class in Program.cs
private static Order GetDataContext()
        {
            Order order = new Order();

            order.Name = "Test Order";
            order.Description = "Test Order Description";
            order.Id = Guid.NewGuid();
            order.ImageUri = "1.bmp";
            order.vendors = new System.Collections.Generic.List<Vendor>();
            order.items = new System.Collections.Generic.List<Item>();

            order.vendors.Add(new Vendor() { Name = "Vendor 1", Id = Guid.NewGuid() });
            order.vendors.Add(new Vendor() { Name = "Vendor 2", Id = Guid.NewGuid() });

            order.items.Add(new Item() { Name = "Item 1", Id = Guid.NewGuid() });
            order.items.Add(new Item() { Name = "Item 2", Id = Guid.NewGuid() });

            return order;
        }
add a picture type content control to word template tag it as "PlaceholderImageA"
then use it as usual as other content control.


if anyone has better solution or problem, please tell me: rqx110@163.com