Convert Excel to PDF in C#
Excel to PDF
Nutrient .NET SDK (formerly GdPicture.NET) offers Office-to-PDF conversion that relies entirely on its own technology. The conversion doesn’t rely on third-party tools such as LibreOffice.
This article explains how to convert Excel files such as XLS or XLSX to PDF.
Creating a PDF from an Excel file
To create a PDF from an Excel file, follow the steps below:
- Create a
GdPictureDocumentConverter
object. - Load the source document by passing its path to the
LoadFromFile
method. Recommended: Specify the source document format with a member of theDocumentFormat
enumeration. - Save the output in a new PDF document with the
SaveAsPDF
method.
The example below creates a PDF document from an Excel file:
using GdPictureDocumentConverter gdpictureDocumentConverter = new GdPictureDocumentConverter();// Load the source document.gdpictureDocumentConverter.LoadFromFile(@"C:\temp\source.xlsx", GdPicture14.DocumentFormat.DocumentFormatXLSX);// Save the output in a new PDF document.gdpictureDocumentConverter.SaveAsPDF(@"C:\temp\output.pdf");
Using gdpictureDocumentConverter As GdPictureDocumentConverter = New GdPictureDocumentConverter() ' Load the source document. gdpictureDocumentConverter.LoadFromFile("C:\temp\source.xlsx", GdPicture14.DocumentFormat.DocumentFormatXLSX) ' Save the output in a new PDF document. gdpictureDocumentConverter.SaveAsPDF("C:\temp\output.pdf")End Using
Used methods
Related topics
Optional configuration properties
Optionally, configure the conversion with the following properties of the GdPictureDocumentConverter
object:
RenderSheetHeadersAndFooters
is a Boolean value that specifies whether to display headers and footers in the output PDF file.SplitExcelSheetsIntoPages
is a Boolean value that specifies whether to display each sheet of the source Excel file on a separate page in the output PDF file.
The example below creates a PDF document from an Excel file with a custom configuration:
using GdPictureDocumentConverter gdpictureDocumentConverter = new GdPictureDocumentConverter();// Load the source document.gdpictureDocumentConverter.LoadFromFile(@"C:\temp\source.xlsx", GdPicture14.DocumentFormat.DocumentFormatXLSX);// Configure the conversion.gdpictureDocumentConverter.RenderSheetHeadersAndFooters = true;gdpictureDocumentConverter.SplitExcelSheetsIntoPages = true;// Save the output in a new PDF document.gdpictureDocumentConverter.SaveAsPDF(@"C:\temp\output.pdf");
Using gdpictureDocumentConverter As GdPictureDocumentConverter = New GdPictureDocumentConverter() ' Load the source document. gdpictureDocumentConverter.LoadFromFile("C:\temp\source.xlsx", GdPicture14.DocumentFormat.DocumentFormatXLSX) ' Configure the conversion. gdpictureDocumentConverter.RenderSheetHeadersAndFooters = True gdpictureDocumentConverter.SplitExcelSheetsIntoPages = True ' Save the output in a new PDF document. gdpictureDocumentConverter.SaveAsPDF("C:\temp\output.pdf")End Using
Used methods and properties
Related topics