The Syncfusion Blazor 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 Blazor 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 XlsIO renderer
XlsIORenderer renderer = new XlsIORenderer();
//Convert Excel document into PDF document
PdfDocument pdfDocument = renderer.ConvertToPDF(workbook);
//Save the 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 XlsIORendererSettings
XlsIORendererSettings settings = new XlsIORendererSettings();
//Set layout option as FitSheetOnOnePage
settings.LayoutOptions = LayoutOptions.FitSheetOnOnePage;
//Initialize XlsIORenderer
XlsIORenderer renderer = new XlsIORenderer();
//Convert the Excel document to PDF with renderer settings
PdfDocument pdfDocument = renderer.ConvertToPDF(workbook, 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 XlsIORendererSettings
XlsIORendererSettings settings = new XlsIORendererSettings();
//Set CustomerPaperSize
settings.CustomPaperSize = new SizeF(10, 20);
//Initialize XlsIORenderer
XlsIORenderer renderer = new XlsIORenderer();
//Convert the Excel document to PDF with renderer settings
PdfDocument pdfDocument = renderer.ConvertToPDF(workbook, 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 XlsIORendererSettings
XlsIORendererSettings settings = new XlsIORendererSettings();
//Set the conformance to PDF/A-1b conversion
settings.PdfConformanceLevel = PdfConformanceLevel.Pdf_A1B;
//Initialize XlsIORenderer
XlsIORenderer renderer = new XlsIORenderer();
//Convert the Excel document to PDF with renderer settings
PdfDocument pdfDocument = renderer.ConvertToPDF(workbook, 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 XlsIORendererSettings.
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 XlsIORendererSettings
XlsIORendererSettings settings = new XlsIORendererSettings();
//Export form fields to PDF
settings.EnableFormFields = true;
//Initialize XlsIORenderer
XlsIORenderer renderer = new XlsIORenderer();
//Convert the Excel document to PDF with renderer settings
PdfDocument pdfDocument = renderer.ConvertToPDF(workbook, 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 XlsIORendererSettings
XlsIORendererSettings settings = new XlsIORendererSettings();
//Export quality image in PDF
settings.ExportQualityImage = true;
//Initialize XlsIORenderer
XlsIORenderer renderer = new XlsIORenderer();
//Convert the Excel document to PDF with renderer settings
PdfDocument pdfDocument = renderer.ConvertToPDF(workbook, 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 XlsIORendererSettings
XlsIORendererSettings settings = new XlsIORendererSettings();
//Embed fonts in PDF
settings.EmbedFonts = true;
//Initialize XlsIORenderer
XlsIORenderer renderer = new XlsIORenderer();
//Convert the Excel document to PDF with renderer settings
PdfDocument pdfDocument = renderer.ConvertToPDF(workbook, settings);
//Save the document into a stream
using (MemoryStream outputStream = new MemoryStream())
{
pdfDocument.Save(outputStream);
}
}
}
Our built-in SubstituteFont event of IApplication allows 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 XlsIORenderer
XlsIORenderer renderer = new XlsIORenderer();
//Convert the Excel document to PDF with renderer settings
PdfDocument pdfDocument = renderer.ConvertToPDF(workbook, settings);
//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 Blazor Excel library, developers can also:
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.