Converting Excel spreadsheets to PDF files is a common task for many users. PDFs maintain the professional formatting and layout of your spreadsheets, allowing you to share your work securely and reliably. The conversion process is straightforward, giving you an easy way to distribute Excel files without compromising the original content and appearance.
This guide will walk you through the steps to convert an Excel spreadsheet to a PDF file, including how to apply specific configurations, using C# and the Spire.XLS for .NET library.
- Convert a Worksheet to PDF with Default Page Settings in C#
- Fit Sheet on One Page while Converting a Worksheet to PDF in C#
- Customize Page Margins while Converting a Worksheet to PDF in C#
- Specify Page Size while Converting a Worksheet to PDF in C#
- Preserve Gridlines while Converting a Worksheet to PDF in C#
- Specify Page Orientation while Converting a Worksheet to PDF in C#
- Convert a Cell Range to PDF in C#
- Convert an Entire Workbook to PDF in C#
Install Spire.XLS for .NET
To begin with, you need to add the DLL files included in the Spire.XLS for .NET package as references in your .NET project. The DLL files can be either downloaded from this link or installed via NuGet.
PM> Install-Package Spire.XLS
Convert a Worksheet to PDF with Default Page Settings in C#
Page settings control how your worksheet prints or displays when converted to different formats. These settings let you manage orientation, paper size, margins, scaling, and more. If you've already set the desired page settings for your worksheet, you can convert it to PDF using the Worksheet.SaveToPdf() method.
The following are the steps to convert a worksheet to PDF with the default page settings.
- Create a Workbook object.
- Load an Excel file using Workbook.LoadFromFile() method.
- Get a specific worksheet through Workbook.Worksheets property.
- Convert it to PDF using Worksheet.SaveToPdf() method.
- C#
using Spire.Xls; namespace ConvertExcelToPdfWithDefaultPageSettings { class Program { static void Main(string[] args) { // Create a Workbook object Workbook workbook = new Workbook(); // Load an Excel document workbook.LoadFromFile("C:\\Users\\Administrator\\Desktop\\Input.xlsx"); // Get a specific worksheet Worksheet sheet = workbook.Worksheets[0]; // Convert it to a PDF file sheet.SaveToPdf("WorksheetToPdf.pdf"); // Dispose resources workbook.Dispose(); } } }
Fit Sheet on One Page while Converting a Worksheet to PDF in C#
Fitting the content onto a single page enhances the readability and allows viewers to take in the entire dataset at a glance, improving their understanding. To ensure that your worksheet content fits neatly on a single page when converting it to a PDF format, you need to set the Workbook.ConverterSetting.SheetFitToPage property to true.
The following are the detailed steps.
- Create a Workbook object.
- Load an Excel file using Workbook.LoadFromFile() method.
- Fit worksheet on one page by setting Workbook.ConverterSetting.SheetFitToPage property to true.
- Get a specific worksheet through Workbook.Worksheets property.
- Convert it to PDF using Worksheet.SaveToPdf() method.
- C#
using Spire.Xls; namespace FitOnOnePage { class Program { static void Main(string[] args) { // Create a Workbook object Workbook workbook = new Workbook(); // Load an Excel document workbook.LoadFromFile("C:\\Users\\Administrator\\Desktop\\Input.xlsx"); // Fit sheet on one page workbook.ConverterSetting.SheetFitToPage = true; // Get a specific worksheet Worksheet sheet = workbook.Worksheets[0]; // Convert the worksheet to a PDF file sheet.SaveToPdf("FitOnePage.pdf"); // Dispose resources workbook.Dispose(); } } }
Customize Page Margins while Converting a Worksheet to PDF in C#
Adjusting the page margin settings allows you to control the spacing between the content and the edges of the PDF page. The PageSetup class can be used to work with various page settings, including margins.
The steps to customize the page margins while converting a worksheet to PDF are as follows.
- Create a Workbook object.
- Load an Excel file using Workbook.LoadFromFile() method.
- Fit worksheet on one page by setting Workbook.ConverterSetting.SheetFitToPage property to true.
- Get a specific worksheet through Workbook.Worksheets property.
- Get the PageSetup object from the worksheet through Worksheet.PageSetup property.
- Set the top, bottom, right, left margins through TopMargin, BottomMargin, RightMargin, LeftMargin properties of the PageSetup object.
- Convert the worksheet to PDF using Worksheet.SaveToPdf() method.
- C#
using Spire.Xls; namespace CustomizePageMargin { class Program { static void Main(string[] args) { // Create a Workbook object Workbook workbook = new Workbook(); // Load an Excel document workbook.LoadFromFile("C:\\Users\\Administrator\\Desktop\\Input.xlsx"); // Fit sheet on one page workbook.ConverterSetting.SheetFitToPage = true; // Get a specific worksheet Worksheet sheet = workbook.Worksheets[0]; // Get the PageSetup object PageSetup pageSetup = sheet.PageSetup; // Set page margins pageSetup.TopMargin = 0.5; pageSetup.BottomMargin = 0.5; pageSetup.LeftMargin = 0.3; pageSetup.RightMargin = 0.3; // Convert it to a PDF file sheet.SaveToPdf("CustomizeMargin.pdf"); // Dispose resources workbook.Dispose(); } } }
Specify Page Size while Converting a Worksheet to PDF in C#
The PageSetup class includes the PaperSize property, which you can use to define the page size when converting a worksheet to PDF. This property enables you to specify standard paper sizes like A3, A4, B4, B5, or even a custom paper size to meet your specific requirements.
The following are the steps to specify page size while converting a worksheet to PDF.
- Create a Workbook object.
- Load an Excel file using Workbook.LoadFromFile() method.
- Fit worksheet on one page by setting Workbook.ConverterSetting.SheetFitToPage property to true.
- Fit worksheet on one page without changing the paper size by setting Workbook.ConverterSetting.SheetFitToPageRetainPaperSize to true.
- Get a specific worksheet through Workbook.Worksheets property.
- Get the PageSetup object from the worksheet through Worksheet.PageSetup property.
- Set the paper size through PageSetup.PaperSize property.
- Convert the worksheet to PDF using Worksheet.SaveToPdf() method.
- C#
using Spire.Xls; namespace SpecifyPageSize { class Program { static void Main(string[] args) { // Create a Workbook object Workbook workbook = new Workbook(); // Load an Excel document workbook.LoadFromFile("C:\\Users\\Administrator\\Desktop\\Input.xlsx"); // Fit sheet on one page workbook.ConverterSetting.SheetFitToPage = true; // Fit sheet on one page while retaining paper size workbook.ConverterSetting.SheetFitToPageRetainPaperSize = true; // Get a specific worksheet Worksheet sheet = workbook.Worksheets[0]; // Get the PageSetup object PageSetup pageSetup = sheet.PageSetup; // Set the page size to A4 pageSetup.PaperSize = PaperSizeType.PaperA4; // Convert the worksheet to a PDF file sheet.SaveToPdf("SpecifyPageSize.pdf"); // Dispose resources workbook.Dispose(); } } }
Preserve Gridlines while Converting a Worksheet to PDF in C#
By preserving the gridlines during the conversion process, you can create PDF versions of your worksheets that closely mirror the original layout and enhance the overall usability and understanding of the data. To preserve the gridlines, set the PageSetup.IsPrintGridlines property to true.
The detailed steps are as follows.
- Create a Workbook object.
- Load an Excel file using Workbook.LoadFromFile() method.
- Fit worksheet on one page by setting Workbook.ConverterSetting.SheetFitToPage property to true.
- Get a specific worksheet through Workbook.Worksheets property.
- Get the PageSetup object from the worksheet through Worksheet.PageSetup property.
- Preserve gridlines by setting PageSetup.IsPrintGridlines property to true.
- Convert the worksheet to PDF using Worksheet.SaveToPdf() method.
- C#
using Spire.Xls; namespace PreserveGridlines { class Program { static void Main(string[] args) { // Create a Workbook object Workbook workbook = new Workbook(); // Load an Excel document workbook.LoadFromFile("C:\\Users\\Administrator\\Desktop\\Input.xlsx"); // Fit sheet on one page workbook.ConverterSetting.SheetFitToPage = true; // Get a specific worksheet Worksheet sheet = workbook.Worksheets[0]; // Get the PageSetup object PageSetup pageSetup = sheet.PageSetup; // Preserve gridlines pageSetup.IsPrintGridlines = true; // Convert it to a PDF file sheet.SaveToPdf("PreserveGridlines.pdf"); // Dispose resources workbook.Dispose(); } } }
Specify Page Orientation while Converting a Worksheet to PDF in C#
Selecting the appropriate orientation ensures the data, text, and other elements are properly displayed without being cropped or stretched. To specify the page orientation, you can use the PageSetup.Orientation property.
The following are the steps to specify page orientation while converting a worksheet to PDF.
- Create a Workbook object.
- Load an Excel file using Workbook.LoadFromFile() method.
- Get a specific worksheet through Workbook.Worksheets property.
- Get the PageSetup object from the worksheet through Worksheet.PageSetup property.
- Set the page orientation through PageSetup.Orientation property.
- Convert the worksheet to PDF using Worksheet.SaveToPdf() method.
- C#
using Spire.Xls; namespace SpecifyPageOrientation { class Program { static void Main(string[] args) { // Create a Workbook object Workbook workbook = new Workbook(); // Load an Excel document workbook.LoadFromFile("C:\\Users\\Administrator\\Desktop\\Input.xlsx"); // Get a specific worksheet Worksheet sheet = workbook.Worksheets[0]; // Get the PageSetup object PageSetup pageSetup = sheet.PageSetup; // Set page orientation to Landscape or Portrait pageSetup.Orientation = PageOrientationType.Landscape; // Convert it to a PDF file sheet.SaveToPdf("PageOrientation.pdf"); // Dispose resources workbook.Dispose(); } } }
Convert a Cell Range to PDF in C#
By converting only the necessary cell range, you can create a PDF that highlights the most critical data, insights, or information, without including extraneous content. To specify a cell range for conversion, you can use the PageSetup.PrintArea property.
The following are the steps to convert a call range of a worksheet to PDF.
- Create a Workbook object.
- Load an Excel file using Workbook.LoadFromFile() method.
- Get a specific worksheet through Workbook.Worksheets property.
- Get the PageSetup object from the worksheet through Worksheet.PageSetup property.
- Set the cell range to be converted through PageSetup.PrintArea property.
- Convert the cell range to PDF using Worksheet.SaveToPdf() method.
- C#
using Spire.Xls; namespace CellRangeToPdf { class Program { static void Main(string[] args) { // Create a Workbook object Workbook workbook = new Workbook(); // Load an Excel document workbook.LoadFromFile("C:\\Users\\Administrator\\Desktop\\Input.xlsx"); // Fit sheet on one page workbook.ConverterSetting.SheetFitToPage = true; // Get a specific worksheet Worksheet sheet = workbook.Worksheets[0]; // Get the PageSetup object PageSetup pageSetup = sheet.PageSetup; // Set print area pageSetup.PrintArea = "A13:D22"; // Convert it to a PDF file sheet.SaveToPdf("CellRangeToPdf.pdf"); // Dispose resources workbook.Dispose(); } } }
Convert an Entire Workbook to PDF in C#
Converting an entire workbook to a single PDF, with each worksheet represented as a separate page, can offer benefits in terms of comprehensive reporting, streamlined distribution, preserving workbook structure, and improved document management.
To convert a workbook to a single PDF file, you can use the Workbook.SaveToFile() method. Before conversion, you may also need to configure the convert options and page settings.
The following are the steps to convert an entire worksheet to PDF using Spire.XLS.
- Create a Workbook object.
- Load an Excel file using Workbook.LoadFromFile() method.
- Set the convert options through Workbook.ConverterSetting property.
- Iterate through each worksheet in the workbook, and configure the page settings through Worksheet.PageSetup property.
- Convert the workbook to PDF using Workbook.SaveToFile() method.
- C#
using Spire.Xls; namespace CellRangeToPdf { class Program { static void Main(string[] args) { // Create a Workbook object Workbook workbook = new Workbook(); // Load an Excel document workbook.LoadFromFile("C:\\Users\\Administrator\\Desktop\\Input.xlsx"); // Fit each sheet on one page workbook.ConverterSetting.SheetFitToPage = true; // Fit sheet on one page while retaining paper size workbook.ConverterSetting.SheetFitToPageRetainPaperSize = true; // Iterate through the worksheets for (int i = 0; i < workbook.Worksheets.Count; i++) { // Get a specific worksheet Worksheet sheet = workbook.Worksheets[0]; // Get the PageSetup object PageSetup pageSetup = sheet.PageSetup; // Set the page size to A4 pageSetup.PaperSize = PaperSizeType.PaperA4; } // Convert the workbook to PDF workbook.SaveToFile("WorkbookToPdf.pdf", FileFormat.PDF); // Dispose resources workbook.Dispose(); } } }
Apply for a Temporary License
If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.