You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Describe the bug
We have been trying to work out why when some 3rd party systems convert a Word document that we have generated using the Open XML SDK that images are not visible and came across other people with the same issue.
We have done quite a bit of investigation and research and it appears to be down to the SDK putting the images and relationships in a media folder in the root of the package, rather than in the word sub folder.
If one of these documents is editing in Word then it detects that this is wrong and moves the images and then the document is fine again. Someone posted on the internet about this 3.5 years ago and have a work-around which was to insert the images manually and avoid the AddImagePart function but his sample code no longer works in the new versions and it seems to make more sense to resolve the issue at source rather than trying to fudge a way round it.
sample code that shows the behaviour:
ImagePart imagePart = mainPart.AddImagePart(ImagePartType.Png);
imagePart.FeedData(imageStream);
I also tried using an embedded object to see if I could make that work in the meantime and although it didn't do what I wanted it does put the EmbeddedObjects in the correct WORD sub folder so the functionality appears to be inconsistent:
var imagePart = mainPart.AddEmbeddedObjectPart(ImagePartType.Png);
imagePart.FeedData(imageStream);
Screenshots
Word generated document has created the media folder under the word folder:
OpenXMLSDK generated document has created the media folder in root:
To Reproduce
Steps to reproduce the behavior: ImagePart imagePart = mainPart.AddImagePart(ImagePartType.Png);
imagePart.FeedData(imageStream);
Observed behavior
The image uploads to the \media folder within the package.
Expected behavior
The image should upload to the \word\media folder within the package.
Additional context
If the AddImagePart function could be changed to upload media files into the word sub folder then that would be ideal but if that proposes a problem then having an additional parameter would be a good alternative solution to get the desired result.
Desktop (please complete the following information)
OS: Docker mcr.microsoft.com/dotnet/aspnet:8.0
Office version [e.g. 16.0.15427.20178]
.NET Target: .net8
DocumentFormat.OpenXml Version: 3.0.2
The text was updated successfully, but these errors were encountered:
Any news on this? I also have to keep using the old version (2.19.0), because of this issue. DocumentFormat.OpenXml.Packaging.Package is not available more in the current version, so the workaround (stackoverflow) doesn't work anymore.
This is the code I used as a workaround in the meantime if it helps anyone:
// Add physical image to main document part
UInt32Value useId = ReturnRandomUInt32Value(1000000000, 2147483647);
// -----------------------------------------------------------------------------------------------------
// This but uploads the image into \media and which Word moves to \word\media if amended so logged with Microsoft
// ImagePart imagePart = mainPart.AddImagePart(ImagePartType.Png);
// imagePart.FeedData(imageStream);
// string relationshipId = mainPart.GetIdOfPart(imagePart);
// -----------------------------------------------------------------------------------------------------
// New code to replace the above - If Microsoft resolve the AddImagePart function can be swapped back
Uri imageUri = new("/word/media/" + useId + ".png", UriKind.Relative);
Uri imageUriInternal = new("media/" + useId + ".png", UriKind.Relative);
var docPackage = mainPart.OpenXmlPackage.GetPackage();
var imagePart = docPackage.CreatePart(imageUri, "image/png", System.IO.Packaging.CompressionOption.NotCompressed);
imageStream.CopyTo(imagePart.GetStream(FileMode.Open, FileAccess.Write));
var mainPartPackage = docPackage.GetPart(mainPart.Uri);
var docRelationship = mainPartPackage.Relationships.Create(imageUriInternal, System.IO.Packaging.TargetMode.Internal, "http://schemas.openxmlformats.org/officeDocument/2006/relationships/image");
string relationshipId = docRelationship.Id;
// -----------------------------------------------------------------------------------------------------
Describe the bug
We have been trying to work out why when some 3rd party systems convert a Word document that we have generated using the Open XML SDK that images are not visible and came across other people with the same issue.
We have done quite a bit of investigation and research and it appears to be down to the SDK putting the images and relationships in a media folder in the root of the package, rather than in the word sub folder.
If one of these documents is editing in Word then it detects that this is wrong and moves the images and then the document is fine again. Someone posted on the internet about this 3.5 years ago and have a work-around which was to insert the images manually and avoid the AddImagePart function but his sample code no longer works in the new versions and it seems to make more sense to resolve the issue at source rather than trying to fudge a way round it.
sample code that shows the behaviour:
ImagePart imagePart = mainPart.AddImagePart(ImagePartType.Png);
imagePart.FeedData(imageStream);
I also tried using an embedded object to see if I could make that work in the meantime and although it didn't do what I wanted it does put the EmbeddedObjects in the correct WORD sub folder so the functionality appears to be inconsistent:
var imagePart = mainPart.AddEmbeddedObjectPart(ImagePartType.Png);
imagePart.FeedData(imageStream);
Screenshots
Word generated document has created the media folder under the word folder:
OpenXMLSDK generated document has created the media folder in root:
To Reproduce
Steps to reproduce the behavior:
ImagePart imagePart = mainPart.AddImagePart(ImagePartType.Png);
imagePart.FeedData(imageStream);
Observed behavior
The image uploads to the \media folder within the package.
Expected behavior
The image should upload to the \word\media folder within the package.
Additional context
If the AddImagePart function could be changed to upload media files into the word sub folder then that would be ideal but if that proposes a problem then having an additional parameter would be a good alternative solution to get the desired result.
Desktop (please complete the following information)
OS: Docker mcr.microsoft.com/dotnet/aspnet:8.0
Office version [e.g. 16.0.15427.20178]
.NET Target: .net8
DocumentFormat.OpenXml Version: 3.0.2
The text was updated successfully, but these errors were encountered: