Aspect Ratio + Java Slides

Hi,

I have read a couple of posts on the aspect ratio and wanted additional help.

I have a couple of images that I am adding to the slides, now, is there a way in Aspose.Slides, that I can send the image and it would help me calculate the aspect ration of the page to the size of the slide?

Regards,
Vivek

Hi Vivek,

Thanks for your interest in Aspose.Slides.

I like to share that Aspse.Slides for Java provides the slide width and height and by using simple division, we can extract the aspect ratio of the slide. Similarly, when we add the image using Aspose.Slides, we also have access to image width and height and we can calculate the aspect ratio of the image as well. Please use the sample code snippet given below to add a sample image in presentation slide. The aspect ratio for both slides and image is calculated in the following code snippet. Please share with us, if you may feel any issue while using the code snippet.

//Instantiate a Presentation object that represents a PPT file
Presentation pres = new Presentation();

//Accessing a slide using its slide position
Slide slide = pres.addEmptySlide();

//Creating a picture object that will be used to fill the ellipse
com.aspose.slides.Picture pic = new com.aspose.slides.Picture(pres, new FileInputStream(new File ("D:\\Aspose Data\\Desert.jpg") ));
//Adding the picture object to pictures collection of the presentation
//After the picture object is added, the picture is given a uniqe picture Id
int picId = pres.getPictures().add(pic);

//Calculating picture width and height
int pictureWidth = (int)pres.getPictures().get(picId - 1).getWidth() * 4;
int pictureHeight = (int)pres.getPictures().get(picId - 1).getHeight() * 4;

//Calculating slide width and height
int slideWidth = slide.getBackground().getWidth ();
int slideHeight = slide.getBackground().getHeight();

//Getting image Aspect Ratio
double ImageAR=pictureWidth/pictureHeight;

//Getting Slide Aspect Ratio
double SlideAR=slideWidth/slideHeight;

//Calculating the width and height of picture frame
int pictureFrameWidth = (int)(slideWidth / 2 - pictureWidth / 2);
int pictureFrameHeight = (int)(slideHeight / 2 - pictureHeight / 2);

//Adding picture frame to the slide
slide.getShapes().addPictureFrame(picId, pictureFrameWidth, pictureFrameHeight,pictureWidth, pictureHeight);
//Writing the presentation as a PPT file
pres.write("D:\\modifiedjava.ppt");

Thanks and Regards,

Thanks Mudassir for the info, another question I had was how to I get the picture details the same you mentioned for a PPTX?

Hi Vivek,

Please use the following code snippet to get the Aspect ratio for image and slides in PPTX presentation.

//Instantiate a Presentation object that represents a PPT file
PresentationEx pres = new PresentationEx();

//Accessing a slide using its slide position
SlideEx slide = pres.getSlides().get(0);

//Creating a picture object that will be used to fill the ellipse
BufferedImage img= ImageIO.read(new File ("D:\\Aspose Data\\Desert.jpg"));
//Instantiate the ImageEx class
ImageEx imgx = pres.getImages().addImage(img);

//Calculating picture width and height
int pictureWidth = (int)img.getWidth ()* 4;
int pictureHeight = (int)img.getHeight() * 4;

//Calculating slide width and height
int slideWidth = pres.getSlideSize().getSize().width;
int slideHeight = pres.getSlideSize().getSize().height;

//Getting image Aspect Ratio
double ImageAR=(double)pictureWidth/pictureHeight;

//Getting Slide Aspect Ratio
double SlideAR=(double)slideWidth/slideHeight;

//Calculating the width and height of picture frame
int pictureFrameWidth = (int)(slideWidth / 2 - pictureWidth / 2);
int pictureFrameHeight = (int)(slideHeight / 2 - pictureHeight / 2);

//Adding picture frame to the slide
slide.getShapes().addPictureFrame(ShapeTypeEx.RECTANGLE, pictureFrameWidth, pictureFrameHeight,pictureWidth, pictureHeight,imgx);

