Converting a /Embed to a w:drawing

后端 未结 1 338
刺人心
刺人心 2021-01-25 11:26

I have a V:Shape that I am assuming has come from the conversion of a .Doc file to a .Docx file, but in the code I have written, I am wanting a DocumentFormat

1条回答
  •  囚心锁ツ
    2021-01-25 11:35

    I found a quick/simple way to do this - and I should have worked it out earlier!

    The Shape object has a ImageData object as a descendant, so I checked for that, and ran it through an extra method to get the ImagePart:

    A.Blip pic = item.Descendants().FirstOrDefault();
    
    ImageData imageData = item.Descendants().FirstOrDefault();
    
    if (pic == null && imageData == null) //pictures are processed differently - they're an absolute s**t storm to code...
    {
      runToAmend.InsertAfterSelf(item.CloneNode(true));
    }
    else
    {
      if(pic != null)
      {
        runToAmend.InsertAfterSelf(CreateImageFromBlip(source, item, footerHeaderPart,pic));
      }
      else if (imageData != null)
      {
        runToAmend.InsertAfterSelf(CreateImageFromShape(source, item, footerHeaderPart, imageData));
      }
    }
    

    Then created a new method - CreateImageFromShape(..) which then calls the original CreateImageRun.

    private Run CreateImageFromShape(WordprocessingDocument sourceDoc, Run sourceRun, OpenXmlPart headerFooterPart, ImageData imageData)
    {
      ImagePart p = sourceDoc.MainDocumentPart.GetPartById(imageData.RelationshipId) as ImagePart;
    
      return CreateImageRun(sourceDoc, sourceRun, headerFooterPart, p);
    }
    

    Job done.

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