Articles in this section
Category / Section

How to convert worksheet to PDF in WinForms XIsIO?

2 mins read

It is possible to convert a WinForms Excel workbook to PDF with different converter settings for each worksheet. This can be achieved by carrying forward the PdfDocument through the TemplateDocument property available in ExcelToPdfConverterSettings.

The following code snippet explains this.

using (ExcelEngine excelEngine = new ExcelEngine())
{
    IApplication application = excelEngine.Excel;
    application.DefaultVersion = ExcelVersion.Excel2016;
 
    IWorkbook workbook = application.Workbooks.Open("../../Data/Sample.xlsx");
 
    // Load the first worksheet into ExcelToPdfConverter
    ExcelToPdfConverter converter = new ExcelToPdfConverter(workbook.Worksheets[0]);
 
    // Define ExcelToPdfConverterSettings
    ExcelToPdfConverterSettings settings = new ExcelToPdfConverterSettings();
    settings.DisplayGridLines = GridLinesDisplayStyle.Visible;
    settings.LayoutOptions = LayoutOptions.FitAllColumnsOnOnePage;
 
    // Convert the first worksheet to PDF with converter settings
    PdfDocument document = converter.Convert(settings);
 
    //Now load the second worksheet into ExcelToPdfConverter
    converter = new ExcelToPdfConverter(workbook.Worksheets[1]);
 
    // Have ExcelToPdfConverterSettings               
    settings = new ExcelToPdfConverterSettings();
    //Note: Assigning the document to TemplateDocument in converter settings is mandatory
    //This will carry over the already converted worksheets
    settings.TemplateDocument = document;
    settings.DisplayGridLines = GridLinesDisplayStyle.Visible;
 
    // Convert the second worksheet to PDF with converter settings
    document = converter.Convert(settings);
 
    // Now load third worksheet into ExcelToPdfConverter
    converter = new ExcelToPdfConverter(workbook.Worksheets[2]);
 
    // Have ExcelToPdfConverterSettings               
    settings = new ExcelToPdfConverterSettings();
    // Note: Assigning the document to TemplateDocument in converter settings is mandatory
    // This will carry over the already converted worksheets
    settings.TemplateDocument = document;
    settings.DisplayGridLines = GridLinesDisplayStyle.Invisible;
    settings.LayoutOptions = LayoutOptions.FitSheetOnOnePage;
 
    // Convert the third worksheet to PDF with converter settings
    document = converter.Convert(settings);
 
    // Save the PDF
    document.Save("Output.pdf");
    System.Diagnostics.Process.Start("Output.pdf");
}

 

A complete working sample to convert each worksheet with different ExcelToPdfConverterSettings can be downloaded from ExcelToPDF.zip.

 

Take a moment to peruse the documentation where you can find basic Excel document processing options along with the features like import and export data, chart, formulas, conditional formatting, data validation, tables, pivot tables and protect the Excel documents, and most importantly, the PDF, CSV and Image conversions with code examples.

 

Conclusion

I hope you enjoyed learning about how to convert worksheet to PDF with different converters in WinForms XIsIO.

You can refer to our XIsIO’s feature tour page to learn about its other groundbreaking features. Explore our UG documentation and online demos to understand how to manipulate data in Excel documents.

If you are an existing user, you can access our latest components from the License and Downloads page. For new users, you can try our 30-day free trial to check out XlsIO and other Syncfusion® components.

If you have any queries or require clarification, please let us know in the comments below or contact us through our support forumsSupport Tickets, or feedback portal. We are always happy to assist you!

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please  to leave a comment
Access denied
Access denied