i want to add some chart to workbook, then i need to print all of these chart, so i want to set each of chart’ size to fit the page to ensure to printing beautiful result.
Hi Pyntia,
hi,Babar
Hi,
hi
Hi,
Thanks for your posting and using Aspose.Cells.
You can find the page size of any page in unit of pixels using the SheetRender.GetPageSize() method. Please see the following sample code that explains how to make use of this method. I have also attached the source excel file used in this code and shown the console output of the code for your reference.
C#
Workbook workbook = new Workbook(“source.xlsx”);
Worksheet worksheet = workbook.Worksheets[0];
SheetRender sr = new SheetRender(worksheet, new ImageOrPrintOptions());
Size sz = sr.GetPageSize(0);
Console.WriteLine(sz);
{Width=795, Height=1124}
hi,Faiz
hi
Hi,
Thanks for your posting and using Aspose.Cells.
If the sheet is totally empty, then you will have to put some value in it. You can put value in cell A1 of the sheet so that it does not remain empty, then you will be able to get the page width of the sheet successfully.
hi
Hi,
Yes putting a blank string will be ok and serve the purpose.
hi
or you can see the attchement file.
Hi,
Thanks for your feedback and using Aspose.Cells.
SheetRender.GetPageSize() returns correct page width but when this width is applied to chart, it gets exceeded the page width even when the left and right margins are set to zero. I have tested this issue with the following sample code and attached the source excel file used in this code as well as output excel file generated by it for a reference.
We have therefore logged this issue in our database for investigation. We will look into it and fix this issue. Once the issue is resolved or we have some other update for you, we will let you know asap.
This issue has been logged as
- CELLSNET-43840 - Chart width exceeds Page width derived from SheetRender.GetPageSize
C#
Workbook workbook = new Workbook(“source.xlsx”);
Worksheet worksheet = workbook.Worksheets[0];
Chart chart = worksheet.Charts[0];
worksheet.PageSetup.LeftMargin = 0;
worksheet.PageSetup.RightMargin = 0;
ImageOrPrintOptions opts = new ImageOrPrintOptions();
SheetRender sr = new SheetRender(worksheet, opts);
Size sz = sr.GetPageSize(0);
chart.ChartObject.Width = sz.Width;
workbook.Save(“output.xlsx”);
Hi,
Thanks for using Aspose.Cells.
Please see the following sample code that fixes your issue. After execution of this code, chart occupies the entire width of the page that lies inside the left and right margins and it also does not exceed to next page.
I have attached the source excel file used in this code and the output excel file generated by it for your reference.
C#
Workbook workbook = new Workbook(“source.xlsx”);
Worksheet worksheet = workbook.Worksheets[0];
Cells cells = worksheet.Cells;
Chart chart = worksheet.Charts[0];
//Add a cell to calc page breaks
int cellRowIndex = 100, cellColumnIndex = 100;
cells[cellRowIndex, cellColumnIndex].Value = “Add a cell to calc page breaks”;
ImageOrPrintOptions opts = new ImageOrPrintOptions();
CellArea[] pageBreaks = worksheet.GetPrintingPageBreaks(opts);
CellArea pageBreak = pageBreaks[0];
int pageContentWidth = 0;
for (int i = pageBreak.StartColumn; i <= pageBreak.EndColumn; i++)
{
pageContentWidth += cells.GetColumnWidthPixel(i);
}
//a litte smaller than page content width
chart.ChartObject.Width = pageContentWidth - 1;
//clear the cell
cells[cellRowIndex, cellColumnIndex].Value = null;
//open the generated file in excel printview, it will be ok.
workbook.Save(“output.xlsx”);