Save Specified Worksheets to PDF

By default, Aspose.Cells save all visible worksheets in a workbook to pdf file. With PdfSaveOptions.SheetSet option, you can save specified worksheets to pdf file. e.g. you can save active worksheet to pdf, save all worksheets(both visible and hidden worksheets) to pdf, save custom multiple worksheets to pdf.

Save Active Worksheet to PDF

If you want to only export active sheet to pdf, you can achieve this by passing SheetSet.Active to PdfSaveOptions.SheetSet option.

The sheet Sheet2 is the acitve sheet of the source file sheetset-example.xlsx.

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET
// Open the template excel file
Workbook wb = new Workbook("sheetset-example.xlsx");
// Set active sheet to output
PdfSaveOptions pdfSaveOptions = new PdfSaveOptions();
pdfSaveOptions.SheetSet = SheetSet.Active;
// Save the pdf file with PdfSaveOptions
wb.Save("output.pdf", pdfSaveOptions);

Save All Worksheets to PDF

SheetSet.Visible indicates visible sheets in a workbook, and SheetSet.All indicates all sheets including both visible sheets and hidden/invisible sheets in a workbook. If you want to export all sheets to pdf, you can just pass SheetSet.All to PdfSaveOptions.SheetSet option.

The source file sheetset-example.xlsx contains all four sheets with hidden sheet Sheet3.

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET
// Open the template excel file
Workbook wb = new Workbook("sheetset-example.xlsx");
// Set all sheets to output
PdfSaveOptions pdfSaveOptions = new PdfSaveOptions();
pdfSaveOptions.SheetSet = SheetSet.All;
// Save the pdf file with PdfSaveOptions
wb.Save("output.pdf", pdfSaveOptions);

Save Specified Worksheets to PDF

If you want to export desired/custom multiple sheets to pdf, you can achieve this by passing multiple sheet indices to PdfSaveOptions.SheetSet option.

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET
// Open the template excel file
Workbook wb = new Workbook("sheetset-example.xlsx");
// Set custom multiple sheets(Sheet1, Sheet3) to output
SheetSet sheetSet = new SheetSet(new int[] { 0, 2 });
PdfSaveOptions pdfSaveOptions = new PdfSaveOptions();
pdfSaveOptions.SheetSet = sheetSet;
// Save the pdf file with PdfSaveOptions
wb.Save("output.pdf", pdfSaveOptions);

Reorder Worksheets to PDF

If you want to reorder sheets(e.g. in reverse order) to pdf without modify the source file, you can achieve this by passing reodered sheet indices to PdfSaveOptions.SheetSet option.

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET
// Open the template excel file
Workbook wb = new Workbook("sheetset-example.xlsx");
// Reorder sheets(Sheet1, Sheet2, Sheet3, Sheet4) to sheets(Sheet4, Sheet3, Sheet2, Sheet1)
SheetSet sheetSet = new SheetSet(new int[] { 3, 2, 1, 0 });
PdfSaveOptions pdfSaveOptions = new PdfSaveOptions();
pdfSaveOptions.SheetSet = sheetSet;
// Save the pdf file with PdfSaveOptions
wb.Save("output.pdf", pdfSaveOptions);