none
Replacing a Content Control with a Linked Image in a Word Document

    Question

  • I am having problems trying to replace a content control with a linked image. I have created a template in word with content controls that I am swapping out to build my final document. My linked image is showing up but it is showing a message saying "The linked image cannot be displayed. The file may have been moved, renamed, or deleted. Verify that the link points to the correct file and location." I am thinking it has something to do with the relationship but I cannot seem to figure it out. Can you please take a look at the below code and let me know if I am doing something wrong or not?

    private static void InsertPicture(MainDocumentPart mainDocumentPart, string profilePictureUrl)
            {
                Document doc = mainDocumentPart.Document;
    
                //Get the body
                var body = doc.GetFirstChild<Body>();
                //Get the first table which has our picture
                var table1 = body.GetFirstChild<Table>();
    
                var tableRow1 = table1.GetFirstChild<TableRow>();
    
                var tableCell1 = tableRow1.GetFirstChild<TableCell>();
    
                //Remove content control before replacing it with the linked image
                //sdtCell1.Remove();
    
                var paragraph1 = tableCell1.GetFirstChild<Paragraph>();
    
                var run1 = new Run();
    
                var drawing1 = new Drawing();
    
                var inline1 = new Inline
                                  {
                                      DistanceFromTop = 0U,
                                      DistanceFromBottom = 0U,
                                      DistanceFromLeft = 0U,
                                      DistanceFromRight = 0U
                                  };
    
                var extent1 = new Extent {Cx = 2286000L, Cy = 2159000L};
                var effectExtent1 = new EffectExtent {LeftEdge = 0L, TopEdge = 0L, RightEdge = 0L, BottomEdge = 0L};
                var docProperties1 = new DocProperties {Id = 2U, Name = "Profile_Picture.jpg"};
    
                var nonVisualGraphicFrameDrawingProperties1 = new Wp.NonVisualGraphicFrameDrawingProperties();
    
                var graphicFrameLocks1 = new GraphicFrameLocks {NoChangeAspect = true};
                graphicFrameLocks1.AddNamespaceDeclaration("a", "http://schemas.openxmlformats.org/drawingml/2006/main");
    
                nonVisualGraphicFrameDrawingProperties1.Append(graphicFrameLocks1);
    
                var graphic1 = new Graphic();
                graphic1.AddNamespaceDeclaration("a", "http://schemas.openxmlformats.org/drawingml/2006/main");
    
                var graphicData1 = new GraphicData {Uri = "http://schemas.openxmlformats.org/drawingml/2006/picture"};
    
                var picture1 = new Pic.Picture();
                picture1.AddNamespaceDeclaration("pic", "http://schemas.openxmlformats.org/drawingml/2006/picture");
    
                var nonVisualPictureProperties1 = new Pic.NonVisualPictureProperties();
                var nonVisualDrawingProperties1 = new Pic.NonVisualDrawingProperties {Id = 2U, Name = "Profile_Picture.jpg"};
                var nonVisualPictureDrawingProperties1 = new Pic.NonVisualPictureDrawingProperties();
    
                nonVisualPictureProperties1.Append(nonVisualDrawingProperties1);
                nonVisualPictureProperties1.Append(nonVisualPictureDrawingProperties1);
    
                //Add relationship to document 
                mainDocumentPart.AddExternalRelationship(
                    "http://schemas.openxmlformats.org/officeDocument/2006/relationships/image",
                    new Uri(profilePictureUrl, UriKind.Absolute), "ProfilePhoto");
    
                var blipFill1 = new Pic.BlipFill();
                var blip1 = new Blip {Link = "ProfilePhoto"};
    
                var stretch1 = new Stretch();
                var fillRectangle1 = new FillRectangle();
    
                stretch1.Append(fillRectangle1);
    
                blipFill1.Append(blip1);
                blipFill1.Append(stretch1);
    
                var shapeProperties1 = new Pic.ShapeProperties();
    
                var transform2D1 = new Transform2D();
                var offset1 = new Offset {X = 0L, Y = 0L};
                var extents1 = new Extents {Cx = 2286000L, Cy = 2159000L};
    
                transform2D1.Append(offset1);
                transform2D1.Append(extents1);
    
                var presetGeometry1 = new PresetGeometry {Preset = ShapeTypeValues.Rectangle};
                var adjustValueList1 = new AdjustValueList();
    
                presetGeometry1.Append(adjustValueList1);
    
                shapeProperties1.Append(transform2D1);
                shapeProperties1.Append(presetGeometry1);
    
                picture1.Append(nonVisualPictureProperties1);
                picture1.Append(blipFill1);
                picture1.Append(shapeProperties1);
    
                graphicData1.Append(picture1);
    
                graphic1.Append(graphicData1);
    
                inline1.Append(extent1);
                inline1.Append(effectExtent1);
                inline1.Append(docProperties1);
                inline1.Append(nonVisualGraphicFrameDrawingProperties1);
                inline1.Append(graphic1);
    
                drawing1.Append(inline1);
    
                run1.Append(drawing1);
                paragraph1.Append(run1);
            }


    Chris Jorgensen MCTS - MOSS 2007 and SharePoint 2010, MCPD - SharePoint 2010

    Tuesday, March 12, 2013 1:28 AM

Answers

  • Hi Chirs,

    You might want to read this post written by Eric White.

    Replacing a Picture in a Picture Content Control in an Open XML WordprocessingML Document

    Code 

    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    using System.Text;
    using DocumentFormat.OpenXml.Packaging;
    using DocumentFormat.OpenXml.Wordprocessing;
    using DocumentFormat.OpenXml.Drawing;
    class Program
    {
        static void Main(string[] args)
        {
            using (WordprocessingDocument doc =
                WordprocessingDocument.Open("Test1.docx", true))
            {
                SdtBlock cc = doc.MainDocumentPart.Document.Body.Descendants<SdtBlock>()
                    .FirstOrDefault(c =>
                    {
                        SdtProperties p = c.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 == "MyPicture")
                                return true;
                        }
                        return false;
                    });
                string embed = null;
                if (cc != null)
                {
                    Drawing dr = cc.Descendants<Drawing>().FirstOrDefault();
                    if (dr != null)
                    {
                        Blip blip = dr.Descendants<Blip>().FirstOrDefault();
                        if (blip != null)
                            embed = blip.Embed;
                    }
                }
                if (embed != null)
                {
                    IdPartPair idpp = doc.MainDocumentPart.Parts
                        .Where(pa => pa.RelationshipId == embed).FirstOrDefault();
                    if (idpp != null)
                    {
                        ImagePart ip = (ImagePart)idpp.OpenXmlPart;
                        using (FileStream fileStream =
                            File.Open("After.jpg", FileMode.Open))
                            ip.FeedData(fileStream);
                        Console.WriteLine("done");
                    }
                }
            }
        }
    }

    HTH


    Regards,
    Fermin
    What's life without whimsy?

    Thursday, March 14, 2013 5:47 AM

All replies

  • Also I have verified that the link to the image is valid.


    Chris Jorgensen MCTS - MOSS 2007 and SharePoint 2010, MCPD - SharePoint 2010

    Tuesday, March 12, 2013 1:31 AM
  • Hi Chirs,

    You might want to read this post written by Eric White.

    Replacing a Picture in a Picture Content Control in an Open XML WordprocessingML Document

    Code 

    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    using System.Text;
    using DocumentFormat.OpenXml.Packaging;
    using DocumentFormat.OpenXml.Wordprocessing;
    using DocumentFormat.OpenXml.Drawing;
    class Program
    {
        static void Main(string[] args)
        {
            using (WordprocessingDocument doc =
                WordprocessingDocument.Open("Test1.docx", true))
            {
                SdtBlock cc = doc.MainDocumentPart.Document.Body.Descendants<SdtBlock>()
                    .FirstOrDefault(c =>
                    {
                        SdtProperties p = c.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 == "MyPicture")
                                return true;
                        }
                        return false;
                    });
                string embed = null;
                if (cc != null)
                {
                    Drawing dr = cc.Descendants<Drawing>().FirstOrDefault();
                    if (dr != null)
                    {
                        Blip blip = dr.Descendants<Blip>().FirstOrDefault();
                        if (blip != null)
                            embed = blip.Embed;
                    }
                }
                if (embed != null)
                {
                    IdPartPair idpp = doc.MainDocumentPart.Parts
                        .Where(pa => pa.RelationshipId == embed).FirstOrDefault();
                    if (idpp != null)
                    {
                        ImagePart ip = (ImagePart)idpp.OpenXmlPart;
                        using (FileStream fileStream =
                            File.Open("After.jpg", FileMode.Open))
                            ip.FeedData(fileStream);
                        Console.WriteLine("done");
                    }
                }
            }
        }
    }

    HTH


    Regards,
    Fermin
    What's life without whimsy?

    Thursday, March 14, 2013 5:47 AM
  • Hi Chris,

    I temporarily marked the reply as answer. You can unmark it if it provides no help.

    Please feel free to let us know if you need any help.

    Have a nice day.


    Quist Zhang [MSFT]
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Wednesday, April 03, 2013 5:51 AM
    Moderator