[]
DsExcel allows users to export only specific worksheets in a workbook (instead of the entire workbook) to PDF pages, and also to scan for and remove blank pages in the PDF.
This feature is useful especially when you have a workbook containing large number of worksheets. For instance - While saving to a PDF file, you may not want to export the entire workbook containing multiple worksheets and want only some important worksheets to be saved to the PDF file. In this scenario, you can use this feature to generate a custom PDF file as per your requirements.
In order to export specific pages to the PDF file, create an instance of the PrintManager class and get the default pagination using the Paginate() method. Next, you need to specify the pages that you want to export or print. Finally, call the UpdatePageNumberAndPageSettings() method in order to update the indexes of the page number and the page settings for each page. When you are done, simply save your PDF file using the SavePDF() method.
Refer to the following example code to export some specific pages to the PDF file.
// Initialize workbook.
Workbook workbook = new Workbook();
// Open Excel file.
workbook.Open("PrintSpecificPDFPages.xlsx");
// Create an instance of the PrintManager class.
PrintManager printManager = new PrintManager();
// Get the natural pagination information of the workbook.
IList<PageInfo> pages = printManager.Paginate(workbook);
// Pick some pages to print.
IList<PageInfo> newPages = new List<PageInfo>();
newPages.Add(pages[0]);
newPages.Add(pages[2]);
/* Update the page number and the page settings of
each page. The page number is continuous */
printManager.UpdatePageNumberAndPageSettings(newPages);
// Save the pages into PDF file.
printManager.SavePDF(@"PrintSpecificPages.pdf", newPages);While exporting a workbook to a PDF file, sometimes you may encounter a couple of extra pages that are completely blank. In a workbook with large number of worksheets, it is extremely difficult to find out which pages are empty and even more time-consuming to delete them from the middle without impacting the pagination.
In order to avoid printing and publishing of blank pages, DsExcel enables users to scan through the pages of the PDF, find out which pages are blank and then exclude the blank pages from the middle while also updating the pagination information accurately.
For removing blank pages from your PDF file, you need to first create an instance of the PrintManager class and use the Paginate method to get the default pagination of the workbook. Now, you can use the HasPrintContent method to check whether the pages have content or not. Finally, call the UpdatePageNumberAndPageSettings method in order to update the indexes of the page number and the page settings for each page. When you are done, simply save your PDF file using the SavePDF() method.
Refer to the following example code to allow users to delete the blank pages in the middle while exporting to a PDF file.
// Initialize workbook.
Workbook workbook = new Workbook();
// Open Excel file.
workbook.Open("DeletingBlankPages.xlsx");
// Create an instance of the PrintManager class.
PrintManager printManager = new PrintManager();
// Get the natural pagination information of the workbook.
IList<PageInfo> pages = printManager.Paginate(workbook);
// Remove empty pages.
IList<PageInfo> newPages = new List<PageInfo>();
foreach (PageInfo page in pages)
{
// True if there is content in the range to print.
if (printManager.HasPrintContent(page.PageContent.Range))
{
newPages.Add(page);
}
}
// Update the page number and the page settings of each page.
printManager.UpdatePageNumberAndPageSettings(newPages);
// Save to PDF file.
printManager.SavePDF("DeleteBlankPagesIntheMiddle.pdf", newPages);