Excel to PDF Conversion in .NET Excel Library
13 Aug 202624 minutes to read
XlsIO supports converting an entire workbook, a single worksheet, or a chart to a PDF document. The following links list the assemblies and NuGet packages required for each platform:
NOTE
IMPORTANT: Before running the samples on this page, install the required NuGet package for your target platform (see the links above) and register your Syncfusion license key. For more information, see the Licensing overview.
NOTE
Worksheet To Image conversion can be performed by referring the Syncfusion.XlsIORenderer.Net.Core NuGet package in UWP platform.
Workbook to PDF
The following code illustrates how to convert an entire Excel workbook to PDF.
using (ExcelEngine excelEngine = new ExcelEngine())
{
IApplication application = excelEngine.Excel;
application.DefaultVersion = ExcelVersion.Xlsx;
IWorkbook workbook = application.Workbooks.Open(Path.GetFullPath(@"Data/InputTemplate.xlsx"));
//Initialize XlsIO renderer.
XlsIORenderer renderer = new XlsIORenderer();
//Convert Excel document into PDF document
PdfDocument pdfDocument = renderer.ConvertToPDF(workbook);
#region Save
//Saving the workbook
pdfDocument.Save(Path.GetFullPath("Output/WorkbookToPDF.pdf"));
#endregion
}using (ExcelEngine excelEngine = new ExcelEngine())
{
IApplication application = excelEngine.Excel;
application.DefaultVersion = ExcelVersion.Xlsx;
IWorkbook workbook = application.Workbooks.Open("Sample.xlsx", ExcelOpenType.Automatic);
//Open the Excel document to Convert
ExcelToPdfConverter converter = new ExcelToPdfConverter(workbook);
//Initialize PDF document
PdfDocument pdfDocument = new PdfDocument();
//Convert Excel document into PDF document
pdfDocument = converter.Convert();
//Save the PDF file
pdfDocument.Save("ExcelToPDF.pdf");
}Using excelEngine As ExcelEngine = New ExcelEngine()
Dim application As IApplication = excelEngine.Excel
application.DefaultVersion = ExcelVersion.Xlsx
Dim workbook As IWorkbook = application.Workbooks.Open("Sample.xlsx", ExcelOpenType.Automatic)
'Open the Excel document to convert
Dim converter As ExcelToPdfConverter = New ExcelToPdfConverter(workbook)
'Initialize the PDF document
Dim pdfDocument As PdfDocument = New PdfDocument()
'Convert Excel document into PDF document
pdfDocument = converter.Convert()
'Save the PDF file
pdfDocument.Save("ExcelToPDF.pdf")
End UsingA complete working example for converting entire Excel workbook to PDF in C# is present on this GitHub page.
To learn more about different conversion settings in Excel To PDF conversion, refer the Excel to PDF conversion settings.
Worksheet to PDF
The following code shows how to convert a particular worksheet to PDF document.
using (ExcelEngine excelEngine = new ExcelEngine())
{
IApplication application = excelEngine.Excel;
application.DefaultVersion = ExcelVersion.Xlsx;
IWorkbook workbook = application.Workbooks.Open(Path.GetFullPath(@"Data/InputTemplate.xlsx"));
IWorksheet worksheet = workbook.Worksheets[0];
//Initialize XlsIO renderer.
XlsIORenderer renderer = new XlsIORenderer();
//Convert Excel document into PDF document
PdfDocument pdfDocument = renderer.ConvertToPDF(worksheet);
#region Save
//Saving the workbook
pdfDocument.Save(Path.GetFullPath("Output/WorksheetToPDF.pdf"));
#endregion
}using (ExcelEngine excelEngine = new ExcelEngine())
{
IApplication application = excelEngine.Excel;
application.DefaultVersion = ExcelVersion.Xlsx;
IWorkbook workbook = application.Workbooks.Open("Sample.xlsx", ExcelOpenType.Automatic);
IWorksheet sheet = workbook.Worksheets[0];
//convert the sheet to PDF
ExcelToPdfConverter converter = new ExcelToPdfConverter(sheet);
PdfDocument pdfDocument = new PdfDocument();
pdfDocument = converter.Convert();
pdfDocument.Save("ExcelToPDF.pdf");
}Using excelEngine As ExcelEngine = New ExcelEngine()
Dim application As IApplication = excelEngine.Excel
application.DefaultVersion = ExcelVersion.Xlsx
Dim workbook As IWorkbook = application.Workbooks.Open("Sample.xlsx", ExcelOpenType.Automatic)
Dim sheet As IWorksheet = workbook.Worksheets(0)
'Converts the particular sheet
Dim converter As ExcelToPdfConverter = New ExcelToPdfConverter(sheet)
Dim pdfDocument As PdfDocument = New PdfDocument()
pdfDocument = converter.Convert()
'Save the PDF file
pdfDocument.Save("ExcelToPDF.pdf")
End UsingA complete working example for converting particular worksheet to PDF in C# is present on this GitHub page.
Creating individual PDF document for each worksheet
The following code snippet shows how to create an individual PDF document for each worksheet in a workbook.
using (ExcelEngine excelEngine = new ExcelEngine())
{
IApplication application = excelEngine.Excel;
application.DefaultVersion = ExcelVersion.Xlsx;
IWorkbook workbook = application.Workbooks.Open(Path.GetFullPath(@"Data/InputTemplate.xlsx"));
//Initialize XlsIO renderer.
XlsIORenderer renderer = new XlsIORenderer();
foreach (IWorksheet sheet in workbook.Worksheets)
{
using (PdfDocument pdfDocument = renderer.ConvertToPDF(sheet))
{
#region Save
//Save each worksheet as a separate PDF.
pdfDocument.Save(sheet.Name + ".pdf");
#endregion
}
}
}using (ExcelEngine excelEngine = new ExcelEngine())
{
IApplication application = excelEngine.Excel;
application.DefaultVersion = ExcelVersion.Xlsx;
IWorkbook workbook = application.Workbooks.Open("Sample.xlsx", ExcelOpenType.Automatic);
foreach (IWorksheet sheet in workbook.Worksheets)
{
ExcelToPdfConverter converter = new ExcelToPdfConverter(sheet);
using (PdfDocument pdfDocument = converter.Convert())
{
//Save the PDF file
pdfDocument.Save(sheet.Name + ".pdf");
}
converter.Dispose();
}
}Using excelEngine As ExcelEngine = New ExcelEngine()
Dim application As IApplication = excelEngine.Excel
application.DefaultVersion = ExcelVersion.Xlsx
Dim workbook As IWorkbook = application.Workbooks.Open("Sample.xlsx")
Dim pdfDocument As New PdfDocument()
For Each sheet As IWorksheet In workbook.Worksheets
Dim converter As New ExcelToPdfConverter(sheet)
Dim pdfDocument As PdfDocument = converter.Convert()
'Save the PDF file
pdfDocument.Save(sheet.Name + ".pdf")
converter.Dispose()
Next
End UsingA complete working example to convert each worksheet into individual PDF in C# is present on this GitHub page.
Excel with chart to PDF
XlsIO supports converting a workbook, a worksheet with charts, or a single chart to a PDF document.
To preserve the charts during Excel-to-PDF conversion in the .NET Framework, initialize the ChartToImageConverter property of the IApplication interface. Otherwise, the charts in the worksheet are skipped.
The following code illustrates how to convert an Excel with chart to PDF document.
using (ExcelEngine excelEngine = new ExcelEngine())
{
IApplication application = excelEngine.Excel;
application.DefaultVersion = ExcelVersion.Xlsx;
IWorkbook workbook = application.Workbooks.Open(Path.GetFullPath(@"Data/InputTemplate.xlsx"));
IWorksheet worksheet = workbook.Worksheets[0];
IChart chart = worksheet.Charts[0];
//Initialize XlsIO renderer.
XlsIORenderer renderer = new XlsIORenderer();
//Convert Excel document with charts into PDF document
PdfDocument pdfDocument = renderer.ConvertToPDF(chart);
#region Save
//Saving the workbook
pdfDocument.Save(Path.GetFullPath("Output/ChartToPDF.pdf"));
#endregion
}using (ExcelEngine excelEngine = new ExcelEngine())
{
IApplication application = excelEngine.Excel;
application.DefaultVersion = ExcelVersion.Xlsx;
//Instantiate the ChartToImageConverter and assign it to the XlsIO application.
application.ChartToImageConverter = new ChartToImageConverter();
//Tune the chart image quality.
application.ChartToImageConverter.ScalingMode = ScalingMode.Best;
IWorkbook workbook = application.Workbooks.Open("chart.xlsx", ExcelOpenType.Automatic);
ExcelToPdfConverter converter = new ExcelToPdfConverter(workbook);
using (PdfDocument pdfDocument = converter.Convert())
{
pdfDocument.Save("ExcelToPDF.pdf");
}
}Using excelEngine As ExcelEngine = New ExcelEngine()
Dim application As IApplication = excelEngine.Excel
application.DefaultVersion = ExcelVersion.Xlsx
'Instantiating the ChartToImageConverter and assigning the ChartToImageConverter instance of XlsIO application
application.ChartToImageConverter = New ChartToImageConverter()
'Tuning chart image quality
application.ChartToImageConverter.ScalingMode = ScalingMode.Best
Dim workbook As IWorkbook = application.Workbooks.Open("chart.xlsx")
Dim converter As New ExcelToPdfConverter(workbook)
Dim pdfDocument As PdfDocument = converter.Convert()
pdfDocument.Save("ExcelToPDF.pdf")
End UsingA complete working example to convert Excel chart to PDF in C# is present on this GitHub page.
Excel with comments (notes) to PDF
XlsIO supports converting a workbook or a worksheet that contains comments (notes) to a PDF document. By default, comments are not converted. To convert the comments in a worksheet, set the print options through the ExcelPrintLocation enumeration. This option supports three modes:
- comments as displayed in place,
- comments at the end of the sheet, and
- without comments.
Convert comments as displayed in place
Comments (notes) will be rendered in the output PDF document as displayed in the Excel file, if the PrintInPlace option is selected. The following code illustrates this.
using (ExcelEngine excelEngine = new ExcelEngine())
{
IApplication application = excelEngine.Excel;
application.DefaultVersion = ExcelVersion.Xlsx;
IWorkbook workbook = application.Workbooks.Open(Path.GetFullPath(@"Data/InputTemplate.xlsx"));
IWorksheet worksheet = workbook.Worksheets[0];
//Set print location to comments
worksheet.PageSetup.PrintComments = ExcelPrintLocation.PrintInPlace;
//Initialize XlsIO renderer.
XlsIORenderer renderer = new XlsIORenderer();
//Convert Excel document into PDF document
PdfDocument pdfDocument = renderer.ConvertToPDF(worksheet);
#region Save
//Saving the workbook
pdfDocument.Save(Path.GetFullPath("Output/CommentsInPlaceToPDF.pdf"));
#endregion
}using (ExcelEngine excelEngine = new ExcelEngine())
{
IApplication application = excelEngine.Excel;
application.DefaultVersion = ExcelVersion.Xlsx;
IWorkbook workbook = application.Workbooks.Open("Sample.xlsx");
IWorksheet worksheet = workbook.Worksheets[0];
//Set print location to comments
worksheet.PageSetup.PrintComments = ExcelPrintLocation.PrintInPlace;
ExcelToPdfConverter converter = new ExcelToPdfConverter(worksheet);
//Initialize PDF document
PdfDocument pdfDocument = new PdfDocument();
//Convert Excel document into PDF document
pdfDocument = converter.Convert();
//Save the PDF file
pdfDocument.Save("ExcelToPDF.pdf");
System.Diagnostics.Process.Start("ExcelToPDF.pdf");
}Using excelEngine As ExcelEngine = New ExcelEngine()
Dim application As IApplication = excelEngine.Excel
application.DefaultVersion = ExcelVersion.Xlsx
Dim workbook As IWorkbook = application.Workbooks.Open("Sample.xlsx", ExcelOpenType.Automatic)
Dim worksheet As IWorksheet = workbook.Worksheets(0)
'Set print location to comments
worksheet.PageSetup.PrintComments = ExcelPrintLocation.PrintInPlace
'Open the Excel document to convert
Dim converter As ExcelToPdfConverter = New ExcelToPdfConverter(worksheet)
'Initialize the PDF document
Dim pdfDocument As PdfDocument = New PdfDocument()
'Convert Excel document into PDF document
pdfDocument = converter.Convert()
'Save the PDF file
pdfDocument.Save("ExcelToPDF.pdf")
End UsingA complete working example to convert Excel with comments to PDF and render comments in place in C# is present on this GitHub page.
The following screenshot represents the input Excel file with notes

