I am taking images from excel and adding them to ppt slides. It works well except when the text in excel cells is wider than the width of printed area.
I attached resulted slide (redacted) for reference as well as original excel file. As you can see the two images come from sheets of the same size however one if much wider giving it a different aspect ratio (the aspect ratio is the real issue for me). It is triggered by having a lot of text in the overflowing cell.
Desktop.zip (60.5 KB)
Funny that writing files to disk or using Image.FromStream(stream) give matching ratios but lower quality and higher ppt size.
Below is the code I used to generate this:
public void demo()
{
new Aspose.Cells.License().SetLicense(@"...Cells.lic");
new Aspose.Slides.License().SetLicense(@"...Slides.lic");
Workbook wb = new Workbook(@"...ExcelWithChart.xlsx");
Presentation p = new Presentation(@"...Template.pptx");
ISlide template = p.Slides[0];
fillSlide(wb, p, template, 0);
fillSlide(wb, p, template, 1);
p.Save(new FileStream(@"....output.pptx", FileMode.Create), Aspose.Slides.Export.SaveFormat.Pptx);
}
private static void fillSlide(Workbook wb, Presentation p, ISlide template, int index)
{
var slide = p.Slides.InsertClone(0, template);
MemoryStream stream = getStream(wb, index);
IPPImage image = p.Images.AddImage(stream);
IPictureFrame toBeReplace = slide.Shapes.FirstOrDefault(x => x.Name == "tableImage") as IPictureFrame;
int indexOfImage = slide.Shapes.IndexOf(toBeReplace);
IPictureFrame pictureFrame = slide.Shapes.InsertPictureFrame(indexOfImage, toBeReplace.ShapeType,
toBeReplace.X, toBeReplace.Y, image.Width, image.Height, image);
slide.Shapes.Remove(toBeReplace);
pictureFrame.Name = toBeReplace.Name;
}
private static MemoryStream getStream(Workbook wb, int index)
{
var sheet = wb.Worksheets[index];
PageSetup pageSetup = sheet.PageSetup;
pageSetup.PrintArea = "A1:F5";
pageSetup.RightMargin = pageSetup.BottomMargin = pageSetup.LeftMargin = pageSetup.TopMargin = 0;
var stream = new MemoryStream();
new Aspose.Cells.Rendering.SheetRender(sheet, IMAGE_OR_PRINT_OPTIONS).ToImage(0, stream);
stream.Position = 0;
return stream;
}
public static Aspose.Cells.Rendering.ImageOrPrintOptions IMAGE_OR_PRINT_OPTIONS
{
get
{
return new Aspose.Cells.Rendering.ImageOrPrintOptions
{
SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality,
EmfType = System.Drawing.Imaging.EmfType.EmfPlusDual,
ImageType = Aspose.Cells.Drawing.ImageType.Emf,
PrintingPage = Aspose.Cells.PrintingPageType.IgnoreBlank,
IsCellAutoFit = true,
OnePagePerSheet = true,
};
}
}