Free Support Forum - aspose.com

Crop Image in Word File

Hello,

I have a problem cropping an image in Aspose.Words 18.5 for Java. Load a document and try to crop all images for example with factor -0.5.

The image gets compressed and not cropped. How can I just cut off the image on the right side?

Best Regards

Example:
[…loadDocument…]
NodeCollection childNodes = doc.getChildNodes(NodeType.SHAPE, true);
if (childNodes != null) {
Iterator imageIt = childNodes.iterator();
while (imageIt.hasNext()) {
Shape image = imageIt.next();
if (!image.hasImage()) {
continue;
}
image.getImageData().setCropRight(-0.5);
}
}

@dvtdaten,

Thanks for your inquiry. Please ZIP and attach your input Word document here for testing. We will investigate the issue on our side and provide you more information.

I attached a zip-File with example, we convert this to mhtml, crop the images (in example with -0.5) and export this to pdf.

In the Zip-File you will find the msg-file, mhtml-file and finally pdf-file + code.

Thanks in advance

example.zip (4.3 MB)

@dvtdaten,

Thanks for sharing the detail. We have tested the scenario and have not found the shared issue. Please execute the code example with and without using ImageData.CropRight property and compare the output PDF files to check the difference.

If you want to fit the image into page, you need to resize the image according to page size.

I checked this already. Without using ImageData.CropRight I get images which are too large and exceed the border on the right. We want to crop the images, so that there is a border on the right side. What is your output? Can you attach it? My output is in the ZIP and has images width wrong dimensions. There is no cropping done. The image is resized and gets compressed in width.

We do NOT want to resize the images. We want to cut it on the right side.

Thanks for further help

@dvtdaten,

Thanks for your inquiry. Please note that negative values will result in the picture being squeezed inward from the edge being cropped. The positive values less than 1 will result in the remaining picture being stretched to fit the shape.

We have logged a feature request as WORDSNET-16929 in our issue tracking system to crop the image from the right side. You will be notified via this forum thread once this feature is available. We apologize for your inconvenience.

Thanks for logging the feature request.
If I crop an image I want to cut it off (like word does) and not to squeeze it.

@dvtdaten,

We will inform you via this forum thread once there is any update available on this feature.

@dvtdaten,

Thanks for your patience. Please use the following code example to get the desired output. Please change the value cropPercent according to your requirement.

Hope this helps you. Please let us know if you have any more queries.

Document doc = new Document(MyDir + "große bilder.msg.mhtml");
double cropPercent = 0.8;
bool isAspectRatioLocked;
 
 
foreach (Shape shape in doc.GetChildNodes(NodeType.Shape, true))
{
   shape.ImageData.CropRight = cropPercent;
 
   isAspectRatioLocked = shape.AspectRatioLocked;
   shape.AspectRatioLocked = false;
 
   shape.Width -= shape.Width * cropPercent;
 
   shape.AspectRatioLocked = isAspectRatioLocked;
}
 
 
PdfSaveOptions options = new PdfSaveOptions();
options.Compliance = PdfCompliance.PdfA1a;
doc.Save(MyDir + "crop.pdf", options);

Thanks for the reply! I found a solution like this, the aspectRatio was the point which I missed in my implementation.

@dvtdaten,

Thanks for your feedback. We are closing this issue WORDSNET-16929. Please let us know if you have any more queries.