The following screenshot represents the output pdf file generated by the XlsIO using PrintInPlace option

Convert comments at the end of the sheet
Comments (notes) will be rendered in the output PDF document at the end of the each sheet which contains the comments (notes), when the PrintSheetEnd option is selected. The following code illustrates this.
using (ExcelEngine excelEngine = new ExcelEngine())
{
IApplication application = excelEngine.Excel;
application.DefaultVersion = ExcelVersion.Xlsx;
IWorkbook workbook = application.Workbooks.Open(Path.GetFullPath(@"Data/InputTemplate.xlsx"));
IWorksheet worksheet = workbook.Worksheets[0];
//Set print location to comments
worksheet.PageSetup.PrintComments = ExcelPrintLocation.PrintSheetEnd;
//Initialize XlsIO renderer.
XlsIORenderer renderer = new XlsIORenderer();
//Convert Excel document into PDF document
PdfDocument pdfDocument = renderer.ConvertToPDF(worksheet);
#region Save
//Saving the workbook
pdfDocument.Save(Path.GetFullPath("Output/CommentsToPDFAtEnd.pdf"));
#endregion
}using (ExcelEngine excelEngine = new ExcelEngine())
{
IApplication application = excelEngine.Excel;
application.DefaultVersion = ExcelVersion.Xlsx;
IWorkbook workbook = application.Workbooks.Open("Sample.xlsx");
IWorksheet worksheet = workbook.Worksheets[0];
//Set print location to comments
worksheet.PageSetup.PrintComments = ExcelPrintLocation.PrintSheetEnd;
ExcelToPdfConverter converter = new ExcelToPdfConverter(worksheet);
//Initialize PDF document
PdfDocument pdfDocument = new PdfDocument();
//Convert Excel document into PDF document
pdfDocument = converter.Convert();
//Save the PDF file
pdfDocument.Save("ExcelToPDF.pdf");
System.Diagnostics.Process.Start("ExcelToPDF.pdf");
}Using excelEngine As ExcelEngine = New ExcelEngine()
Dim application As IApplication = excelEngine.Excel
application.DefaultVersion = ExcelVersion.Xlsx
Dim workbook As IWorkbook = application.Workbooks.Open("Sample.xlsx", ExcelOpenType.Automatic)
Dim worksheet As IWorksheet = workbook.Worksheets(0)
'Set print location to comments
worksheet.PageSetup.PrintComments = ExcelPrintLocation.PrintSheetEnd
'Open the Excel document to convert
Dim converter As ExcelToPdfConverter = New ExcelToPdfConverter(worksheet)
'Initialize the PDF document
Dim pdfDocument As PdfDocument = New PdfDocument()
'Convert Excel document into PDF document
pdfDocument = converter.Convert()
'Save the PDF file
pdfDocument.Save("ExcelToPDF.pdf")
End UsingA complete working example to convert Excel with comments to PDF and render comments at the end in C# is present on this GitHub page.
The following screenshot represents the input Excel file with notes

