The Syncfusion .NET Excel (XlsIO) library provides the easiest way for converting Excel documents to PDF in C#/VB.NET with the PDF conformance level PDF/A1B using just a few lines of code. This process is fast, reliable, and supported in hosting environments such as AWS, Google Cloud App, and Microsoft Azure web services.
The Excel to PDF conversion works seamlessly on these platforms: .NET MAUI, ASP.NET Core, ASP.NET MVC, Blazor, Windows Forms, WinUI, WPF, and Xamarin.
Here is an example of how to convert an Excel document to PDF in C# using the Syncfusion .NET Excel library.
using (ExcelEngine excelEngine = new ExcelEngine())
{
IApplication application = excelEngine.Excel;
application.DefaultVersion = ExcelVersion.Xlsx;
using(FileStream excelStream = new FileStream("Sample.xlsx", FileMode.Open, FileAccess.Read))
{
IWorkbook workbook = application.Workbooks.Open(excelStream);
//Initialize ExcelToPdfConverter
ExcelToPdfConverter converter = new ExcelToPdfConverter(workbook);
//Initialize PDF document
PdfDocument pdfDocument = new PdfDocument();
//Convert Excel document into PDF document
pdfDocument = converter.Convert();
//Save the PDF document into a stream
using (MemoryStream outputStream = new MemoryStream())
{
pdfDocument.Save(outputStream);
}
}
}
An Excel document can be converted to PDF with different Excel to PDF conversion options. Some of those options are explained here.
The Excel to PDF conversion can be customized with scaling options such as fitting an entire sheet on one page, fitting all rows or columns on one page, and converting sheets in specified scaling.
The six layout options available and maintained under LayoutOptions enumeration are Automatic, CustomScaling, FitAllColumnsOnOnePage, FitAllRowsOnOnePage, FitSheetOnOnePage and NoScaling.
using (ExcelEngine excelEngine = new ExcelEngine())
{
IApplication application = excelEngine.Excel;
application.DefaultVersion = ExcelVersion.Xlsx;
using(FileStream excelStream = new FileStream("Sample.xlsx", FileMode.Open, FileAccess.Read))
{
IWorkbook workbook = application.Workbooks.Open(excelStream);
//Initialize ExcelToPdfConverterSettings
ExcelToPdfConverterSettings settings = new ExcelToPdfConverterSettings();
//Set layout option as FitSheetOnOnePage
settings.LayoutOptions = LayoutOptions.FitSheetOnOnePage;
//Initialize ExcelToPdfConverter
ExcelToPdfConverter converter = new ExcelToPdfConverter(workbook);
//Initialize PDF document
PdfDocument pdfDocument = new PdfDocument();
//Convert Excel document into PDF document
pdfDocument = converter.Convert(settings);
//Save the document into a stream
using (MemoryStream outputStream = new MemoryStream())
{
pdfDocument.Save(outputStream);
}
}
}
The required paper size of a PDF document can be specified using the CustomPaperSize property.
using (ExcelEngine excelEngine = new ExcelEngine())
{
IApplication application = excelEngine.Excel;
application.DefaultVersion = ExcelVersion.Xlsx;
using(FileStream excelStream = new FileStream("Sample.xlsx", FileMode.Open, FileAccess.Read))
{
IWorkbook workbook = application.Workbooks.Open(excelStream);
//Initialize ExcelToPdfConverterSettings
ExcelToPdfConverterSettings settings = new ExcelToPdfConverterSettings();
//Set CustomerPaperSize
settings.CustomPaperSize = new SizeF(10, 20);
//Initialize ExcelToPdfConverter
ExcelToPdfConverter converter = new ExcelToPdfConverter(workbook);
//Initialize PDF document
PdfDocument pdfDocument = new PdfDocument();
//Convert Excel document into PDF document
pdfDocument = converter.Convert(settings);
//Save the document into a stream
using (MemoryStream outputStream = new MemoryStream())
{
pdfDocument.Save(outputStream);
}
}
}
XlsIO allows the users to set the PDF conformance level to PDF/A-1b conformance or PDF/X-1a conformance.
using (ExcelEngine excelEngine = new ExcelEngine())
{
IApplication application = excelEngine.Excel;
application.DefaultVersion = ExcelVersion.Xlsx;
using(FileStream excelStream = new FileStream("Sample.xlsx", FileMode.Open, FileAccess.Read))
{
IWorkbook workbook = application.Workbooks.Open(excelStream);
//Initialize ExcelToPdfConverterSettings
ExcelToPdfConverterSettings settings = new ExcelToPdfConverterSettings();
//Set the conformance to PDF/A-1b conversion
settings.PdfConformanceLevel = PdfConformanceLevel.Pdf_A1B;
//Initialize ExcelToPdfConverter
ExcelToPdfConverter converter = new ExcelToPdfConverter(workbook);
//Initialize PDF document
PdfDocument pdfDocument = new PdfDocument();
//Convert Excel document into PDF document
pdfDocument = converter.Convert(settings);
//Save the document into a stream
using (MemoryStream outputStream = new MemoryStream())
{
pdfDocument.Save(outputStream);
}
}
}
Form fields can be rendered into a PDF document as editable with EnableFormFields property of ExcelToPdfConverterSettings.
using (ExcelEngine excelEngine = new ExcelEngine())
{
IApplication application = excelEngine.Excel;
application.DefaultVersion = ExcelVersion.Xlsx;
using(FileStream excelStream = new FileStream("Sample.xlsx", FileMode.Open, FileAccess.Read))
{
IWorkbook workbook = application.Workbooks.Open(excelStream);
//Initialize ExcelToPdfConverterSettings
ExcelToPdfConverterSettings settings = new ExcelToPdfConverterSettings();
//Export form fields to PDF
settings.EnableFormFields = true;
//Initialize ExcelToPdfConverter
ExcelToPdfConverter converter = new ExcelToPdfConverter(workbook);
//Initialize PDF document
PdfDocument pdfDocument = new PdfDocument();
//Convert Excel document into PDF document
pdfDocument = converter.Convert(settings);
//Save the document into a stream
using (MemoryStream outputStream = new MemoryStream())
{
pdfDocument.Save(outputStream);
}
}
}
Using the ExportQualityImage property, users can save the high quality TIFF images in PDF document, during Excel to PDF conversion.
using (ExcelEngine excelEngine = new ExcelEngine())
{
IApplication application = excelEngine.Excel;
application.DefaultVersion = ExcelVersion.Xlsx;
using(FileStream excelStream = new FileStream("Sample.xlsx", FileMode.Open, FileAccess.Read))
{
IWorkbook workbook = application.Workbooks.Open(excelStream);
//Initialize ExcelToPdfConverterSettings
ExcelToPdfConverterSettings settings = new ExcelToPdfConverterSettings();
//Export quality image in PDF
settings.ExportQualityImage = true;
//Initialize ExcelToPdfConverter
ExcelToPdfConverter converter = new ExcelToPdfConverter(workbook);
//Initialize PDF document
PdfDocument pdfDocument = new PdfDocument();
//Convert Excel document into PDF document
pdfDocument = converter.Convert(settings);
//Save the document into a stream
using (MemoryStream outputStream = new MemoryStream())
{
pdfDocument.Save(outputStream);
}
}
}
Embed the entire TrueType fonts used in the document and display the document contents uniformly irrespective of whether the font is available or not, during conversion.
using (ExcelEngine excelEngine = new ExcelEngine())
{
IApplication application = excelEngine.Excel;
application.DefaultVersion = ExcelVersion.Xlsx;
using(FileStream excelStream = new FileStream("Sample.xlsx", FileMode.Open, FileAccess.Read))
{
IWorkbook workbook = application.Workbooks.Open(excelStream);
//Initialize ExcelToPdfConverterSettings
ExcelToPdfConverterSettings settings = new ExcelToPdfConverterSettings();
//Embed fonts in PDF
settings.EmbedFonts = true;
//Initialize ExcelToPdfConverter
ExcelToPdfConverter converter = new ExcelToPdfConverter(workbook);
//Initialize PDF document
PdfDocument pdfDocument = new PdfDocument();
//Convert Excel document into PDF document
pdfDocument = converter.Convert(settings);
//Save the document into a stream
using (MemoryStream outputStream = new MemoryStream())
{
pdfDocument.Save(outputStream);
}
}
}
Our built-in SubstituteFont event of IApplication allows users to choose alternate fonts from the device or upload fonts to substitute specified fonts from the Excel document in Excel to PDF conversion.
using (ExcelEngine excelEngine = new ExcelEngine())
{
IApplication application = excelEngine.Excel;
application.DefaultVersion = ExcelVersion.Xlsx;
//Initializes the SubstituteFont event to perform font substitution in Excel-to-PDF conversion.
application.SubstituteFont += new SubstituteFontEventHandler(SubstituteFont);
using(FileStream excelStream = new FileStream("Sample.xlsx", FileMode.Open, FileAccess.Read))
{
IWorkbook workbook = application.Workbooks.Open(excelStream);
//Initialize ExcelToPdfConverter
ExcelToPdfConverter converter = new ExcelToPdfConverter(workbook);
//Initialize PDF document
PdfDocument pdfDocument = new PdfDocument();
//Convert Excel document into PDF document
pdfDocument = converter.Convert();
//Save the document into a stream
using (MemoryStream outputStream = new MemoryStream())
{
pdfDocument.Save(outputStream);
}
}
}
private static void SubstituteFont(object sender, SubstituteFontEventArgs args)
{
//Substitute a font if the specified font is not installed in the machine.
if (args.OriginalFontName == "Arial Unicode MS")
{
//Substitute by font name.
args.AlternateFontName = "Arial";
}
else if (args.OriginalFontName == "Homizio")
{
//Substitute by font stream.
var assembly = Assembly.GetExecutingAssembly();
var resourceName = "ExceltoPDF.Fonts.Homizio.ttf";
using(Stream fileStream = assembly.GetManifestResourceStream(resourceName))
{
using(MemoryStream memoryStream = new MemoryStream())
{
fileStream.CopyTo(memoryStream);
args.AlternateFontStream = memoryStream;
}
}
}
}
Besides this simple Excel to PDF conversion using Syncfusion .NET Excel library, developers can also:
CASE STUDY
Syncfusion’s file format components helped me create the reports I needed, fast. – J. Pereira, Software Developer.
The libraries have been built from scratch and refined for more than a decade to provide blazing-fast performance, comprehensive API, and compatibility across the latest and older versions of these files.
VIDEOS
Syncfusion File Format Libraries - Manipulate Excel, Word, PowerPoint, and PDF files
Read and write Excel, Word, PDF, and PowerPoint files. Also includes integrated visualization capabilities. Advanced features include support for pivot tables, pivot charts, mail-merge, and extensive formatting.
E-BOOK
Succinctly Series: Statistics Using Excel Succinctly
Greatness—it’s one thing to say you have it, but it means more when others recognize it. Syncfusion is proud to hold the following industry awards.