Tim
Tim

Reputation: 38

How to inject images into a Word template via docx4j without getting them resized

My program injects text and pictures into a Word template. This works great via content control data binding (thanks to docx4j and Content-Control-Toolkit).

My problem is, that images get resized after injection. What I actually want, is the behavoir that Jason decribed here: http://www.docx4java.org/forums/data-binding-java-f16/picture-content-control-size-t634.html

The current behaviour is to just let it be whatever its natural size is (at a given dpi), unless that is greater than page width, in which case it is scaled down.

According to that post, the behavoir of docx4j has been changed so that the pictures always fit the size of the content control with respect to the ratio. Is it possible to get the "old" behavoir back? Do I have to do that on my own, or is the switch, that Jason wrote about, already implemented?

As the answer to How to force Docx4j to refresh a replaced image file states, the size of a picture is stored in the main document part. At the moment, I only use XPath to set content in the custom XML part. If there is any possibility to get what I need without touching the documents XML directly, I would really prefer that. A macro to set the size after opening the document in Word is no option for me.

Upvotes: 0

Views: 1359

Answers (1)

JasonPlutext
JasonPlutext

Reputation: 15878

The first thing to be aware of is that these days we prefer to have a picture in a rich text content control, as opposed to a picture content control.

This is because Word limits your ability to "float" a picture content control.

The handling for this is triggered by w:tag containing 'od:Handler=picture': datastorage/bind.xslt#L165

The basic behaviour is that if the w:sdtContent contains an existing w:drawing/wp:inline/a:graphic then reuse it, so any formatting thus configured is used.

But for a "legacy" picture content control which doesn't contain a:blip (when would this be?), xpathInjectImage is invoked with wp:extent passed in (see bind.xslt#L240).

At line 1143, if (cxl==0 || cyl==0) // Let BPAI work out size

So if you want the image at its natural size, you could try removing the when clause at bind.xslt#L212

By the way, we can also bind escaped XHTML. But there, we make an effort to fit any image not just to the page width, but if in a table cell, to that as well.

Upvotes: 1

Related Questions