The following screenshot represents the output pdf file generated by the XlsIO using PrintSheetEnd option


Convert without comments
Comments (notes) will not be displayed in the output PDF document, if the PrintNoComments option is selected. The following code illustrates this.
using (ExcelEngine excelEngine = new ExcelEngine())
{
IApplication application = excelEngine.Excel;
application.DefaultVersion = ExcelVersion.Xlsx;
IWorkbook workbook = application.Workbooks.Open(Path.GetFullPath(@"Data/InputTemplate.xlsx"));
IWorksheet worksheet = workbook.Worksheets[0];
//Set print location to comments
worksheet.PageSetup.PrintComments = ExcelPrintLocation.PrintNoComments;
//Initialize XlsIO renderer.
XlsIORenderer renderer = new XlsIORenderer();
//Convert Excel document into PDF document
PdfDocument pdfDocument = renderer.ConvertToPDF(worksheet);
#region Save
//Saving the workbook
pdfDocument.Save(Path.GetFullPath("Output/NoCommentsInPDF.pdf"));
#endregion
}using (ExcelEngine excelEngine = new ExcelEngine())
{
IApplication application = excelEngine.Excel;
application.DefaultVersion = ExcelVersion.Xlsx;
IWorkbook workbook = application.Workbooks.Open("Sample.xlsx");
IWorksheet worksheet = workbook.Worksheets[0];
//Set print location to comments
worksheet.PageSetup.PrintComments = ExcelPrintLocation.PrintNoComments;
ExcelToPdfConverter converter = new ExcelToPdfConverter(worksheet);
//Initialize PDF document
PdfDocument pdfDocument = new PdfDocument();
//Convert Excel document into PDF document
pdfDocument = converter.Convert();
//Save the PDF file
pdfDocument.Save("ExcelToPDF.pdf");
System.Diagnostics.Process.Start("ExcelToPDF.pdf");
}Using excelEngine As ExcelEngine = New ExcelEngine()
Dim application As IApplication = excelEngine.Excel
application.DefaultVersion = ExcelVersion.Xlsx
Dim workbook As IWorkbook = application.Workbooks.Open("Sample.xlsx", ExcelOpenType.Automatic)
Dim worksheet As IWorksheet = workbook.Worksheets(0)
'Set print location to comments
worksheet.PageSetup.PrintComments = ExcelPrintLocation.PrintNoComments
'Open the Excel document to convert
Dim converter As ExcelToPdfConverter = New ExcelToPdfConverter(worksheet)
'Initialize the PDF document
Dim pdfDocument As PdfDocument = New PdfDocument()
'Convert Excel document into PDF document
pdfDocument = converter.Convert()
'Save the PDF file
pdfDocument.Save("ExcelToPDF.pdf")
End UsingA complete working example to convert Excel with comments to PDF ignoring the comments in C# is present on this GitHub page.
The following screenshot represents the input Excel file with notes

The following screenshot represents the output pdf file generated by the XlsIO using PrintNoComments option

Excel with threaded comments to PDF
XlsIO supports converting threaded comments conversation in Excel document to PDF. To convert the threaded comments to PDF, the print options should be set through ExcelPrintLocation enumeration.
Convert threaded comments at the end of the sheet
Threaded comments will be rendered in the PDF document at the end of the each sheet, when the PrintSheetEnd option is selected. The following code illustrates how to set print options to convert threaded comments.
using (ExcelEngine excelEngine = new ExcelEngine())
{
IApplication application = excelEngine.Excel;
application.DefaultVersion = ExcelVersion.Xlsx;
IWorkbook workbook = application.Workbooks.Open("CommentsTemplate.xlsx");
IWorksheet worksheet = workbook.Worksheets[0];
//Add threaded comment
IThreadedComment threadedComment = worksheet.Range["H16"].AddThreadedComment("What is the reason for the higher total amount of \"desk\" in the west region?", "User1", DateTime.Now);
//Add reply to the threaded comement
threadedComment.AddReply("The unit cost of desk is higher compared to other items in the west region. As a result, the total amount is elevated.", "User2", DateTime.Now);
threadedComment.AddReply("Additionally, Wilson sold 31 desks in the west region, which is also a contributing factor to the increased total amount.", "User3", DateTime.Now);
//Set print location to threaded comments
worksheet.PageSetup.PrintComments = ExcelPrintLocation.PrintSheetEnd;
//Initialize XlsIO renderer.
XlsIORenderer renderer = new XlsIORenderer();
//Convert Excel document into PDF document
PdfDocument pdfDocument = renderer.ConvertToPDF(worksheet);
pdfDocument.Save("ExcelComments.pdf");
}using (ExcelEngine excelEngine = new ExcelEngine())
{
IApplication application = excelEngine.Excel;
application.DefaultVersion = ExcelVersion.Xlsx;
IWorkbook workbook = application.Workbooks.Open("CommentsTemplate.xlsx");
IWorksheet worksheet = workbook.Worksheets[0];
//Add threaded comment
IThreadedComment threadedComment = worksheet.Range["H16"].AddThreadedComment("What is the reason for the higher total amount of \"desk\" in the west region?", "User1", DateTime.Now);
//Add reply to the threaded comement
threadedComment.AddReply("The unit cost of desk is higher compared to other items in the west region. As a result, the total amount is elevated.", "User2", DateTime.Now);
threadedComment.AddReply("Additionally, Wilson sold 31 desks in the west region, which is also a contributing factor to the increased total amount.", "User3", DateTime.Now);
//Set print location to threaded comments
worksheet.PageSetup.PrintComments = ExcelPrintLocation.PrintSheetEnd;
ExcelToPdfConverter converter = new ExcelToPdfConverter(worksheet);
//Initialize PDF document
PdfDocument pdfDocument = new PdfDocument();
//Convert Excel document into PDF document
pdfDocument = converter.Convert();
//Save the PDF document
pdfDocument.Save("ExcelComments.pdf");
}Using excelEngine As ExcelEngine = New ExcelEngine()
Dim application As IApplication = excelEngine.Excel
application.DefaultVersion = ExcelVersion.Xlsx
Dim workbook As IWorkbook = application.Workbooks.Open("CommentsTemplate.xlsx", ExcelOpenType.Automatic)
Dim worksheet As IWorksheet = workbook.Worksheets(0)
'Add threaded comment
Dim threadedComment As IThreadedComment = worksheet.Range("H16").AddThreadedComment("What is the reason for the higher total amount of ""desk"" in the west region?", "User1", DateTime.Now)
'Add reply to the threaded comement
threadedComment.AddReply("The unit cost of desk is higher compared to other items in the west region. As a result, the total amount is elevated.", "User2", DateTime.Now)
threadedComment.AddReply("Additionally, Wilson sold 31 desks in the west region, which is also a contributing factor to the increased total amount.", "User3", DateTime.Now)
'Set print location to threaded comments
worksheet.PageSetup.PrintComments = ExcelPrintLocation.PrintSheetEnd
'Open the Excel document to convert
Dim converter As ExcelToPdfConverter = New ExcelToPdfConverter(workbook)
'Initialize the PDF document
Dim pdfDocument As PdfDocument = New PdfDocument()
'Convert Excel document into PDF document
pdfDocument = converter.Convert()
'Save the PDF document
pdfDocument.Save("ExcelComments.pdf")
End UsingThe following screenshot represents the input Excel document with threaded comments