//Writing the presentation as a PPTX file
pres.write("D:\\modifiedjava.pptx");

Thanks and Regards,

Hi,



In the code, we are calculating the pictureHeight & width, why are we multiplying it by 4?



What if I want get the original size of the image and compare with the size of the slide and then accordingly set the height & width and then insert (using aspect ratio), how to do this?



Apart from this, can I add audio, video, flash, objects into a PPT/PPTX, where can I find the examples or documentation for the same.



Regards,

Vivek

Hi Viviek,

Please find the code snippet below in which I have set the image size w.r.t aspect ratio of original image size. The multiplication factor was actually used for some scaling and it is not necessary. I have also added the code snippet for adding audio and video frames. I regret to share that support for flash objects is not available in Aspose.Slides for Java at the moment.

//Instantiate a Presentation object that represents a PPT file
PresentationEx pres = new PresentationEx();

//Accessing a slide using its slide position
SlideEx slide = pres.getSlides().get(0);

//Creating a picture object that will be used to fill the ellipse
BufferedImage img= ImageIO.read(new File ("D:\\Aspose Data\\Desert.jpg"));
//Instantiate the ImageEx class
ImageEx imgx = pres.getImages().addImage(img);


//Calculating picture width and height
int pictureWidth = (int)img.getWidth ();
int pictureHeight = (int)img.getHeight();

//Calculating slide width and height
int slideWidth = pres.getSlideSize().getSize().width;
int slideHeight = pres.getSlideSize().getSize().height;

//Getting image Aspect Ratio
double ImageAR=(double)pictureWidth/pictureHeight;

//Getting Slide Aspect Ratio
double SlideAR=(double)slideWidth/slideHeight;

//Calculating the width and height of picture frame
int pictureFrameWidth = (int)(slideHeight *ImageAR);
int pictureFrameHeight = (int)(slideWidth * (1/ ImageAR));

//Adding picture frame to the slide
// slide.getShapes().addPictureFrame(picId, pictureFrameWidth, pictureFrameHeight,pictureWidth, pictureHeight);
slide.getShapes().addPictureFrame(ShapeTypeEx.RECTANGLE, pictureFrameWidth, pictureFrameHeight,pictureWidth, pictureHeight,imgx);

//Adding Audio Frame
int Index = pres.getSlides().addEmptySlide (pres.getLayoutSlides().get (0));
slide=pres.getSlides ().get(Index);

//Load the wav sound file to stram
final FileInputStream fis = new FileInputStream("d:\\Aspose Data\\Sound.wav");

//Add Audio Frame
AudioFrameEx af = slide.getShapes().addAudioFrameEmbedded(50, 150, 100, 100, fis);

//Set Play Mode and Volume of the Audio
af.setPlayMode( AudioPlayModePresetEx.AUTO);
af.setVolume(AudioVolumeModeEx.LOUD);
fis.close();


//Adding Video Frame
Index = pres.getSlides().addEmptySlide (pres.getLayoutSlides().get (0));
slide=pres.getSlides ().get(Index);

//Add Video Frame
java.lang.String videoPath="D:\\Aspose Data\\SEARCH.AVI";
VideoFrameEx vf = slide.getShapes().addVideoFrame(50f, 150f, 100f, 100f, videoPath);

//Set Play Mode and Volume of the Audio
vf.setPlayMode( VideoPlayModePresetEx.AUTO);
vf.setVolume(AudioVolumeModeEx.LOUD);

//Writing the presentation as a PPTX file
pres.write("D:\\modifiedjava.pptx");

Thanks and Regards,

Thanks for the information, I shall try it out and get back if there is any problem.



1. Is it not possible to create a generic object and add it to the presentation, since I could be adding an Image/Video/Audio and those could be of different formats like jpg/jpeg/gif/bmp/avi/wmv/mp4/acc/flac, etc.

