Excel to PDF by setOnePagePerSheet Question

When I try to convert excel to pdf using this method, how do I set the converted pdf page size to be the same?
image.png (26.6 KB)
I want the first page to be the same size as the second and third pages.

@iPlan,

Thanks for the screenshot.

When you specify OnePagePerSheet option to true, Aspose.Cells will render to single PDF page per sheet. The page size purely depends upon the contents of worksheet. If the content or data is less, you will get squeezed/slim PDF page. If the content or data of the sheet is large, you will get big PDF page. If you do not set this option, you will get consistent page size based on Page Setup page size of the sheet.

Maybe you could use Aspose.PDF for Java library (which is PDF manipulation API) to manipulate page size of PDF.

@iPlan

Please also try the following code.

  1. Calclulate the max page width and height with OnePagePerSheet option.
  2. Set custom paper width and height for every sheet with the calculated max page with and height.
  3. Save to pdf directly without OnePagePerSheet option.
Workbook workbook = new Workbook("Book1.xlsx");

//calculate max page width and height.
ImageOrPrintOptions imageOrPrintOptions = new ImageOrPrintOptions();
imageOrPrintOptions.setOnePagePerSheet(true);       
float maxPageWidthInch = 0;
float maxPageHeightInch = 0;
for(int i = 0; i < workbook.getWorksheets().getCount(); i++)
{
    Worksheet sheet = workbook.getWorksheets().get(i);
    SheetRender sheetRender = new SheetRender(sheet, imageOrPrintOptions);
    if(sheetRender.getPageCount() > 0)
    {
        //index 0: width, index 1: height
        float[] pageWidthHeight = sheetRender.getPageSizeInch(0);
        if(pageWidthHeight[0] > maxPageWidthInch)
        {
            maxPageWidthInch = pageWidthHeight[0];
        }
        if(pageWidthHeight[1] > maxPageHeightInch)
        {
            maxPageHeightInch = pageWidthHeight[1];
        }
    }       
}

//some safe delta.
float delta = 0.1f;
//set custom page width and height for each sheet.
for(int i = 0; i < workbook.getWorksheets().getCount(); i++)
{
    Worksheet sheet = workbook.getWorksheets().get(i);
    sheet.getPageSetup().customPaperSize(maxPageWidthInch + delta, maxPageHeightInch + delta);
}


workbook.save("output.pdf");