The following screenshot represents the output pdf document generated by the XlsIO using PrintSheetEnd option

Substitute Font in Excel-to-PDF Conversion
By default, XlsIO substitutes unsupported fonts with Microsoft Sans Serif during Excel-to-PDF conversion. If you need a different fallback, subscribe to the SubstituteFont event on IApplication. The event handler receives the following arguments:
| Argument | Type | Description |
|---|---|---|
| OriginalFontName | string |
Name of the font in the workbook that is not installed on the machine. Read-only. |
| AlternateFontName | string |
Name of an installed font to substitute. Use this when an equivalent system font is available. |
| AlternateFontStream | Stream |
Font stream (for example, an embedded-resource .ttf) to substitute. Use this when no installed font is available. |
The following code illustrates how to perform Excel-to-PDF conversion by substituting unsupported fonts in the machine.
using System;
using System.IO;
using Syncfusion.XlsIO;
using Syncfusion.XlsIORenderer;
using Syncfusion.Pdf;
using System.Reflection;
namespace FontSubstitution
{
class Program
{
static void Main(string[] args)
{
using (ExcelEngine excelEngine = new ExcelEngine())
{
IApplication application = excelEngine.Excel;
application.DefaultVersion = ExcelVersion.Xlsx;
//Initializes the SubstituteFont event to perform font substitution during Excel-to-PDF conversion
application.SubstituteFont += new SubstituteFontEventHandler(SubstituteFont);
//Initialize XlsIO renderer.
XlsIORenderer renderer = new XlsIORenderer();
IWorkbook workbook = application.Workbooks.Open("Template.xlsx");
//Convert Excel document with charts into PDF document
PdfDocument pdfDocument = renderer.ConvertToPDF(workbook);
pdfDocument.Save("ExcelToPDF.pdf");
}
}
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")
args.AlternateFontName = "Arial";
else if (args.OriginalFontName == "Homizio")
{
//Substitute by font stream.
var assembly = Assembly.GetExecutingAssembly();
var resourceName = "ExceltoPDF.Fonts.Homizio.ttf";
Stream fileStream = assembly.GetManifestResourceStream(resourceName);
MemoryStream memoryStream = new MemoryStream();
fileStream.CopyTo(memoryStream);
fileStream.Close();
args.AlternateFontStream = memoryStream;
}
}
}
}using Syncfusion.ExcelToPdfConverter;
using Syncfusion.Pdf;
using Syncfusion.XlsIO;
using Syncfusion.XlsIO.Implementation;
using System.IO;
using System.Reflection;
namespace FontSubstitution
{
class Program
{
static void Main(string[] args)
{
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);
IWorkbook workbook = application.Workbooks.Open("Template.xlsx");
IWorksheet worksheet = workbook.Worksheets[0];
ExcelToPdfConverter converter = new ExcelToPdfConverter(workbook);
PdfDocument pdfDocument = new PdfDocument();
pdfDocument = converter.Convert();
pdfDocument.Save("ExcelToPDF.pdf");
}
}
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";
Stream fileStream = assembly.GetManifestResourceStream(resourceName);
MemoryStream memoryStream = new MemoryStream();
fileStream.CopyTo(memoryStream);
fileStream.Close();
args.AlternateFontStream = memoryStream;
}
}
}
}Imports Syncfusion.ExcelToPdfConverter
Imports Syncfusion.Pdf
Imports Syncfusion.XlsIO
Imports Syncfusion.XlsIO.Implementation
Imports System.IO
Imports System.Reflection
Namespace FontSubstitution
Class Program
Private Shared Sub Main(ByVal args() As String)
Dim excelEngine As ExcelEngine = New ExcelEngine
Dim application As IApplication = excelEngine.Excel
application.DefaultVersion = ExcelVersion.Xlsx
'Initializes the SubstituteFont event to perform font substitution in Excel-to-PDF conversion.
AddHandler application.SubstituteFont, AddressOf Me.SubstituteFont
Dim workbook As IWorkbook = application.Workbooks.Open("Template.xlsx")
Dim worksheet As IWorksheet = workbook.Worksheets(0)
Dim converter As ExcelToPdfConverter = New ExcelToPdfConverter(workbook)
Dim pdfDocument As PdfDocument = New PdfDocument
pdfDocument = converter.Convert
pdfDocument.Save("ExcelToPDF.pdf")
End Sub
Private Shared Sub SubstituteFont(ByVal sender As Object, ByVal args As SubstituteFontEventArgs)
'Substitute a font if the specified font is not installed in the machine.
If (args.OriginalFontName = "Arial Unicode MS") Then
'Substitute by font name.
args.AlternateFontName = "Arial"
ElseIf (args.OriginalFontName = "Homizio") Then
'Substitute by font stream.
Dim assembly = Assembly.GetExecutingAssembly
Dim resourceName = "ExceltoPDF.Fonts.Homizio.ttf"
Dim fileStream As Stream = assembly.GetManifestResourceStream(resourceName)
Dim memoryStream As MemoryStream = New MemoryStream
fileStream.CopyTo(memoryStream)
fileStream.Close
args.AlternateFontStream = memoryStream
End If
End Sub
End Class
End NamespaceExcel to PDF conversion in Linux OS
In Linux OS, the Excel to PDF conversion can be performed using .NET Core (Targeting .NET core app) application. Please refer Excel to PDF conversion NuGet packages to know about the packages required to deploy .NET Core (Targeting .NET core app) application with Excel to PDF conversion capabilities.
In addition to the previous NuGet packages, the SkiaSharp.Linux helper NuGet package is required. It can be generated with the following steps:
- Download
libSkiaSharp.sofrom the SkiaSharp release page. The native binary should be placed in the folder structureSkiaSharp.Linux/runtimes/linux-x64/native/libSkiaSharp.so(the filename must be exactlylibSkiaSharp.so). - Create a folder named
SkiaSharp.Linuxcontaining theruntimes/linux-x64/native/libSkiaSharp.sofile from step 1. - Create a nuspec file named
SkiaSharp.Linux.nuspecinside theSkiaSharp.Linuxfolder using the following metadata. The nuspec can be customized.
<?xml version="1.0" encoding="utf-8"?>
<package xmlns="http://schemas.microsoft.com/packaging/2012/06/nuspec.xsd">
<metadata>
<id>SkiaSharp.Linux</id>
<version>3.119.1</version>
<title>SkiaSharp for Linux</title>
<authors>Syncfusion Inc.</authors>
<owners>Syncfusion Inc.</owners>
<requireLicenseAcceptance>false</requireLicenseAcceptance>
<description>SkiaSharp for Linux is a supporting package for Linux platforms.</description>
<tags>linux,cross-platform,skiasharp,net-standard,net-core,excel-to-pdf</tags>
<dependencies>
<group targetFramework=".NETStandard1.4">
<dependency id="SkiaSharp" version="3.119.1" />
</group>
</dependencies>
</metadata>
</package>- Make sure that the nuget.exe file is present along with SkiaSharp.Linux folder (in the parent folder of SkiaSharp.Linux folder). If not, download it from here.
- Open a command prompt and navigate to SkiaSharp.Linux folder.
- Execute the following command.
nuget pack SkiaSharp.Linux\SkiaSharp.Linux.nuspec -outputdirectory "C:\NuGet"
The output directory can be customized as per your need.
Now, SkiaSharp.Linux NuGet will be generated in the mentioned output directory and add the generated NuGet as additional reference. SkiaSharp.Linux NuGet package can also be downloaded from here.
Print Excel document
XlsIO supports printing Excel documents by first converting them to PDF and then sending the PDF to the printer. An Excel document can be printed with the specified page setup and printer settings in XlsIO.
The following printer settings can be applied when printing an Excel document.

