Convert specific tabs from excel to PDF

Hi Team, Is it possible to export selective sheets from an excel book to PDF. I see, the limitation with the below code is, whole workbook will be converted to PDF. But I don’t want to export all the tabs from my source workbook. Please advise. Thanks.

workbook.Save(dataDir + “output.pdf”, SaveFormat.Pdf);

@SubbuatAspose,

Thanks for your query.

You can opt to hide unwanted worksheets in the workbook and render the workbook having desired worksheets only to PDF using the standard approach. Please check the document/article that uses the same technique to render each worksheet to a separate PDF file for your reference:

Hope, this helps a bit.

Thanks Amjad for the quick reply.

The only problem I see here is, looping through all the sheets two times. (once for making invisible and then visible). Because my requirement is, to have all sheets in excel report but only 1 in PDF.

@SubbuatAspose,

Why you are using two loops. I think you may easily cope with it using one loop. In the loop you will make invisible all sheets except your desired one (which you need to render into output PDF). See the following sample code for your reference. Suppose there is a sheet named “MySheet” in the workbook. This is the only sheet in the workbook you need to render to PDF file format. The following sample is just for demonstration purpose so you may edit or update it accordingly:
e.g
Sample code:

var book = new Workbook("D:/temp/sample.xlsx");
var worksheets = book.Worksheets;
foreach (Worksheet worksheet in worksheets)
{
      if (worksheet.Name != "MySheet")
      {
            worksheet.IsVisible = false;
      }
}
book.Save("out1.pdf", new PdfSaveOptions());

Hope, this helps a bit.

Agree, but does this code not hide the sheets permanently in the excel, if we run the loop only once?

Thank you very much,
Subbu

Basically, after I save down the excel, anyone opens the excel should be able to see all the sheets (without doing any manual unhide operation) and see 1 page in PDF. So, if we run the above loop, we are making invisible true - means, hiding them in the workbook and not unhiding them back after rending the required sheet as PDF?

@SubbuatAspose,

Well, since you are saving to PDF file so what’s wrong with it. I mean your original Excel file will remain the same unless you overwrite it by the same name (of Excel file).

And, yes, if you are also re-saving your Excel file, you have to unhide the worksheets (as per original Excel file) again.

Thanks Amjad.

@SubbuatAspose,

You are welcome. In case you find any issue or have other queries, please feel free to contact us any time and we will be happy to assist you soon.