none
Copying ImagePart from one Word doc to the other RRS feed

  • Question

  • I can copy image parts without any problems, but the actual media gets written into /media instead of /word/media . Also in the document.xml.rels I getTarget="/media/image.bin" (instead of .gif) for this Relationship. Using the copied image in the doc works just fine, but just for being organized sake I would prefer for it to be in the correct folder and with correct extension.

    This is the code I am using:
    Code Snippet

    using (WordprocessingDocument wordDoc1 = WordprocessingDocument.Open(fromDocument1, false))
    using (WordprocessingDocument wordDoc2 = WordprocessingDocument.Open(toDocument2, true))
    {
    foreach (ImagePart ipt in wordDoc1.MainDocumentPart.ImageParts)
    {
    ImagePart newIpt = wordDoc2.MainDocumentPart.AddImagePart(ipt.ContentType);
    newIpt.FeedData(ipt.GetStream());
    }

    }


    Any suggestions what I might be doing wrong?
    Thursday, July 12, 2007 9:12 PM

Answers

  • The issue is that the SDK uses hardcoded locations for parts, since this location is not important for document correctness (but it is for proper housekeeping as you mention).


    The second solution works because in effect you import a part instead of creating a new one.

     

    Wouter

    Friday, July 13, 2007 9:53 AM

All replies

  • I think the extension problem is because /[Content_Types].xml after AddPart has:

    <Default Extension="bin" ContentType="image/gif" />

    Is this something I can control through SDK when adding the part?
    Thursday, July 12, 2007 9:28 PM
  • This seems to do the job Smile

    Target is now Target="/word/media/image1.gif" and in content types I have <Default Extension="gif" ContentType="image/gif" />

    Code Snippet


               using (WordprocessingDocument wordDoc1 = WordprocessingDocument.Open(fromDocument1, false))
               using (WordprocessingDocument wordDoc2 = WordprocessingDocument.Open(toDocument2, true))
               {
                    foreach (ImagePart ipt in wordDoc1.MainDocumentPart.ImageParts)
                    {
                        ImagePart newIpt = wordDoc2.MainDocumentPart.AddPart<ImagePart>(ipt);
                    }

              }


    Thursday, July 12, 2007 9:48 PM
  • The issue is that the SDK uses hardcoded locations for parts, since this location is not important for document correctness (but it is for proper housekeeping as you mention).


    The second solution works because in effect you import a part instead of creating a new one.

     

    Wouter

    Friday, July 13, 2007 9:53 AM
  • Makes sense.  Thanks for explaining.
    Friday, July 13, 2007 1:19 PM
  • I am trying to copy the ImageParts inside each HeaderPart of a WordprocessingDocument but the parameters of the AddPart method for HeaderParts does not contain an ImagePart parameter. Does anyone know the reason for this?

     

     

    Monday, January 24, 2011 10:31 AM