Hi Marco,
In order to achieve your desired results, you have to increase the column widths as per specified page size and then render the spreadsheet to PDF. Please check the following piece of code for demonstration purposes and amend it to suit your application requirements.
C#
Workbook book = new Workbook(dir + “Book2.xlsx”);
Worksheet sheet = book.Worksheets[0];
Cells cells = sheet.Cells;
PageSetup pageSetup = sheet.PageSetup;
double paperWidthInch = GetPaperWidthInch(pageSetup.PaperSize, pageSetup.Orientation);
int indexA = CellsHelper.ColumnNameToIndex(“A”);
int indexB = CellsHelper.ColumnNameToIndex(“B”);
int indexC = CellsHelper.ColumnNameToIndex(“C”);
double columnAWidthInch = cells.GetColumnWidthInch(indexA);
double columnBWidthInch = cells.GetColumnWidthInch(indexB);
double columnCWidthInch = cells.GetColumnWidthInch(indexC);
double scale = paperWidthInch / (columnAWidthInch + columnBWidthInch + columnCWidthInch);
cells.SetColumnWidthInch(indexA, columnAWidthInch * scale);
cells.SetColumnWidthInch(indexB, columnAWidthInch * scale);
cells.SetColumnWidthInch(indexC, columnAWidthInch * scale);
pageSetup.FitToPagesWide = 1;
pageSetup.FitToPagesTall = 0;
book.Save(dir + “output.pdf”);
Here is the GetPaperWidthInch method for your reference.
private static double GetPaperWidthInch(PaperSizeType type, PageOrientationType orientation)
{
//in inch
double width, height;
//Assume paper A4 is used, you should update some other paper size you used.
switch (type)
{
//paper A4 is 210mm X 297mm
case PaperSizeType.PaperA4:
width = 210 / 25.4;
height = 297 / 25.4;
break;
//A4
default:
width = 210 / 25.4;
height = 297 / 25.4;
break;
}
if (orientation == PageOrientationType.Landscape)
{
return height;
}
return width;
}