Skip to content

Commit

Permalink
Incorrect images resize with ${imageFileId:WxH} in DOCX #80
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrey Subbotin committed Aug 30, 2018
1 parent 7552b9e commit e1412f5
Showing 1 changed file with 15 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -180,14 +180,21 @@ public void inlineToDocx(WordprocessingMLPackage wordPackage, Text text, Object
try {
Image image = new Image(paramValue, paramsMatcher);
if (image.isValid()) {
BinaryPartAbstractImage imagePart = BinaryPartAbstractImage.createImagePart(wordPackage, resolveTextPartForDOCX(text, wordPackage),
image.imageContent);
Inline inline = imagePart.createImageInline("", "", docxUniqueId1++, docxUniqueId2++, false);
ImageSize oldSize = imagePart.getImageInfo().getSize();
double widthExtent = (double) image.width / oldSize.getWidthPx();
double heightExtent = (double) image.height / oldSize.getHeightPx();
inline.getExtent().setCx((long) (inline.getExtent().getCx() * widthExtent));
inline.getExtent().setCy((long) (inline.getExtent().getCy() * heightExtent));
Part part = resolveTextPartForDOCX(text, wordPackage);
BinaryPartAbstractImage imagePart = BinaryPartAbstractImage.createImagePart(wordPackage, part, image.imageContent);
int originalWidth = imagePart.getImageInfo().getSize().getWidthPx();
int originalHeight = imagePart.getImageInfo().getSize().getHeightPx();

double widthScale = (double) image.width / (double) originalWidth;
double heightScale = (double) image.height / (double) originalHeight;
double actualScale = Math.min(widthScale, heightScale);

long targetWidth = Math.round(originalWidth * actualScale);
long targetHeight = Math.round(originalHeight * actualScale);

Inline inline = imagePart.createImageInline("", "", docxUniqueId1++, docxUniqueId2++,
XlsxUtils.convertPxToEmu(targetWidth), XlsxUtils.convertPxToEmu(targetHeight), false);

org.docx4j.wml.Drawing drawing = new org.docx4j.wml.ObjectFactory().createDrawing();
R run = (R) text.getParent();
run.getContent().add(drawing);
Expand Down

0 comments on commit e1412f5

Please sign in to comment.