Save Each Worksheet to a Different PDF File
Contents
[
Hide
]
Aspose.Cells supports converting spreadsheet files (that contain images, charts, etc.) to PDF documents. Aspose.Cells for Java can work independently to convert a spreadsheet to PDF document and you do not need to use Aspose.PDF for Java for the conversion any longer. The conversion does not require to create / use any temporary file(s) too as the whole process can be done in the memory.
If you need to save each worksheet in your template Excel file to generate different PDF files. This can be achieved easily. You may try to set one sheet index to PdfSaveOptions.SheetSet option at a time to render to PDF.
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(SaveEachWorksheettoDifferentPDF.class); | |
// Instantiate a new workbook and open the Excel | |
// File from its location | |
Workbook workbook = new Workbook(dataDir + "input.xlsx"); | |
// Get the count of the worksheets in the workbook | |
int sheetCount = workbook.getWorksheets().getCount(); | |
// Define PdfSaveOptions | |
PdfSaveOptions pdfSaveOptions = new PdfSaveOptions(); | |
// Take Pdfs of each sheet | |
for (int j = 0; j < sheetCount; j++) { | |
Worksheet ws = workbook.getWorksheets().get(j); | |
//set worksheet to output | |
SheetSet sheetSet = new SheetSet(new int[] { ws.getIndex() }); | |
pdfSaveOptions.setSheetSet(sheetSet); | |
workbook.save(dataDir + "_" + ws.getName() + ".pdf", pdfSaveOptions); | |
} |
If the spreadsheet contains formulas, it is best to call the Workbook.calculateFormula() method just before rendering the spreadsheet to PDF. This ensures that formula dependent values are recalculated, and the correct values are rendered in the PDF.