Page 1 of 1

Copy media image from one HeaderPart to another

PostPosted: Fri Feb 22, 2019 12:05 am
by dermaga
I want to copy the image resource which is typically in the docx word/media folder to another docx headerpart.

At the moment I have to store the image as a resource in my project and retrieve it to add it to the HeaderPart like this:

Code: Select all
BinaryPartAbstractImage.createImagePart(wordMLPackage,
            headerPart, BufferUtil.getBytesFromInputStream(WordX.class.getResourceAsStream("myimage.png")));


But I have an template docx file with a template HeaderPart and I woud like to copy the image out of the template part and add it to my main HeaderPart.

How could I copy the image resource from the template headerpart media folder to my main HeaderPart?

Re: Copy media image from one HeaderPart to another

PostPosted: Mon Feb 25, 2019 7:18 pm
by dermaga
Found solution:

Code: Select all
final Part part = headerTemplate.getRelationshipsPart().getRelationships().getRelationship().stream()
            .filter(rs -> headerTemplate.getRelationshipsPart().getPart(rs) instanceof BinaryPartAbstractImage)
            .findFirst().map(rs -> headerTemplate.getRelationshipsPart().getPart(rs)).get();

        BinaryPartAbstractImage.createImagePart(wordMLPackage,
            headerPart, ((BinaryPartAbstractImage)part).getBytes());