Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AddImagePart adds Media folder and images to Root instead of the Word sub folder within the document package #1730

Open
oaktechnology opened this issue May 24, 2024 · 3 comments
Assignees

Comments

@oaktechnology
Copy link

oaktechnology commented May 24, 2024

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:
StandardWord

OpenXMLSDK generated document has created the media folder in root:
OpenXMLGenerated

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

@voidray
Copy link

voidray commented Sep 17, 2024

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.

@liluhe
Copy link

liluhe commented Sep 18, 2024

I have the same issue.

@oaktechnology
Copy link
Author

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;
// -----------------------------------------------------------------------------------------------------

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants