Free Support Forum - aspose.com

Document.renderToScale Colors all messed up

When using the following code the rendered image of my Word doc shows up with a salmon color instead of white.

Document doc = new Document(fil);
PageInfo pageInfo = doc.getPageInfo(Integer.parseInt(pageNum) - 1);
Dimension pageSize = pageInfo.getSizeInPixels(Integer.parseInt(zoom) / 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.renderToScale(Integer.parseInt(pageNum) - 1, gr, 0, 0, Integer.parseInt(zoom) / 100f);
ImageIO.write(img, “JPEG”, response.getOutputStream());

I have attached a screen shot of the original file and the rendered file. Any idea why this is happening? FYI, it seems that setting the background has no effect.

Hi Keith,


Thanks for your inquiry. I have tested the scenario and have managed to reproduce the same problem on my side. For the sake of correction, I have logged this problem as WORDSJAVA-648 in our issue tracking system. We will further look into the details of this problem and will keep you updated on the status of correction. We apologize for your inconvenience.

Best Regards,

Hi Keith,


Thanks for your patience. While you’re waiting for the fix, please use the regular Document.save() method to be able to render Word documents to Image formats. Rest assured, we will inform you as soon as your issue is resolved.

Best Regards,

Hi Keith,


It is to update you that our development team has finished working on your issue (WORDSJAVA-648) and has come to a conclusion that your issue and the behaviour you’re observing is actually not a bug in Aspose.Words. So, we’ve closed this issue as ‘Not a Bug’. So, the solution would be to 1) specify BufferedImage.TYPE_INT_RGB in case you want to write to JPEG format 2) specify BufferedImage.TYPE_INT_ARGB in case you want to write to PNG format. Here is a sample code:
Document doc = new Document(“C:\Temp\Word+Sample.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_RGB);
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
.renderToScale(Integer.parseInt(“1”) - 1, gr, 0, 0, Integer.parseInt(“100”) / 100f);
ImageIO
.write(img, “JPEG”, new File(“C:\Temp\out.jpg”));
If we can help you with anything else, please feel free to ask.

Best Regards,