We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Is it possible to remove white space from PNG image?

Hi Andrey

Thank you for your reply.

I need to extract all the shapes from the table inserted in Doc file. Extracted shapes will be stored in PNG format in the database . while generating the report ,PNG Image should be placed in table cell but png image size will not be fit into the table cells size because PNG image is big in size.

Please let me know is it possible to reduce or remove white space from PNG image through aspose Api

Please find the attached input and output file.
input.png (99.1 KB)
output.png (78.8 KB)

@Ramya_D You can use ShaepRenderer to save individual shape as an image. In the ShapeRenderer.save method you can pass ImageSaveOption, where you can use GraphicsQualityOptions, which can affect the resulting image size.

1 Like

Hi Team,
Thank you for your response.

I have written code but could not reach my requirement. Can you please help me out with java code (White space should not come while saving shape in PNG format).

Thanks in advance.

@Ramya_D Could you please attach your input document, code and expected output? We will check and provide you more information.

Hi Alexey,

Please check my code.

Image image = convertShapeToImage(shapeObj);
BufferedImage bufferedImage = crop((BufferedImage) image);
ImageIO.write( bufferedImage, "png", imageFile);
public static BufferedImage crop(BufferedImage image) {

	    int width = image.getWidth();
	    int height = image.getHeight();

	    int top = height / 2;
	    int bottom = top;

	    int left = width / 2 ;
	    int right = left;

	    for (int x = 0; x < width; x++) {
	        for (int y = 0; y < height; y++) {
	            if (isFg(image.getRGB(x, y))){

	                top    = Math.min(top, y);
	                bottom = Math.max(bottom, y);

	                left   = Math.min(left, x);
	                right  = Math.max(right, x);


	    return image.getSubimage(left, top, right - left, bottom - top);

I have written code to crop PNG image file .But it is not right way to reach my requirements(Quality of image will be gone and PNG file size will be large)

My requirement is while saving shape in PNG format I have to avoid white space , PNG file should contains only shape.

Please find the attached input and expected output file.

Thanks in advanceinput (2).png (6.1 KB)
output.png (20.3 KB)

@Ramya_D Please attach your input MS Word document here for testing and also, please, provide source of convertShapeToImage method you use.

 Hi Alexey,

public static Image convertShapeToImage(ShapeObject shapeObj) {
Document document = new Document();
Section section = document.addSection();
Image image = document.saveToImages(0, ImageType.Bitmap);
return image;
inputDoc.docx (32.8 KB)

@Ramya_D I think, in your case it would be easier to use ShapeRenderer to render shapes to image. For example see the following code:

Document doc = new Document("C:\\Temp\\inputDoc.docx");

Iterable<Shape> shapes  = (Iterable<Shape>)doc.getChildNodes(NodeType.SHAPE, true);
int i = 0;
for(Shape s : shapes)
    s.getShapeRenderer().save("C:\\Temp\\out\\shape_"+i+".png", new ImageSaveOptions(SaveFormat.PNG));