2. What are the formats supported and is there a generic way to do the same?



Update status



3. The image and slides sizes are not checked and sometimes the image size is larger than the slide size

4. When I populate the image, it does not go into the slide, its put into some location, when I open the created ppt, the image location is about 600-700px below the slide



Could you please help me answer/solve the above mentioned problems?



Thanks & Regards,

Vivek

Hi Vivek,

I regret to share that unfortunately it is not possible to add a generic object image, video frame and audio frames in Aspose.Slides for .NET as each of mentioned shape has different parameters, when you add them. Secondly, Aspose.Slides for Java supports images, audio and video formats that are supported in PowerPoint. They include BMP, JPEG, GIF and PNG for images, AVI, MPEG etc for videos, and MP3, WAV etc for audio. Unfortunately, there is no way to add them generically.

Please share some details about question 3 and 4, as I have not been able to completely understand them.

Thanks and Regards,

Hi,



Thanks for your response.



Here are some more details:



The images are of 800x600 and 800x534 respectively, when I add it to a slide, the image have not been resized according to the size of the slide and the positioning of the image is also different.



I am attaching a created PPT, with 2 images, please take a look at it and let me know.



And here is the code I am using to write to the PPTX



//Add an empty slide to the SlidesEx collection

SlidesEx pptxSlides = pptxPresentation.getSlides();

pptxSlides.addClone(pptxPresentation.getSlides().get(0));



//Storing the actual file path

//Altered this line of code to show adding of 1 image only, but actually the code is in loop and image file paths are inside a list

String filePath = “/usr/vivek/pic1.jpg”;



//Creating a picture object that will be used to fill the ellipse

BufferedImage img = ImageIO.read(new File (filePath));



//Instantiate the ImageEx class

ImageEx imgEx = pptxPresentation.getImages().addImage(img);



//Calculating picture width and height

int pictureWidth = imgEx.getWidth();

int pictureHeight = imgEx.getHeight();



//Calculating slide width and height

int slideWidth = pptxPresentation.getSlideSize().getSize().width;

int slideHeight = pptxPresentation.getSlideSize().getSize().height;



//Getting image Aspect Ratio

double ImageAR = (double)pictureWidth/pictureHeight;



//Getting Slide Aspect Ratio

//double SlideAR = (double)slideWidth/slideHeight;



//Calculating the width and height of picture frame

int pictureFrameWidth = (int)(slideHeight * ImageAR);

int pictureFrameHeight = (int)(slideWidth * (1/ ImageAR));



//Add the pictureframe

int iLastSlideNo = pptxSlides.size();

pptxSlides.get(iLastSlideNo-1).getShapes().addPictureFrame(ShapeTypeEx.RECTANGLE, pictureFrameWidth, pictureFrameHeight, pictureWidth, pictureHeight, imgEx);



Let me know if this helps.



Thanks & Regards,

Vivek

Hi Vivek,

Please follow this documentation link for addPictureFrame parameters. I have modified the code snippet for you and you may use this. Please note that the first argument of the method refers to the type of shape in picture frame. The second and third parameters are related to x and y position of the top left corner of the picture frame respectively. The 4th and 5th arguments refers to width and height respectively. The last parameters is for the image of picture frame. Please note that x, y position, height and width parameter are to be determined by you and you can develop your own logic to meet your appropriate needs.

slide.getShapes().addPictureFrame(ShapeTypeEx.RECTANGLE , 0, 0,pictureFrameWidth, pictureFrameHeight,imgx);

Thanks and Regards,

This is excellent. I have it now up and running.

BTW is there a size limit for the file? configurable?

Thanks & Regards,
Vivek

Hi Vivek,

I am glad to hear that things worked for you. Secondly, the maximum slide limit is 65536 theoretically. You can have your slides well with in this range. The maximum slide number can also be dependent on the size of memory available on your machine to load the desired presentation as well.

Thanks and Regards,