Setting desired width or height for image rendering?

Hi!


How to set a desired width/height when rendering a cells sheet, assuming setOnePagePerSheet(true) is used ?

Code snippet:

final int desiredWidth = getMyDesiredWidth();
final int desiredHeight = getMyDesiredHeight();

final InputStream is = getMyDocumentStream();
final Workbook doc = new Workbook(is);
final WorksheetCollection sheets = doc.getWorksheets();
final Worksheet sheet = sheets.get(0);

// AT THIS POINT, ho to set the resolution according to desiredWidth/desiredHeight ?
imgOptions.setHorizontalResolution(???);
imgOptions.setVerticalResolution(???);

final ImageOrPrintOptions imgOptions = new ImageOrPrintOptions();
imgOptions.setImageFormat(ImageFormat.getPng());
imgOptions.setOnePagePerSheet(true);

final SheetRender sr = new SheetRender(sheet, imgOptions);
final ByteArrayOutputStream out = new ByteArrayOutputStream();
sr.toImage(imageIndex, out);

Thanks for any hint!

Hi,


Well, the output image’s width and height is actually decided by the PageSetup’s paper size. If you set OnePagePerSheet to true, it is decided by the sheet’s content only.

So, if you want to get your desired width and height for the image, I am afraid, you have to use your own code. For example, you may try to redraw the output image by using:
e.g
Graphic2D.drawImage(img, dx1, dy1, dx2, dy2, sx1, sy1, sx2, sy2, observer) or any other JAVA APIs to accomplish the task.

Thank you.
Amjad Sahi:
Hi,
If you set OnePagePerSheet to true, it is decided by the sheet's content only.

Is there a way to get the sheet's content estimated size ? Having the exact width/height is not important, but having a good approximation is.

I could use Graphic2D.drawImage() to resize the image, but it would not be good for large dimensions.

Hi,

Thanks for your posting and using Aspose.Cells.

Please use SheetRender.getPageSize(pageIndex) method for your needs. Let us know if you have any other question, we will look into it and help you asap.

mshakeel.faiz:
Hi,
Please use SheetRender.getPageSize(pageIndex) method for your needs. Let us know if you have any other question, we will look into it and help you asap.

Thank you.

My real question was actually to know if I can know the optimal resolution (ie. set ImageOrPrintOptions.setHorizontalResolution() and ImageOrPrintOptions.setVerticalResolution()) before creating SheetRender, because after creating SheetRender it is too late (ie. resolution can not be changed, unless I create another SheetRender object)

This would probably require a Worksheet.getPageSize() function of equivalent, returning a size in inchs or in pica points, and I would then do simple maths to set the desired number of pixels per inch (or per dots).

As a temporary workaround, I can try to set a default resolution (to 150 dpi for example), ask SheetRender.getPageSize(), and then recreate another SheetRender object with proper settings - but this will cost more, I suppose ?

EDIT: This solution does not appear to work ; setting a different resolution only produce a gray image

Note: by the way, it appears that SheetRender.getPageSize() does not give the correct size ; eg:
getPageSize() => 497.92398x1771.3033
toImage() => 664x2362

To get the correct size, it seems that using:
Math.round(com.aspose.words.ConvertUtil.pointToPixel(pageSize[0]))
provides the correct answer (almost, sometimes with 1 pixel difference).

This is a bit confusing to me - ConvertUtil.pointToPixel(double) converts "points to pixels at 96 dpi", even if I am using another dpi settings

Hi,

Thanks for your posting and using Aspose.Cells.

We will look into your issue in detail and help you asap. Please let us know if such a solution is available in Aspose.Words. Could you please post a runnable sample code of Aspose.Words showing this solution for our reference which we could run at our end and observe it? It will help us look into your requirements more closely and precisely and we will be able to provide a similar solution for your needs.

Thanks for your cooperation.

Hi,

With Aspose.Words, I do the following:

final InputStream is = myStream();
final Document doc = new Document(is);

final BufferedImage image = new BufferedImage(renderWidth, renderHeight, BufferedImage.TYPE_INT_ARGB);
final Graphics2D gr = image.createGraphics();
gr.setColor(Color.WHITE);
gr.fillRect(0, 0, image.getWidth(), image.getHeight());
gr.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);

doc.renderToSize(page, gr, 0, 0, renderWidth, renderHeight);
...

And with Aspose.Slides, I do the following:

final InputStream is = myStream();
final Presentation doc = new Presentation(is);
final List listSlides = getListSlidesSorted(doc);
final ISlide slide = listSlides.get(0);

final Dimension size = new DimensionUIResource(renderWidth, renderHeight);
final BufferedImage image = slide.getThumbnail(size);
...

Unfortunately, it appears there is no "renderToSize" equivalent for Aspose.Cells.


Hi,

Thanks for your sample code and using Aspose.Cells.

The sample code you provided clarifies your requirements. We will test this sample code and logged a New Feature request in our database to support this feature. We will update you asap.

Thank you. Can you provide the ID number of the feature request if possible for tracking purpose ?


Thanks!

Hi,

Thanks for your posting and using Aspose.Cells.

We were able to generated image of Microsoft Word page with desired width and height using Aspose.Words using the following code. I have attached the output images and source word document used in the code for a reference.

We will support the similar feature for worksheet image if possible. We have therefore logged a New Feature request in our database to investigate and implement this feature. Once, the feature is supported or we have some other update for you, we will let you know asap.

This issue has been logged as

  • CELLSJAVA-41235 - Support RenderToSize API for Worksheet Image

Java

String filePath = “F:\Shak-Data-RW\Downloads\sample.docx”;


int renderWidth=400;

int renderHeight = 400;


Document doc = new Document(filePath);


BufferedImage image = new BufferedImage(renderWidth, renderHeight, BufferedImage.TYPE_INT_ARGB);

final Graphics2D gr = image.createGraphics();

gr.setColor(Color.WHITE);

gr.fillRect(0, 0, image.getWidth(), image.getHeight());

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


doc.renderToSize(0, gr, 0, 0, renderWidth, renderHeight);


ImageIO.write(image, “PNG”, new File(“Image.400x400.png”));


Hi,


I am also interested in this functionality. When it is delivered will it also be available for .NET?

Thanks

Hi,

Thanks for your posting and considering Aspose.Cells.

We have added the same New Feature request in our database for .NET version. So, now once it is implemented, it will be available for both platforms (.NET and Java).

This issue has been logged as

  • CELLSNET-43469 - Support RenderToSize API for Worksheet Image

Hi,

Thanks for your posting and using Aspose.Cells.

Please download and try the latest fix: Aspose.Cells for .NET v8.4.0.1 and let us know your feedback.

We have added new API: SetDesiredSize(int desiredWidth, int desiredHeight) in
ImageOrPrintOptions

Hi,


Please try our latest version/fix: Aspose.Cells for Java v8.4.0.2 (attached)
We have also fixed the issue for JAVA version, we include the similar API in the JAVA version.

Let us know your feedback.

Thank you.


The issues you have found earlier (filed as CELLSNET-43469) have been fixed in this update.


This message was posted using Notification2Forum from Downloads module by Aspose Notifier.

The issues you have found earlier (filed as CELLSJAVA-41235) have been fixed in this update.


This message was posted using Notification2Forum from Downloads module by Aspose Notifier.