Fit All Worksheet Columns on Single PDF Page
Contents
[
Hide
]
Sometimes you want to generate a PDF file that fits all a worksheet’s columns onto a single page. The PdfSaveOptions.setAllColumnsInOnePagePerSheet property provides this feature in a very easy-to-use manner. Complex calculations such as the height and width of the output PDF page are handled internally and are based on the data in the worksheet.
Fit Worksheet Columns on Single PDF Page
PdfSaveOptions.setAllColumnsInOnePagePerSheet ensures that all columns of a worksheet are rendered to a single PDF page, although rows may expand to several pages depending upon the data in worksheet.
When a given worksheet has many columns, the rendered PDF file may show the contents at a very small size. It is still readable when scaled up in a viewing application such as Acrobat Reader.
The sample code below shows how to use the PdfSaveOptions.setAllColumnsInOnePagePerSheet property to render a large worksheet of 100 columns.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java | |
// The path to the documents directory. | |
String dataDir = Utils.getDataDir(FitAllWorksheetColumns.class); | |
// Create and initialize an instance of Workbook | |
Workbook book = new Workbook(dataDir + "TestBook.xlsx"); | |
// Create and initialize an instance of PdfSaveOptions | |
PdfSaveOptions saveOptions = new PdfSaveOptions(SaveFormat.PDF); | |
// Set AllColumnsInOnePagePerSheet to true | |
saveOptions.setAllColumnsInOnePagePerSheet(true); | |
// Save Workbook to PDF fromart by passing the object of PdfSaveOptions | |
book.save(dataDir + "output.pdf", saveOptions); |
If your spreadsheet contains formulas, it is best to call Workbook.calculateFormula method just before rendering the spreadsheet to PDF format. Doing so will ensure that the formula dependent values are recalculated, and the correct values are rendered in the PDF.