When using aspose slides to get the slide size, we get different size depending on the document. How to we set the slide resolution to render it to our specific size.
For example, In the below code,
Presentation pres = new Presentation("pres.pptx");
IRenderingOptions renderOptions = new RenderingOptions();
Dimension2D slideSize = pres.getSlideSize().getSize();
ISlide slide = pres.getSlides().get_Item(pageNum);
SlideSize returns the width:height as “960.0:540.0” for one document and “720.0:540.0” for another document.
I want to set the resolution to render so that any document can be rendered to the graphics object correctly .
For example,
public void Graphics(Graphics2D graphics)
{
Presentation pres = new Presentation("pres.pptx");
IRenderingOptions renderOptions = new RenderingOptions();
Dimension2D slideSize = pres.getSlideSize().getSize();
ISlide slide = pres.getSlides().get_Item(pageNum);
slide.render(renderOptions, graphics);
}
If the documents are of different size, the rendered graphics object is cut off for some documents or having extra width for smaller width documents. Is there a way to set the dpi or resolution for the slides?
We have opened the following new ticket(s) in our internal issue tracking system and will consider the question according to the terms mentioned in Free Support Policies.
Issue ID(s): SLIDESJAVA-39412
You can obtain Paid Support Services if you need support on a priority basis, along with the direct access to our Paid Support management team.
Hi Andrew, Thanks for providing the above code. But we want to render the slide image to a existing graphics object. We tried the above code with our existing graphics object but we dont see the images rendered correctly.
@mirnalini,
Thank you for the code snippets. Unfortunately, we don’t know what was done to the Graphics object (transformations, for example) that you pass to the method. Could you kindly share the simplest standalone project that reproduces the problem?
In aspose words, we have getSizeinPixels which provides the Dimension required and renderToSize using the below code works perfectly. But we are not able to find similar methods for aspose slides.
Dimension thumbSize = doc.getPageInfo(pageNum).getSizeInPixels(scale, 72);
int imgWidth = (int) (thumbSize.getWidth());
int imgHeight = (int) (thumbSize.getHeight());
doc.renderToSize(pageNum - 1, gr, 0, 0, imgWidth, imgHeight); - gr is the graphics context to which we want to render our document page
Also we see that aspose words has getWidthinPoints for each page. But for aspose slides, we are not able to get the slide width and height. We can only get for the presentation size. If we have slides of varying size, how does aspose set its slide width and height.
@mirnalini,
Thank you for your patience. You are creating images and Graphics2D objects that are 720 x 540, whereas presentation slides are 960 x 540, so the images appear cropped. Please try resizing your slides before rendering like this:
Thank you for the code. We are aware that when we set the size, we can render it correctly which is what we have done in the first render method in our standalone code. Let me rephrase my question.
If i have slides of different height and width, this setSize option when applied to all the slides, we see image cut off when rendering to the same graphics object .
Can you please answer my below question.
In aspose words, we have getSizeinPixels which provides the Dimension required and renderToSize using the below code works perfectly. But we are not able to find similar methods for aspose slides.
Dimension thumbSize = doc.getPageInfo(pageNum).getSizeInPixels(scale, 72);
int imgWidth = (int) (thumbSize.getWidth());
int imgHeight = (int) (thumbSize.getHeight());
doc.renderToSize(pageNum - 1, gr, 0, 0, imgWidth, imgHeight); - gr is the graphics context to which we want to render our document page
Also we see that aspose words has getWidthinPoints for each page. But for aspose slides, we are not able to get the slide width and height. We can only get for the presentation size. If we have slides of varying size, how does aspose set its slide width and height.
@mirnalini,
Aspose.Slides returns the size of slides in points with 72 DPI. You can convert the size to any other size. I offered you the solution above. Unfortunately, I don’t have any additional information.
Thank you for confirming about dpi . Can you also confirm us what is the resolution of the slides when using renderToGraphics method?
Also what happens inside renderToGraphics method when the below method is invoked.
If i have a document of width and height : 720 * 540 and another document of width and height 960 * 720 and both has to be rendered to the same graphics object , what approach we can take?
I see that there is a rendertographics with scale parameters , but we see a image cut off when rendered to graphics object.
slide.renderToGraphics(renderoptions, graphics, new Dimension(width, height));
@mirnalini,
Unfortunately, I don’t have answers to your questions but we found that you mixed up the method parameters in the sample project. We fixed it and now everything should work fine:
public static void renderRenderToGraphicstoslideSize(Presentation pres)
{
System.out.println("start");
Dimension2D slideSize = pres.getSlideSize().getSize();
System.out.println("slide size render1 :" + slideSize.getWidth() + ":" + slideSize.getHeight());
// Creates a BufferedImage with the slide size
BufferedImage slideImage = new BufferedImage((int) slideSize.getWidth(), (int) slideSize.getHeight(), BufferedImage.TYPE_INT_RGB);
int w = (int) slideImage.getWidth();
int h = (int) slideImage.getHeight();
java.awt.Graphics2D graphics = slideImage.createGraphics();
graphics.setColor(Color.red);
try
{
for (int index = 0; index < pres.getSlides().size(); index++)
{
ISlide slide = pres.getSlides().get_Item(index);
slide.renderToGraphics(new RenderingOptions(), graphics, new Dimension(w, h));
ImageIO.write(slideImage, "png", new File(folderPath+"render1slide" + index + ".png"));
}
System.out.println("stop");
}
catch(Exception ex)
{
ex.printStackTrace();
}
}
public static void render2RendertoGraphicswithDimension(Presentation pres)
{
System.out.println("start");
Dimension2D slideSize = pres.getSlideSize().getSize();
System.out.println("slide size render1 :" + slideSize.getWidth() + ":" + slideSize.getHeight());
// our graphics context is of the below dimension
BufferedImage slideImage = new BufferedImage(720, 540, BufferedImage.TYPE_INT_RGB);
int w = (int) slideImage.getWidth();
int h = (int) slideImage.getHeight();
java.awt.Graphics2D graphics = slideImage.createGraphics();
graphics.setColor(Color.red);
try
{
for (int index = 0; index < pres.getSlides().size(); index++)
{
ISlide slide = pres.getSlides().get_Item(index);
slide.renderToGraphics(new RenderingOptions(), graphics, new Dimension(w, h));
ImageIO.write(slideImage, "png", new File(folderPath+"render2slide" + index + ".png"));
}
System.out.println("stop");
}
catch(Exception ex)
{
ex.printStackTrace();
}
}
public static void renderGetThumbnail(Presentation pres)
{
System.out.println("start");
Dimension2D slideSize = pres.getSlideSize().getSize();
System.out.println("slide size render1 :" + slideSize.getWidth() + ":" + slideSize.getHeight());
// our graphics context is of the below dimension
BufferedImage slideImage = new BufferedImage(720, 540, BufferedImage.TYPE_INT_RGB);
int w = (int) slideImage.getWidth();
int h = (int) slideImage.getHeight();
java.awt.Graphics2D graphics = slideImage.createGraphics();
graphics.setColor(Color.red);
try
{
for (int index = 0; index < pres.getSlides().size(); index++)
{
ISlide slide = pres.getSlides().get_Item(index);
BufferedImage image = slide.getThumbnail(new Dimension(w, h));
graphics.drawImage(image, null, 0, 0);
ImageIO.write(slideImage, "png", new File(folderPath+"render3slide" + index + ".png"));
}
System.out.println("stop");
}
catch(Exception ex)
{
ex.printStackTrace();
}
}
Please also note that the renderToGraphics methods will be removed in version 24.8.
If renderToGraphics method is removed, What will be the alternate we would get? If we need to use BufferedImage, drawing it to graphics object is causing out of memory errors. Can you please provide more information on this?