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

Replace an image #1

Closed
Selmirrrrr opened this issue Aug 25, 2021 · 7 comments
Closed

Replace an image #1

Selmirrrrr opened this issue Aug 25, 2021 · 7 comments

Comments

@Selmirrrrr
Copy link

Hello,

Firstly, thank you very much for this lib, it's a pleasure to work with. Easy to use, fast, and well documented thanks to the tests.

I work for a small company here in Switzerland, we use Wordroller to generate documents from existing templates. However, we would also need to replace images in a document.

Wordroller can replace text and add images to a document, so I think we're not so far away. I've tried many ways to accomplish this with the current version, but it looks like it's not a supported scenario...

I would be really happy to contribute to this project by adding this feature myself, but I'm completely new to the Office Open XML world and would probably need some guidance if it's possible,

Thanks again for the great work accomplished, it's very helpful !

Selmir

@Selmirrrrr Selmirrrrr changed the title Image an image Replace an image Aug 25, 2021
@Selmirrrrr
Copy link
Author

Just for info, for now, I've tried to only retrieve an existing image from a package :

package = Package.Open(memoryStream, FileMode.Open, FileAccess.ReadWrite);
var images= package.GetRelationshipsByType(RelationshipTypes.Image);

And from a Document part :

package = Package.Open(memoryStream, FileMode.Open, FileAccess.ReadWrite);

var officeDocumentRelationship = package.GetRelationshipsByType(RelationshipTypes.OfficeDocument).Single();

DocumentPart = package.GetPart(PackagePartHelper.EnsureCorrectUri(officeDocumentRelationship.TargetUri));
var images = DocumentPart.GetRelationships().Where(r => r.RelationshipType == relationshipType)

But each time this returns nothing even though there's an image in the document I'm processing, and I really don't get why...

@shestakov
Copy link
Owner

Hello, @Selmirrrrr, thanks for coming in!

Happy to know the lib adds some value to others, especially if it is a small business.

Image replacement is a crucial feature, indeed.

I hope, I can add the feature this week. Will also investigate retrieving existing images, and get back to you.

@Selmirrrrr
Copy link
Author

Thank you very much for the quick support. Personally, I don't really need to retrieve images, it was just an entry point to try to implement the replacement.

Btw, if you have a Patreon or a GitHub Sponsor account, I'll be happy to sponsor this feature :)

@shestakov
Copy link
Owner

shestakov commented Aug 25, 2021

@Selmirrrrr, could you please provide some more specifics on the task you're trying to accomplish?

Problem is, inserting and image in Word is actually two operations: adding a bitmap to a package and inserting [a reference to] the image into one or more places in text as a drawing. The latter implies specifying image output size and other transformations.

This means, that if you just need to replace an image with another one of the same size and position within the document, you just need to replace the bitmap resource behind it.

But if you also need to make some adjustments (like size on the page or alt text), you'll need to change all the drawings referencing the bitmap. In this case I'd suggest to just add the new image, insert it everywhere you need after or before the existing drawings, and then remove the old ones.

@shestakov
Copy link
Owner

Btw, if you have a Patreon or a GitHub Sponsor account, I'll be happy to sponsor this feature :)

Thank you very much, I appreciate it! But consider this project company backed.

I could maybe use some support one day, but only in case of adding something really heavy :)

@shestakov
Copy link
Owner

@Selmirrrrr, I've just pushed a test demonstrating image replacement.

This test replaces the image file within the document (you can find it if you unzip the .docx file). But it also finds all the drawings with images in the document, so you can make other adjustments and replacements I described above.

@Selmirrrrr
Copy link
Author

Great, thank you very much.

It works, it just has some strange behaviors with headers and footers, but I'll take some time to debug and understand what's going on.

Thanks again for everything :)

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

2 participants