none
Copy linked (external) images from a part to another RRS feed

  • Question

  • I need to copy the content of a word document header to the body, the header contain embeded images and linked (external) images. I have no problems with embedeb images since I copy parts from header to mainPart and I assign the relationship id correctly but I can't figure out how to do this for linked images (no image parts for linked images).
    Tuesday, October 23, 2012 5:17 PM

Answers

  • Thank you Tom and Brandon for your help,

    I am inserting the picture using interop without including the file with the document (only link and not include),

    contentControl.Range.InlineShapes.AddPicture(filePath, LinkToFile: true, SaveWithDocument: false);

    I solved my problem in this way:

    I iterate through drawings, foreach drawing I get the Blip then I generate a new random relationship id for this Blip (blip.Link = "newRandomId") and I get a copy for this relationship,  in this way when the drawing is copied to the body I avoid   the conflict with the exisiting relationships, then I add the external relationships to the body. Here is some code (not complete)

    Dictionary<String, ExternalRelationship> externalRelationships = new Dictionary<String, ExternalRelationship>();
    		foreach (var drawing in headerPart.Header.Descendants<Drawing>())
                                    {
                                        A.Blip blip = drawing.Descendants<A.Blip>().FirstOrDefault();
                                        if (blip != null && blip.Link != null && blip.Link.HasValue)
                                        {
                                            // Change the link that refer to relationship id to new random id to avoid conflicts when moved to body
                                            string newExternalRelationshipId = "R" + Guid.NewGuid().ToString("D",
                                                CultureInfo.InvariantCulture).Replace("-", "");
                                            // Save a copy of external relationship
                                            externalRelationships.Add(newExternalRelationshipId,
                                                headerPart.GetExternalRelationship(blip.Link));
                                            // Assign the new relationship id
                                            blip.Link = newExternalRelationshipId;
                                        }
                                    }
    				// Here code to copy header content to the body
    								
    				// Update copied drawings relationships in the body
    				foreach (Drawing drawing in mainPart.Document.Descendants<Drawing>())
                        		{
    				    A.Blip blip = drawing.Descendants<A.Blip>().FirstOrDefault();
    				    if (blip != null && blip.Link != null && blip.Link.HasValue)
                           		       {
                                             var externalRelationship = (from e in externalRelationships
                                                            where e.Key == blip.Link.Value
                                                            select e.Value).FirstOrDefault();
                                             if (externalRelationship != null)
                                                {
                                                   mainPart.AddExternalRelationship("http://schemas.openxmlformats.org/officeDocument/2006/relationships/image", externalRelationship.Uri,
                                                                     blip.Link.Value);
                                                }
                                            }
    				}

     

    Monday, October 29, 2012 8:54 AM

All replies

  • Hi Tunisian_BB,

    Thanks for posting in the MSDN Forum.

    I will involve some experts into your issue to see whether they can help you. There might be some time delay, appreciate for your patience.

    Have a good day,

    Tom


    Tom Xu [MSFT]
    MSDN Community Support | Feedback to us

    Thursday, October 25, 2012 5:13 AM
    Moderator
  • What method are you using to insert the linked images into the document?  Are you using the INCLUDEPICTURE field code or some other mechanism?

    Brandon

    Friday, October 26, 2012 10:36 PM
  • Thank you Tom and Brandon for your help,

    I am inserting the picture using interop without including the file with the document (only link and not include),

    contentControl.Range.InlineShapes.AddPicture(filePath, LinkToFile: true, SaveWithDocument: false);

    I solved my problem in this way:

    I iterate through drawings, foreach drawing I get the Blip then I generate a new random relationship id for this Blip (blip.Link = "newRandomId") and I get a copy for this relationship,  in this way when the drawing is copied to the body I avoid   the conflict with the exisiting relationships, then I add the external relationships to the body. Here is some code (not complete)

    Dictionary<String, ExternalRelationship> externalRelationships = new Dictionary<String, ExternalRelationship>();
    		foreach (var drawing in headerPart.Header.Descendants<Drawing>())
                                    {
                                        A.Blip blip = drawing.Descendants<A.Blip>().FirstOrDefault();
                                        if (blip != null && blip.Link != null && blip.Link.HasValue)
                                        {
                                            // Change the link that refer to relationship id to new random id to avoid conflicts when moved to body
                                            string newExternalRelationshipId = "R" + Guid.NewGuid().ToString("D",
                                                CultureInfo.InvariantCulture).Replace("-", "");
                                            // Save a copy of external relationship
                                            externalRelationships.Add(newExternalRelationshipId,
                                                headerPart.GetExternalRelationship(blip.Link));
                                            // Assign the new relationship id
                                            blip.Link = newExternalRelationshipId;
                                        }
                                    }
    				// Here code to copy header content to the body
    								
    				// Update copied drawings relationships in the body
    				foreach (Drawing drawing in mainPart.Document.Descendants<Drawing>())
                        		{
    				    A.Blip blip = drawing.Descendants<A.Blip>().FirstOrDefault();
    				    if (blip != null && blip.Link != null && blip.Link.HasValue)
                           		       {
                                             var externalRelationship = (from e in externalRelationships
                                                            where e.Key == blip.Link.Value
                                                            select e.Value).FirstOrDefault();
                                             if (externalRelationship != null)
                                                {
                                                   mainPart.AddExternalRelationship("http://schemas.openxmlformats.org/officeDocument/2006/relationships/image", externalRelationship.Uri,
                                                                     blip.Link.Value);
                                                }
                                            }
    				}

     

    Monday, October 29, 2012 8:54 AM