Print Excel document
The following code snippet illustrates how to print the Excel document in XlsIO.
//XlsIO supports Excel To PDF conversion in Windows Forms, WPF, ASP.NET, and ASP.NET MVC platforms. Refer to the Workbook to PDF section to convert using web service.using (ExcelEngine excelEngine = new ExcelEngine())
{
IApplication application = excelEngine.Excel;
application.DefaultVersion = ExcelVersion.Xlsx;
IWorkbook workbook = application.Workbooks.Open("Excel.xlsx"));
// Convert the workbook
ExcelToPdfConverter converter = new ExcelToPdfConverter(workbook);
// Print the converted PDF document
converter.Print();
}Using excelEngine As ExcelEngine = New ExcelEngine()
Dim application As IApplication = excelEngine.Excel
application.DefaultVersion = ExcelVersion.Xlsx
Dim workbook As IWorkbook = application.Workbooks.Open("Excel.xlsx")
'Convert the workbook
Dim converter As ExcelToPdfConverter = New ExcelToPdfConverter(workbook)
'Print the converted PDF document
converter.Print()
End UsingPrint with printer settings
The following code snippet illustrates how to print the Excel document with printer settings in XlsIO.
//XlsIO supports Excel To PDF conversion in Windows Forms, WPF, ASP.NET, and ASP.NET MVC platforms. Refer to the Workbook to PDF section to convert using web service.using (ExcelEngine excelEngine = new ExcelEngine())
{
IApplication application = excelEngine.Excel;
application.DefaultVersion = ExcelVersion.Xlsx;
IWorkbook workbook = application.Workbooks.Open("Excel.xlsx"));
//Convert the workbook
ExcelToPdfConverter converter = new ExcelToPdfConverter(workbook);
//Initialize the printer settings
PrinterSettings printerSettings = new PrinterSettings();
//customizing the printer settings
printerSettings.PrinterName = "HP LaserJet Pro MFP M127-M128 PCLmS";
printerSettings.Copies = 2;
printerSettings.FromPage = 2;
printerSettings.ToPage = 3;
printerSettings.DefaultPageSettings.Color = false;
printerSettings.Duplex = Duplex.Vertical;
//Print the converted PDF document with printer settings
converter.Print(printerSettings);
}Using excelEngine As ExcelEngine = New ExcelEngine()
Dim application As IApplication = excelEngine.Excel
application.DefaultVersion = ExcelVersion.Xlsx
Dim workbook As IWorkbook = application.Workbooks.Open("Excel.xlsx")
'Convert the workbook
Dim converter As ExcelToPdfConverter = New ExcelToPdfConverter(workbook)
'Initialize the printer settings
Dim printerSettings As PrinterSettings = New PrinterSettings
'customizing the printer settings
printerSettings.PrinterName = "HP LaserJet Pro MFP M127-M128 PCLmS"
printerSettings.Copies = 2
printerSettings.FromPage = 2
printerSettings.ToPage = 3
printerSettings.DefaultPageSettings.Color = false
printerSettings.Duplex = Duplex.Vertical
'Print the converted PDF document with printer settings
converter.Print(printerSettings)
End UsingPrint with Excel To PDF converter settings
The following code snippet illustrates how to print the Excel document with Excel To PDF converter settings in XlsIO.
//XlsIO supports Excel To PDF conversion in Windows Forms, WPF, ASP.NET, and ASP.NET MVC platforms. Refer to the Workbook to PDF section to convert using web service.using (ExcelEngine excelEngine = new ExcelEngine())
{
IApplication application = excelEngine.Excel;
application.DefaultVersion = ExcelVersion.Xlsx;
IWorkbook workbook = application.Workbooks.Open("Excel.xlsx");
//Convert the workbook
ExcelToPdfConverter converter = new ExcelToPdfConverter(workbook);
//Initializes the Excel To PDF converter setting class
ExcelToPdfConverterSettings converterSettings = new ExcelToPdfConverterSettings();
//Layout the page using FitAllColumnsOnOnePage options
converterSettings.DisplayGridLines = GridLinesDisplayStyle.Visible;
converterSettings.LayoutOptions = LayoutOptions.FitAllColumnsOnOnePage;
//Print the converted PDF document with converter settings
converter.Print(converterSettings);
}Using excelEngine As ExcelEngine = New ExcelEngine()
Dim application As IApplication = excelEngine.Excel
application.DefaultVersion = ExcelVersion.Xlsx
Dim workbook As IWorkbook = application.Workbooks.Open("Excel.xlsx")
'Convert the workbook
Dim converter As ExcelToPdfConverter = New ExcelToPdfConverter(workbook)
'Initialize the Excel To PDF converter setting class
Dim converterSettings As ExcelToPdfConverterSettings = New ExcelToPdfConverterSettings
'Layout the page using FitAllColumnsOnOnePage options
converterSettings.DisplayGridLines = GridLinesDisplayStyle.Visible
converterSettings.LayoutOptions = LayoutOptions.FitAllColumnsOnOnePage
'Print the converted PDF document with converter settings
converter.Print(converterSettings)
End UsingPrint with Excel To PDF converter and printer settings
The following code snippet illustrates how to print the Excel document with Excel To PDF converter settings and printer settings in XlsIO.
//XlsIO supports Excel To PDF conversion in Windows Forms, WPF, ASP.NET, and ASP.NET MVC platforms. Refer to the Workbook to PDF section to convert using web service.using (ExcelEngine excelEngine = new ExcelEngine())
{
IApplication application = excelEngine.Excel;
application.DefaultVersion = ExcelVersion.Xlsx;
IWorkbook workbook = application.Workbooks.Open("Excel.xlsx"));
ExcelToPdfConverter converter = new ExcelToPdfConverter(workbook);
//Initialize the printer settings
PrinterSettings printerSettings = new PrinterSettings();
//customizing the printer settings
printerSettings.PrinterName = "HP LaserJet Pro MFP M127-M128 PCLmS";
printerSettings.Copies = 2;
printerSettings.FromPage = 2;
printerSettings.ToPage = 3;
printerSettings.DefaultPageSettings.Color = true;
printerSettings.Duplex = Duplex.Vertical;
printerSettings.Collate = true;
//Initializes the Excel To PDF converter setting class
ExcelToPdfConverterSettings converterSettings = new ExcelToPdfConverterSettings();
//Layout the page using FitAllColumnsOnOnePage options
converterSettings.DisplayGridLines = GridLinesDisplayStyle.Visible;
converterSettings.LayoutOptions = LayoutOptions.FitAllColumnsOnOnePage;
//Print the converted PDF document with printer settings and converter settings
converter.Print(printerSettings, converterSettings);
}Using excelEngine As ExcelEngine = New ExcelEngine()
Dim application As IApplication = excelEngine.Excel
application.DefaultVersion = ExcelVersion.Xlsx
Dim workbook As IWorkbook = application.Workbooks.Open("Excel.xlsx")
Dim converter As ExcelToPdfConverter = New ExcelToPdfConverter(workbook)
'Initialize the printer settings
Dim printerSettings As PrinterSettings = New PrinterSettings
'customizing the printer settings
printerSettings.PrinterName = "HP LaserJet Pro MFP M127-M128 PCLmS"
printerSettings.Copies = 2
printerSettings.FromPage = 2
printerSettings.ToPage = 3
printerSettings.DefaultPageSettings.Color = true
printerSettings.Duplex = Duplex.Vertical
printerSettings.Collate = true
'Initialize the Excel To PDF converter setting class
Dim converterSettings As ExcelToPdfConverterSettings = New ExcelToPdfConverterSettings
'Layout the page using FitAllColumnsOnOnePage options
converterSettings.DisplayGridLines = GridLinesDisplayStyle.Visible
converterSettings.LayoutOptions = LayoutOptions.FitAllColumnsOnOnePage
'Print the converted PDF document with printer settings and converter settings
converter.Print(printerSettings, converterSettings)
End UsingNOTE
Printing is supported only in Windows Forms and WPF platforms.
Fallback fonts
During Excel to PDF conversions, if a glyph of the input text is unavailable in the specified font, the text will not be rendered properly. To address this, the Syncfusion® Excel (XlsIO) library allows users to specify fallback fonts. When a glyph is missing, the library will use one of the fallback fonts to render the text correctly in the output PDF document.
Users can configure fallback fonts in the following ways:
- Initialize default fallback fonts.
- Set custom fonts as fallback fonts for specific script types, including Arabic, Hebrew, Chinese, Japanese, and more.
- Set custom fonts as fallback fonts for a particular range of Unicode text.
NOTE
XlsIO internally uses user-initialized or specified fallback fonts for Unicode characters during Excel to PDF conversion. Therefore, the specified fallback fonts must be installed in the production environment or embedded in the input Excel document (xlsx). Otherwise, it will not render the text properly using the fallback fonts.
Initialize default fallback fonts
The following code example demonstrates how to initialize a default fallback fonts while converting an Excel document to PDF. The InitializeDefault API sets the default fallback fonts for specific script types like Arabic, Hebrew, Chinese, Japanese etc.
using (ExcelEngine excelEngine = new ExcelEngine())
{
IApplication application = excelEngine.Excel;
application.DefaultVersion = ExcelVersion.Xlsx;
IWorkbook workbook = application.Workbooks.Open(Path.GetFullPath(@"Data/InputTemplate.xlsx"));
//Initialize XlsIORenderer
application.XlsIORenderer = new XlsIORenderer();
//Initialize fallBack font
application.FallbackFonts.InitializeDefault();
//Initialize XlsIO renderer.
XlsIORenderer renderer = new XlsIORenderer();
//Convert Excel document into PDF document
PdfDocument pdfDocument = renderer.ConvertToPDF(workbook);
//Save the converted PDF document
pdfDocument.Save("Sample.pdf");
//Close and Dispose
workbook.Close();
}using (ExcelEngine excelEngine = new ExcelEngine())
{
IApplication application = excelEngine.Excel;
application.DefaultVersion = ExcelVersion.Xlsx;
IWorkbook workbook = application.Workbooks.Open("Sample.xlsx");
//Convert Excel document into PDF document
ExcelToPdfConverter converter = new ExcelToPdfConverter(workbook);
//Initialize fallback font
application.FallbackFonts.InitializeDefault();
//Initialize PdfDocument
PdfDocument pdfDocument = new PdfDocument();
pdfDocument = converter.Convert();
//Save the converted PDF.
pdfDocument.Save("ExcelToPDF.pdf");
}Using excelEngine As ExcelEngine = New ExcelEngine()
Dim application As IApplication = excelEngine.Excel
application.DefaultVersion = ExcelVersion.Xlsx
Dim workbook As IWorkbook = application.Workbooks.Open("Sample.xlsx")
'Convert Excel document into PDF document
Dim converter As ExcelToPdfConverter = New ExcelToPdfConverter(workbook)
'Initialize fallback font
application.FallbackFonts.InitializeDefault()
'Initialize PdfDocument
Dim pdfDocument As PdfDocument = New PdfDocument()
pdfDocument = converter.Convert()
'Save the converted PDF.
pdfDocument.Save("ExcelToPDF.pdf")
End UsingA complete working example for Initialize default fallback fonts in C# is present on this GitHub page.
Fallback fonts based on script type
The following code example demonstrates how a user can add fallback fonts based on the script types, which XlsIO considers internally when converting an Excel document to PDF.
using (ExcelEngine excelEngine = new ExcelEngine())
{
IApplication application = excelEngine.Excel;
application.DefaultVersion = ExcelVersion.Xlsx;
IWorkbook workbook = application.Workbooks.Open(Path.GetFullPath(@"Data/InputTemplate.xlsx"));
//Initialize XlsIORenderer
application.XlsIORenderer = new XlsIORenderer();
//Initialize fallBack font
application.FallbackFonts.InitializeDefault();
//Adds fallback font for "Arabic" script type.
application.FallbackFonts.Add(ScriptType.Arabic, "Arial, Times New Roman");
//Adds fallback font for "Hebrew" script type.
application.FallbackFonts.Add(ScriptType.Hebrew, "Arial, Courier New");
//Adds fallback font for "Thai" script type.
application.FallbackFonts.Add(ScriptType.Thai, "Tahoma, Microsoft Sans Serif");
//Adds fallback font for "Korean" script type.
application.FallbackFonts.Add(ScriptType.Korean, "Malgun Gothic, Batang");
//Initialize XlsIO renderer.
XlsIORenderer renderer = new XlsIORenderer();
//Convert Excel document into PDF document
PdfDocument pdfDocument = renderer.ConvertToPDF(workbook);
//Save the converted PDF document
pdfDocument.Save("Sample.pdf");
//Close and Dispose
workbook.Close();
}using (ExcelEngine excelEngine = new ExcelEngine())
{
IApplication application = excelEngine.Excel;
application.DefaultVersion = ExcelVersion.Xlsx;
IWorkbook workbook = application.Workbooks.Open("Sample.xlsx");
//Convert Excel document into PDF document
ExcelToPdfConverter converter = new ExcelToPdfConverter(workbook);
//Initialize fallBack font
application.FallbackFonts.InitializeDefault();
//Adds fallback font for "Arabic" script type.
application.FallbackFonts.Add(ScriptType.Arabic, "Arial, Times New Roman");
//Adds fallback font for "Hebrew" script type.
application.FallbackFonts.Add(ScriptType.Hebrew, "Arial, Courier New");
//Adds fallback font for "Thai" script type.
application.FallbackFonts.Add(ScriptType.Thai, "Tahoma, Microsoft Sans Serif");
//Adds fallback font for "Korean" script type.
application.FallbackFonts.Add(ScriptType.Korean, "Malgun Gothic, Batang");
//Initialize PdfDocument
PdfDocument pdfDocument = new PdfDocument();
pdfDocument = converter.Convert();
//Save the converted PDF.
pdfDocument.Save("ExcelToPDFScript.pdf");
}Using excelEngine As ExcelEngine = New ExcelEngine()
Dim application As IApplication = excelEngine.Excel
application.DefaultVersion = ExcelVersion.Xlsx
Dim workbook As IWorkbook = application.Workbooks.Open("Sample.xlsx")
'Convert Excel document into PDF document
Dim converter As ExcelToPdfConverter = New ExcelToPdfConverter(workbook)
'Initialize fallback font
application.FallbackFonts.InitializeDefault()
'Adds fallback font for "Arabic" script type.
application.FallbackFonts.Add(ScriptType.Arabic, "Arial, Times New Roman")
'Adds fallback font for "Hebrew" script type.
application.FallbackFonts.Add(ScriptType.Hebrew, "Arial, Courier New")
'Adds fallback font for "Thai" script type.
application.FallbackFonts.Add(ScriptType.Thai, "Tahoma, Microsoft Sans Serif")
'Adds fallback font for "Korean" script type.
application.FallbackFonts.Add(ScriptType.Korean, "Malgun Gothic, Batang")
'Intialize PdfDocument
Dim pdfDocument As PdfDocument = New PdfDocument()
pdfDocument = converter.Convert()
'Save the converted PDF.
pdfDocument.Save("ExcelToPDF.pdf")
End UsingA complete working example for Fallback fonts based on script type in C# is present on this GitHub page.
Fallback fonts for range of Unicode text
Users can set fallback fonts for specific Unicode range of text to be used in Excel to PDF conversion.
The following code example demonstrates how users can add fallback fonts by using a specific Unicode range of text that XlsIO considers internally while converting an Excel document to PDF.
using (ExcelEngine excelEngine = new ExcelEngine())
{
IApplication application = excelEngine.Excel;
application.DefaultVersion = ExcelVersion.Xlsx;
IWorkbook workbook = application.Workbooks.Open(Path.GetFullPath(@"Data/InputTemplate.xlsx"));
//Initialize XlsIORenderer
application.XlsIORenderer = new XlsIORenderer();
//Initialize fallBack font
application.FallbackFonts.InitializeDefault();
//Adds fallback font for "Arabic" specific unicode range.
application.FallbackFonts.Add(new FallbackFont(0x0600,0x06ff,"Arial"));
//Adds fallback font for "Hebrew" specific unicode range.
application.FallbackFonts.Add(new FallbackFont(0x0590, 0x05ff, "Times New Roman"));
//Adds fallback font for "Thai" specific unicode range.
application.FallbackFonts.Add(new FallbackFont(0x0E00, 0x0E7F, "Tahoma"));
//Adds fallback font for "Korean" specific unicode range.
application.FallbackFonts.Add(new FallbackFont(0xAC00, 0xD7A3, "Malgun Gothic"));
//Initialize XlsIO renderer.
XlsIORenderer renderer = new XlsIORenderer();
//Convert Excel document into PDF document
PdfDocument pdfDocument = renderer.ConvertToPDF(workbook);
//Excel to PDF
pdfDocument.Save("Sample.pdf");
//Close and Dispose
workbook.Close();
}using (ExcelEngine excelEngine = new ExcelEngine())
{
IApplication application = excelEngine.Excel;
application.DefaultVersion = ExcelVersion.Xlsx;
IWorkbook workbook = application.Workbooks.Open("Sample.xlsx");
//Convert Excel document into PDF document
ExcelToPdfConverter converter = new ExcelToPdfConverter(workbook);
//Initialize fallBack font
application.FallbackFonts.InitializeDefault();
//Adds fallback font for "Arabic" specific unicode range.
application.FallbackFonts.Add(new FallbackFont(0x0600, 0x06ff, "Arial"));
//Adds fallback font for "Hebrew" specific unicode range.
application.FallbackFonts.Add(new FallbackFont(0x0590, 0x05ff, "Times New Roman"));
//Adds fallback font for "Thai" specific unicode range.
application.FallbackFonts.Add(new FallbackFont(0x0E00, 0x0E7F, "Tahoma"));
//Adds fallback font for "Korean" specific unicode range.
application.FallbackFonts.Add(new FallbackFont(0xAC00, 0xD7A3, "Malgun Gothic"));
//Initialize PdfDocument
PdfDocument pdfDocument = new PdfDocument();
pdfDocument = converter.Convert();
//Save the converted PDF.
pdfDocument.Save("ExcelToPDF.pdf");
}Using excelEngine As ExcelEngine = New ExcelEngine()
Dim application As IApplication = excelEngine.Excel
application.DefaultVersion = ExcelVersion.Xlsx
Dim workbook As IWorkbook = application.Workbooks.Open("Sample.xlsx")
'Convert Excel document into PDF document
Dim converter As ExcelToPdfConverter = New ExcelToPdfConverter(workbook)
'Initialize fallback font
application.FallbackFonts.InitializeDefault()
'Adds fallback font for "Arabic" specific unicode range.
application.FallbackFonts.Add(New FallbackFont(&H600, &H6FF, "Arial"))
'Adds fallback font for "Hebrew" specific unicode range.
application.FallbackFonts.Add(New FallbackFont(&H590, &H5FF, "Times New Roman"))
'Adds fallback font for "Thai" specific unicode range.
application.FallbackFonts.Add(New FallbackFont(&HE00, &HE7F, "Tahoma"))
'Adds fallback font for "Korean" specific unicode range.
application.FallbackFonts.Add(New FallbackFont(&HAC00, &HD7A3, "Malgun Gothic"))
'Intialize PdfDocument
Dim pdfDocument As PdfDocument = New PdfDocument()
pdfDocument = converter.Convert()
'Save the converted PDF.
pdfDocument.Save("ExcelToPDF.pdf")
End UsingA complete working example for Fallback fonts for range of Unicode text in C# is present on this GitHub page.
Modify the existing fallback fonts
The following code example demonstrates how user can modify or customize the existing fallback fonts using FontNames API while converting an Excel document to PDF.
using (ExcelEngine excelEngine = new ExcelEngine())
{
IApplication application = excelEngine.Excel;
application.DefaultVersion = ExcelVersion.Xlsx;
IWorkbook workbook = application.Workbooks.Open(Path.GetFullPath(@"Data/InputTemplate.xlsx"));
//Initialize XlsIORenderer
application.XlsIORenderer = new XlsIORenderer();
//Initialize fallBack font
application.FallbackFonts.InitializeDefault();
FallbackFonts fallbackFonts = application.FallbackFonts;
foreach(FallbackFont fallbackFont in fallbackFonts)
{
//Customize a default fallback font name as "David" for the Hebrew script.
if (fallbackFont.ScriptType == ScriptType.Hebrew)
fallbackFont.FontNames = "David";
}
//Initialize XlsIO renderer.
XlsIORenderer renderer = new XlsIORenderer();
//Convert Excel document into PDF document
PdfDocument pdfDocument = renderer.ConvertToPDF(workbook);
//Save the PDF document
pdfDocument.Save("Sample.pdf");
//Close and Dispose
workbook.Close();
}using (ExcelEngine excelEngine = new ExcelEngine())
{
IApplication application = excelEngine.Excel;
application.DefaultVersion = ExcelVersion.Xlsx;
IWorkbook workbook = application.Workbooks.Open("Sample.xlsx");
//Convert Excel document into PDF document
ExcelToPdfConverter converter = new ExcelToPdfConverter(workbook);
//Initialize fallBack font
application.FallbackFonts.InitializeDefault();
FallbackFonts fallbackFonts = application.FallbackFonts;
foreach (FallbackFont fallbackFont in fallbackFonts)
{
//Customize a default fallback font name as "David" for the Hebrew script.
if (fallbackFont.ScriptType == ScriptType.Hebrew)
fallbackFont.FontNames = "David";
}
//Initialize PdfDocument
PdfDocument pdfDocument = new PdfDocument();
pdfDocument = converter.Convert();
//Save the converted PDF.
pdfDocument.Save("ExcelToPDFModify.pdf");
}Using excelEngine As ExcelEngine = New ExcelEngine()
Dim application As IApplication = excelEngine.Excel
application.DefaultVersion = ExcelVersion.Xlsx
Dim workbook As IWorkbook = application.Workbooks.Open("Sample.xlsx")
'Convert Excel document into PDF document
Dim converter As ExcelToPdfConverter = New ExcelToPdfConverter(workbook)
'Initialize fallback font
application.FallbackFonts.InitializeDefault()
Dim fallbackFonts As FallbackFonts = application.FallbackFonts
For Each fallbackFont As FallbackFont In fallbackFonts
' Customize a default fallback font name as "David" for the Hebrew script.
If fallbackFont.ScriptType = ScriptType.Hebrew Then
fallbackFont.FontNames = "David"
End If
Next
'Intialize PdfDocument
Dim pdfDocument As PdfDocument = New PdfDocument()
pdfDocument = converter.Convert()
'Save the converted PDF.
pdfDocument.Save("ExcelToPDF.pdf")
End UsingA complete working example for Modify the existing fallback fonts in C# is present on this GitHub page.
Supported script types
The following table illustrates the supported script types by the .NET Excel Library in Excel to PDF conversion.
| Script types | Ranges | Default fallback fonts considered in InitializeDefault() API |
|---|---|---|
| Arabic |
0x0600 - 0x06ff 0x0750 - 0x077f 0x08a0 - 0x08ff 0xfb50 - 0xfdff 0xfe70 - 0xfeff |
Arial, Times New Roman, Microsoft Uighur |
| Hebrew |
0x0590 - 0x05ff 0xfb1d - 0xfb4f |
Arial, Times New Roman, David |
| Hindi |
0x0900 - 0x097F 0xa8e0 - 0xa8ff 0x1cd0 - 0x1cff |
Mangal, Utsaah |
| Chinese |
0x4E00 - 0x9FFF 0x3400 - 0x4DBF 0xd840 - 0xd869 0xdc00 - 0xdedf 0xA960 - 0xA97F 0xFF00 - 0xFFEF 0x3000 - 0x303F |
DengXian, MingLiU, MS Gothic |
| Japanese |
0x30A0 - 0x30FF 0x3040 - 0x309F |
Yu Mincho, MS Mincho |
| Thai | 0x0E00 - 0x0E7F | Tahoma, Microsoft Sans Serif |
| Korean |
0xAC00 - 0xD7A3 0x1100 - 0x11FF 0x3130 - 0x318F 0xA960 - 0xA97F 0xD7B0 - 0xD7FF 0xAC00 - 0xD7AF |
Malgun Gothic, Batang |
Supported elements
This feature supports the following elements:
- Styles
- Rich-text formatting
- Headers and footers
- Images
- Picture recolor
- Black and white
- Color change
- DuoTone
- Gray scale
- Text boxes
- Hyperlinks
- Document properties
- Table styles
- Text rotations
- Excel page setup options
- Unicode
- Print titles
- Page breaks
- Print area
- Print order
- 2D charts
- 3D charts
- AutoShapes
- Group shapes
- Conditional formats
- Pivot tables
- Comments
- Form controls
- Check box
- Combo box
- Option button
- Linear and Radial Gradient fill
- Cells
- Shapes
Unsupported elements
The following list contains unsupported elements that presently not preserved in the generated PDF document:
- Sparklines
- Pivot charts
- SmartArt graphics
- Different first page headers
- Different odd and even pages
- Tables
- Custom styles
- Row and column headings
- ActiveX controls
- OLE objects
NOTE
Explore our .NET Excel Library Feature Tour page and .NET Excel Framework demo that shows how to create and modify Excel files from C# with 5 lines of code on different platforms.
Next Steps
- Excel to PDF Converter Settings \u2014 customize layout, embedding, and image quality.
- Assemblies Required for Excel to PDF conversion \u2014 manual assembly references for non-NuGet scenarios.
- NuGet Packages Required for Excel to PDF conversion \u2014 install the right package for your target platform.
- Licensing overview \u2014 register your license key.