Crop label image

Dear Team,
How to crop images (aspose word created jpg images )for label images using java?Kindly provide a solution as it is a very urgent need…
Using version Aspose Word :: 19.11 and Pdf :: 19.10
Attached my samples and expected output for your reference…
Input Samples and output::
images.zip (510.2 KB)

@resh05

The Aspose.Words.Layout namespace provides classes that allow to access information such as on what page and where on a page particular document elements are positioned, when the document is formatted into pages.

Please use layout API as shown below to achieve your requirement. Hope this helps you.

Document doc = new Document(MyDir + "input.docx");

LayoutCollector collector = new LayoutCollector(doc);
LayoutEnumerator enumerator = new LayoutEnumerator(doc);

enumerator.setCurrent(collector.getEntity(doc.getFirstSection().getBody().getLastParagraph()));
double y = enumerator.getRectangle().getY();
doc.getFirstSection().getPageSetup().setPageHeight(y);

doc.updatePageLayout();
doc.save(MyDir + "output.png");

Hi @tahir.manzoor
I tried the above code… but unfortunately the output generated doesn’t fulfill my requirement.

The input you mentioned in the above code is loaded using document. But my output is jpg image which is generated using aspose words.
Requirement : To crop the jpg image and save it as jpg image…
Kindly help…
Attached samples for your reference ::
images.zip (28.2 KB)
Thanks…

@resh05

Aspose.Words does not provide API to crop the image.

When you extract the content from Word document and save it as PNG, you can use the code shared in my previous post to generate the expected output PNG.

If you want to use PNG as input, you can use Aspose.Imaging. Please post your query in Aspose.Imaging forum where you will be guided appropriately.

@resh05,

You can build logic on the following code of Aspose.Words for Java API to be able to crop images:

Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);

BufferedImage img = ImageIO.read(new File("E:\\Temp\\Images\\input (1).jpg"));
Shape croppedImage = builder.insertImage(img, 72 * 5.57, 72 * 2.76);

if (croppedImage.getWidth() / ConvertUtil.pixelToPoint(img.getWidth()) < 1)
    croppedImage.getImageData().setCropRight(1 - croppedImage.getWidth() / ConvertUtil.pixelToPoint(img.getWidth()));

if (croppedImage.getHeight() / ConvertUtil.pixelToPoint(img.getHeight()) < 1)
    croppedImage.getImageData().setCropBottom(1 - croppedImage.getHeight() / ConvertUtil.pixelToPoint(img.getHeight()));

croppedImage.getImageData().setCropTop(0.1);
croppedImage.getImageData().setCropLeft(0.17);
croppedImage.getImageData().setCropBottom(croppedImage.getImageData().getCropBottom() - 0.1);
croppedImage.getImageData().setCropRight(croppedImage.getImageData().getCropRight() - 0.17);

croppedImage.getShapeRenderer().save("E:\\Temp\\Images\\outCropped-Java.jpg", new ImageSaveOptions(SaveFormat.JPEG));

Attachments: input-and-cropped-Images.zip (28.8 KB)

Hi @awais.hafeez
Thanks for the code … but the code works for those kind of images, i have multiple number of images that needed to be cropped… Kindly help
Attached samples:
Sample.zip (523.6 KB)
Thanks…

@resh05,

Your Sample.zip contains many JPG images. Take “ch63_Fig0013.jpg” and “ch63_Fig0001.jpg” image files from “inputs” folder and try running the following code which will cut rectangular areas from the original images and save them to separate cropped JPG image files.

cropImage("E:\\Temp\\Image Cropper\\ch63_Fig0013.jpg",
        "E:\\Temp\\Image Cropper\\cropped-1.jpg",
        124, 90,
        570, 571);

cropImage("E:\\Temp\\Image Cropper\\ch63_Fig0001.jpg",
        "E:\\Temp\\Image Cropper\\cropped-2.jpg",
        276, 80,
        233, 241);

public static void cropImage(String inPath, String outPath,
                             int left, int top,
                             int width, int height) throws Exception {

    Document doc = new Document();
    DocumentBuilder builder = new DocumentBuilder(doc);

    BufferedImage img = ImageIO.read(new File(inPath));

    int effectiveWidth = img.getWidth() - width;
    int effectiveHeight = img.getHeight() - height;

    Shape croppedImage = builder.insertImage(img,
            ConvertUtil.pixelToPoint(img.getWidth() - effectiveWidth),
            ConvertUtil.pixelToPoint(img.getHeight() - effectiveHeight));

    double widthRatio = croppedImage.getWidth() / ConvertUtil.pixelToPoint(img.getWidth());
    double heightRatio = croppedImage.getHeight() / ConvertUtil.pixelToPoint(img.getHeight());

    if (widthRatio < 1)
        croppedImage.getImageData().setCropRight(1 - widthRatio);

    if (heightRatio < 1)
        croppedImage.getImageData().setCropBottom(1 - heightRatio);

    float leftToWidth = (float) left / img.getWidth();
    float topToHeight = (float) top / img.getHeight();

    croppedImage.getImageData().setCropLeft(leftToWidth);
    croppedImage.getImageData().setCropRight(croppedImage.getImageData().getCropRight() - leftToWidth);

    croppedImage.getImageData().setCropTop(topToHeight);
    croppedImage.getImageData().setCropBottom(croppedImage.getImageData().getCropBottom() - topToHeight);

    croppedImage.getShapeRenderer().save(outPath, new ImageSaveOptions(SaveFormat.JPEG));
}

Hope, this helps in achieving what you are looking for.

Hi @awais.hafeez
Thanks for the code it works fine… Is their any possibility/ workaround solution to automatically detect the image in jpg file and remove all whitespaces in the file…
or is their any way to automatically detect the image in jpg files and crop it…

Thanks…

@resh05,

I am afraid, automatically detecting the image in JPG file and removing all white-spaces surrounding it in the file seems not possible. You have to manually specify coordinates (x, y) and (width, height) of cutting rectangle if you want to crop the image by using Aspose.Words for Java.

Alternatively, you may try Aspose.Imaging for Java API to Crop, Rotate and Resize Images.

If we can help you with anything else here, please feel free to ask.

Hi @awais.hafeez
Thanks for the help…