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

Free Support Forum - aspose.com

Doc to Png Image color issue

I am using below code to get png image from doc. I need to set the generated png image size. However the colors look off. The image generated comes in black color.

    Document document = new Document(this.inputStream);
        options.setPageIndex(this.pageNum);
        options.setPageCount(1);
       
        PageInfo pageInfo = document.getPageInfo(this.pageNum);
       
        BufferedImage img = new BufferedImage(1000, 1000, BufferedImage.TYPE_INT_ARGB);
        Graphics2D gr = img.createGraphics();
        document.renderToSize(this.pageNum, gr, 0f, 0f, 1000, 1000);
        File file = new File(FilenameUtils.getBaseName(this.fileName)
                + Constants.MEDIUM_SUFFIX + FILE_EXTENSION);
        ImageIO.write(img, Constants.FileType.PNG.getValue(), file);
        return file;

Is this correct way of resizing. How to keep the color of the original document in generated image
Please advise

@hardeep17

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.

aspose.zip (127.2 KB)
Here are few files I tried

@hardeep17

Thanks for sharing the detail. We have tested the scenario using the latest version of Aspose.Words for Java 19.2 with following code example and have not found the shared issue. Please use Aspose.Words for Java 19.2.

Document document = new Document(MyDir +"Setup Flyway for new services.docx");

PageInfo pageInfo = document.getPageInfo(0);
BufferedImage img = new BufferedImage(1000, 1000, BufferedImage.TYPE_INT_ARGB);

Graphics2D gr = img.createGraphics();
document.renderToSize(0, gr, 0f, 0f, 1000, 1000);

File file = new File(MyDir + "out.png");
ImageIO.write(img, "png", file);

Thanks for your response.After setting color of bufferedimage, i was able to get correct colors. However, is there any way to set quality of the image

@hardeep17

Thanks for your inquiry. In this case, we suggest you please use ImageSaveOptions as shown below to get the desired output. Hope this helps you.

Document doc = new Document(MyDir +"Setup Flyway for new services.docx");
ImageSaveOptions options = new ImageSaveOptions(SaveFormat.PNG);
options.setPageIndex(0);
options.setResolution(300);

doc.save(MyDir + "out.png", options);

Since i am using document.renderToSize and passing graphics object to it and not document.save. How do I set resolution in that case

@hardeep17

Thanks for your inquiry. You cannot set the resolution of image using Document.renderToSize method. However, you can get the good quality image using following code example.

Document doc = new Document(MyDir +"Setup Flyway for new services.docx");

PageInfo pageInfo
        = doc.getPageInfo(Integer.parseInt("1") - 1);

Dimension pageSize = pageInfo.getSizeInPixels(Integer.parseInt("100") / 100f, 96.0f);

BufferedImage img = new BufferedImage((int)pageSize.getWidth(),

        (int)pageSize.getHeight(),

        BufferedImage.TYPE_INT_ARGB);

Graphics2D gr = img.createGraphics();

gr.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);

gr.setBackground(Color.white);

gr.setColor(Color.white);

gr.fillRect(0, 0, (int)pageSize.getWidth(), (int)pageSize.getHeight());

doc.renderToSize(0, gr, 0f, 0f, 1000, 1000);
ImageIO.write(img, "PNG", new File(MyDir+ "Rendering.RenderToSize Out.png"));