Open-XML-SDK icon indicating copy to clipboard operation
Open-XML-SDK copied to clipboard

Adding image to document with image in header: Existing image no longer visible

Open twoelfer opened this issue 3 years ago • 12 comments

When adding an image to a docx that has a header with an image in it, the image in the header is no longer displayed in word after the new image has been added.

  • NET Framewor 4.8
  • DocumentFormar.OpenXml 2.13.1.0

This repros using the example code from msdn here ( https://docs.microsoft.com/en-us/office/open-xml/how-to-insert-a-picture-into-a-word-processing-document?redirectedfrom=MSDN), when using a docx that conatins a header which includes an image. Example docx attached to this issue. vs 2019 project with full sample code also attached to this issue.

Observed

  • Image from header is no longer displayed in word after image has been added to body.

Expected

  • both images should be displayed in word

(i have a so quation concerning this problem, including full description of things i tried hier: https://stackoverflow.com/questions/69637082/inserting-image-into-docx-using-openxml-removes-image-from-header )

docx-problem-files.zip docx-problem-source.zip .

twoelfer avatar Oct 20 '21 08:10 twoelfer

@twoelfer Thanks for reporting the issue. Does the issue occur using previous versions of the Open XML SDK or only in 2.13? From your StackOverflow question it sounds like this is not related strictly to the Open XML SDK but potentially a Word issue. Do you agree? We can still try to assist in determining what's wrong but I'd like to first determine if the SDK has changed something recently that caused this.

tomjebo avatar Oct 20 '21 17:10 tomjebo

@tomjebo Short answer: I don't know.

Long answer: I am actually working on another problem with "vanishing" images that are added to a document with images in the header. I that "real" case, the missing images are the first and second of the images that are added, not the image from the header. In that case, the problem (which is hopefully related) did appear with earlier versions of the sdk. i do not know which version - it was code that was made available by Eric White (i think in his blog), and i think that was available before the sdk even has been on github.

With that code, however, the "related" problem happened. When i realized there was a problem i updated the dll that i was using with the current openxml code from github. The problem persisted.

This was when i was trying to build an easy repro case (but failed for my exact problem, because we are talking about a huge codebase where the real problem appeared.). Thus, i was happy to see a bug which i hope is somehow related to my real problem.

in other words: i cannot really confirm this exact problem has been in the sdk in prior versions, but i think this seems very plausible.

About this beeing a word issue: i really don't see how. i mean, word does not have any problems with images inside the document when there are images inside the header.

One strange thing that i noticed though (might be common knowledge, but was news to me) is that, when adding images to the docx using AddImagePart(), the image location will be /media. Images from the header will be in /word/media. Smells a little like a source for reference-id mixups to me, but this is just a guess.

twoelfer avatar Oct 20 '21 18:10 twoelfer

@twoelfer thanks for the explanation. we can look into the documents and see if the SDK is adding images incorrectly.

tomjebo avatar Oct 20 '21 18:10 tomjebo

@tomjebo Any ideas when you might find the time?

twoelfer avatar Oct 26 '21 15:10 twoelfer

@twoelfer sorry, I was not able to get to this last week. I'll try this week.

tomjebo avatar Oct 26 '21 16:10 tomjebo

Could this be relevant to what other docx libs have been experiencing after Word release 2106?

https://github.com/python-openxml/python-docx/issues/981#issuecomment-891395503 https://github.com/elapouya/python-docx-template/issues/372 https://giters.com/jgm/pandoc/issues/7527

velkommen avatar Nov 04 '21 13:11 velkommen

Well in the python issue linked, word will complain about the docx. This doesn’t happen in this case.

twoelfer avatar Nov 04 '21 16:11 twoelfer

Hi Tom.

Sorry to bother you, but any news?

Anything i could do to help?

Von: Tom Jebo @.> Gesendet: Dienstag, 26. Oktober 2021 18:23 An: OfficeDev/Open-XML-SDK @.> Cc: thomas woelfer @.>; Mention @.> Betreff: Re: [OfficeDev/Open-XML-SDK] Adding image to document with image in header: Existing image no longer visible (Issue #1040)

@twoelfer https://github.com/twoelfer sorry, I was not able to get to this last week. I'll try this week.

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/OfficeDev/Open-XML-SDK/issues/1040#issuecomment-952104795 , or unsubscribe https://github.com/notifications/unsubscribe-auth/AJJNNNRSQYO32HAITLCBWGTUI3IXXANCNFSM5GLA7FYQ . Triage notifications on the go with GitHub Mobile for iOS https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675 or Android https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub . https://github.com/notifications/beacon/AJJNNNRANHGQNSQ3Q4LKEFLUI3IXXA5CNFSM5GLA7FY2YY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOHC77OWY.gif

twoelfer avatar Nov 19 '21 14:11 twoelfer

i faced the same issue but i am using python 3.10 But i was able to fix the issue understanding the root might be following.

document.xml file document.xml

header1.xml file header1.xml

i tried to update the id in the document.xml which solved the issue for me.

updated: During Merge i felt this might causing an issue. So i have updated the id as max(id)+1

eswarthammana avatar Jul 25 '22 10:07 eswarthammana

My recommendation in cases like this is to:

  1. manually create a correct Word document using Microsoft Word;
  2. check out the package structure and Open XML markup; and
  3. create the same structure and markup with your code.

In most cases, people create the wrong markup. Given that Open XML is so complex, this is quite normal. While not impossible, an actual bug in the Open XML SDK is the exception.

ThomasBarnekow avatar Jul 25 '22 13:07 ThomasBarnekow

I have the same problem but not for the same reason.

I have made other documents in c#, and I have inserted images in the header without problem. but there is a document that does not work, however, if I insert the image in the body of the document if it works...

I've been having this problem for days and I still haven't found the solution or the reason for the problem.

SeerGiuS96 avatar Nov 29 '22 14:11 SeerGiuS96

update: I tried what @eswarthammana said, it sounds good, i make the id autoincrement in each image inserted and still i cant have images working in the header even in the xml they got diferent ids...

SeerGiuS96 avatar Jan 09 '23 13:01 SeerGiuS96