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

Free Support Forum - aspose.com

Can't remove white background from png image


I convert my pdf file to png file.

public byte[] convertToImage(byte[] document) {
    try (ByteArrayInputStream in = new ByteArrayInputStream(document);
         ByteArrayOutputStream stampImage = new ByteArrayOutputStream()) {
        Document doc = new Document(in);

        Page p = doc.getPages().get_Item(1);

        // get the content boundaries
        com.aspose.pdf.Rectangle contentBBox = p.calculateContentBBox();
        double height = contentBBox.getHeight();
        double width = contentBBox.getWidth();

        // Create PngDevice object with particular resolution
        PngDevice PngDevice = new PngDevice(new Resolution(500));

        // Convert a particular page and save the image to stream
        PngDevice.process(doc.getPages().get_Item(1), stampImage);
        return = stampImage.toByteArray();
    } catch (Exception e) {
        throw new RuntimeException("Cannot convert to image", e );

This png file has white color background, but I want transpalent background. I do:

    private byte[] removeBackground(byte[] convertedImage) {

    try (InputStream targetStream = new ByteArrayInputStream(convertedImage);
         ByteArrayOutputStream out = new ByteArrayOutputStream()) {
        // For complete examples and data files, please go to https://github.com/Muhammad-Adnan-Ahmad/Aspose.Imaging-for-Java

        // Initialize variables to hold width & height values
        int width = 0;
        int height = 0;

        // Initialize an array of type Color to hold the pixel data
        Color[] pixels = null;

        // Create an instance of RasterImage and load a BMP image
        RasterImage raster = (RasterImage) Image.load(targetStream);
        // Store the width & height in variables for later use
        width = raster.getWidth();
        height = raster.getHeight();

        // Load the pixels of RasterImage into the array of type Color
        pixels = raster.loadPixels(new Rectangle(0, 0, width, height));

        // Create & initialize an instance of PngImage while specifying size and PngColorType
        PngImage png = new PngImage(width, height, PngColorType.TruecolorWithAlpha);
        // Save the previously loaded pixels on to the new PngImage
        png.savePixels(new Rectangle(0, 0, width, height), pixels);

        // Set TransparentColor property to specify which color to be rendered as transparent

        final PngOptions exportOptions = new PngOptions();
        // Save the result on disc
        png.save(out, exportOptions);
        return out.toByteArray();
    } catch (Exception e) {
        throw new RuntimeException("Cannot remove background");

But after work this two function I still have white background!


Can you please provide the source PDF and generated PNG file. Can you please also share that which API versions you have used on your end. I am assuming that you are using latest version.