Creating an ImagePart isn't saving the Relationship in OpenXML

前端 未结 1 1367
既然无缘
既然无缘 2021-01-20 14:46

--Updated to provide full working class example, with 2 sample documents--

www.sklinar.co.uk/wp-content/uploads/mydoc.docx - Original Document with a INCLUDETEXT ins

1条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-01-20 15:28

    I've analyzed your word document. There are a few issues with your word document:

    1. If I try to open your word document in the MS productivity toolkit I get the error message "Root element is missing".

    2. If I open your document as a zip file (renaming to mergedfooter.zip) then in the footer2.xml.rels file the relationship for your image is missing. I think that's the reason you get the "red box".

    To further analyze your problem I need the complete code (how do you get the FooterPart)?

    Below you will find an example of how to insert an image into the footer of a word document. Please note, that the example below assumes that your word document already contains a footer and your footer contains a paragraph element.

    using (WordprocessingDocument wordprocessingDocument = WordprocessingDocument.Open("mydoc.docx", true))
    {
      MainDocumentPart mainPart = wordprocessingDocument.MainDocumentPart;
    
      // Search for your footer part here.
      // Just for the sake of simplicity I take the second footer part.
      FooterPart fp = mainPart.FooterParts.ToList()[2];                
    
      // Create new image part.
      ImagePart ip = fp.AddImagePart(ImagePartType.Jpeg);
    
      using (FileStream fs = File.Open("mypicture.jpg", FileMode.Open))
      {         
        ip.FeedData(fs);         
      }
    
      string relationshipId = fp.GetIdOfPart(ip);
    
      // Create the image element using your function.
      Drawing img = CreateImage(relationshipId);
    
      Run r = new Run(img);
    
      Paragraph para = fp.RootElement.Descendants().FirstOrDefault();
    
      if(para != null)
      {
        para.Append(r);   
      }      
      else
      {
        Console.WriteLine("paragraph is null...");
      }
    }
    

    EDIT:

    After analyzing your newly provided documents:

    The reason the releationship for your image is not saved is because you do not Dispose() or Close() your word document.

    So, just add a using statement:

    using (WordprocessingDocument mainDoc = WordprocessingDocument.Open("mydoc.docx", true))
    {
      foreach (var item in mainDoc.MainDocumentPart.FooterParts)
      {
        ProcessParaIncludeTextMerge(item, item.Footer.Descendants(), mainDoc);
        item.Footer.Save();
      }
    
      mainDoc.MainDocumentPart.Document.Save();           
    }
    

    Furthermore in your ReplaceRunsWithRuns() method you must use PIC.Picture to reference the correct Picture class:

    foreach (var item in value)
    {
      if (!item.Descendants().Any())
      {
        p.Append(item.CloneNode(true));
      }
      else
      {
        p.Append(CreateImageRun(source, item, target, f));
      }
    }
    

    By the same token in your CreateImageRun() method I've changed the first three code lines:

    ImagePart newPart = footerPart.AddImagePart(ImagePartType.Jpeg);                   
    A.Blip shape = sourceRun.Descendants().FirstOrDefault();
    ImagePart p = sourceDoc.MainDocumentPart.GetPartById(shape.Embed.Value) as ImagePart;
    

    With theses changes the image appears in the mydoc.docx word document.

    0 讨论(0)
提交回复
热议问题