diff --git a/Document-Processing/Excel/Conversions/Excel-to-Image/NET/Worksheet-to-Image-Conversion.md b/Document-Processing/Excel/Conversions/Excel-to-Image/NET/Worksheet-to-Image-Conversion.md index 364707cc1c..d808eb7b88 100644 --- a/Document-Processing/Excel/Conversions/Excel-to-Image/NET/Worksheet-to-Image-Conversion.md +++ b/Document-Processing/Excel/Conversions/Excel-to-Image/NET/Worksheet-to-Image-Conversion.md @@ -6,7 +6,7 @@ control: XlsIO documentation: UG --- -# Worksheet to Image Conversion in .NET Excel Library +# Excel Worksheet to Image Conversion in .NET Excel Library ## Assemblies Required @@ -166,6 +166,156 @@ N> 3. Worksheet to image conversion is supported from .NET Framework 2.0 and .NE * Shrink to fit * Complex conditional formatting +## Register custom fonts + +The .NET Excel library allows TrueType (`.ttf`) and OpenType (`.otf`) font files to be registered as custom fonts. The registered fonts are used during Excel-to-Image conversion when the required fonts are not installed on the system. + +The following code example shows how to register custom fonts and use them during Excel-to-Image conversion. + +{% tabs %} +{% highlight c# tabtitle="C# [Cross-platform]" playgroundButtonLink="https://raw.githubusercontent.com/SyncfusionExamples/XlsIO-Examples/master/Worksheet%20to%20Image/Custom%20Font/.NET/Custom%20Font/Custom%20Font/Program.cs,180" %} +// Create a collection to store the custom font streams +List fontStreams = new List(); + +// Retrieve all font files from the specified directory +foreach (string file in Directory.GetFiles(Path.GetFullPath(@"Data/MyFonts"))) +{ + string extension = Path.GetExtension(file); + + // Load only TrueType and OpenType font files + if (extension.Equals(".ttf", StringComparison.OrdinalIgnoreCase) || + extension.Equals(".otf", StringComparison.OrdinalIgnoreCase)) + { + // Read the font file and copy its content to a memory stream + FileStream fileStream = new FileStream(file, FileMode.OpenOrCreate, FileAccess.Read); + Stream stream = new MemoryStream(); + fileStream.CopyTo(stream); + stream.Position = 0; + fontStreams.Add(stream); + } +} + +// Register the custom fonts for use during Excel-to-Image conversion +FontManager.RegisterFonts(fontStreams); + +using (ExcelEngine excelEngine = new ExcelEngine()) +{ + IApplication application = excelEngine.Excel; + application.DefaultVersion = ExcelVersion.Xlsx; + + IWorkbook workbook = application.Workbooks.Open(Path.GetFullPath(@"Data/Input.xlsx")); + IWorksheet sheet = workbook.Worksheets[0]; + + application.XlsIORenderer = new XlsIORenderer(); + + using (FileStream outputStream = new FileStream(Path.GetFullPath("Output/Image.png"), FileMode.Create, FileAccess.Write)) + { + sheet.ConvertToImage(sheet.UsedRange, outputStream); + } +} + +// Clear the registered fonts and dispose their associated streams +FontManager.ClearRegisteredFonts(true); +{% endhighlight %} + +{% highlight c# tabtitle="C# [Windows-specific]" %} +// Create a collection to store the custom font streams +List fontStreams = new List(); + +// Retrieve all font files from the specified directory +foreach (string file in Directory.GetFiles(Path.GetFullPath(@"Data/MyFonts"))) +{ + string extension = Path.GetExtension(file); + + // Load only TrueType and OpenType font files + if (extension.Equals(".ttf", StringComparison.OrdinalIgnoreCase) || + extension.Equals(".otf", StringComparison.OrdinalIgnoreCase)) + { + // Read the font file and copy its content to a memory stream + FileStream fileStream = new FileStream(file, FileMode.OpenOrCreate, FileAccess.Read); + Stream stream = new MemoryStream(); + fileStream.CopyTo(stream); + stream.Position = 0; + fontStreams.Add(stream); + } +} + +// Register the custom fonts for use during Excel-to-Image conversion +FontManager.RegisterFonts(fontStreams); + +using (ExcelEngine excelEngine = new ExcelEngine()) +{ + IApplication application = excelEngine.Excel; + application.DefaultVersion = ExcelVersion.Xlsx; + + IWorkbook workbook = application.Workbooks.Open(Path.GetFullPath("Data/Input.xlsx")); + + IWorksheet worksheet = workbook.Worksheets[0]; + + // Convert the specified Excel range to an image + Image image = worksheet.ConvertToImage(1, 1, 20, 7); + + image.Save(Path.GetFullPath("Output/Sample.jpeg"),ImageFormat.Jpeg); + + workbook.Close(); +} + +// Clear the registered fonts and dispose their associated streams +FontManager.ClearRegisteredFonts(true); +{% endhighlight %} + +{% highlight vb.net tabtitle="VB.NET [Windows-specific]" %} +' Create a collection to store the custom font streams +Dim fontStreams As New List(Of Stream)() + +' Retrieve all font files from the specified directory +For Each file As String In Directory.GetFiles(Path.GetFullPath("Data/MyFonts")) + + Dim extension As String = Path.GetExtension(file) + + ' Load only TrueType and OpenType font files. + If extension.Equals(".ttf", StringComparison.OrdinalIgnoreCase) OrElse +extension.Equals(".otf", StringComparison.OrdinalIgnoreCase) Then + + ' Read the font file and copy its content to a memory stream + Dim fileStream As New FileStream(file, FileMode.OpenOrCreate, FileAccess.Read) + + Dim stream As Stream = New MemoryStream() + fileStream.CopyTo(stream) + stream.Position = 0 + fontStreams.Add(stream) + + fileStream.Dispose() + End If +Next + +' Register the custom fonts for use during Excel-to-image conversion +FontManager.RegisterFonts(fontStreams) + +Using excelEngine As New ExcelEngine() + Dim application As IApplication = excelEngine.Excel + application.DefaultVersion = ExcelVersion.Xlsx + + Dim workbook As IWorkbook = application.Workbooks.Open(Path.GetFullPath("Data/Input.xlsx")) + + Dim worksheet As IWorksheet = workbook.Worksheets(0) + + ' Convert the specified Excel range to an image + Dim image As Image = worksheet.ConvertToImage(1, 1, 20, 7) + + ' Save the converted image in JPEG format + image.Save(Path.GetFullPath("Output/Sample.jpeg"), ImageFormat.Jpeg) + + workbook.Close() +End Using + +' Clear the registered fonts and dispose their associated streams +FontManager.ClearRegisteredFonts(True) +{% endhighlight %} +{% endtabs %} + +A complete working example of registering custom fonts in C# is present on this GitHub page. + ## See also * [Assemblies Information](https://help.syncfusion.com/document-processing/excel/excel-library/net/assemblies-required#converting-excel-worksheet-to-image) — manual assembly references. diff --git a/Document-Processing/Excel/Conversions/Excel-to-Image/overview.md b/Document-Processing/Excel/Conversions/Excel-to-Image/overview.md index aa40ece859..fbce060bb9 100644 --- a/Document-Processing/Excel/Conversions/Excel-to-Image/overview.md +++ b/Document-Processing/Excel/Conversions/Excel-to-Image/overview.md @@ -171,6 +171,156 @@ N> 3. Worksheet to image conversion is supported from .NET Framework 2.0 and .NE * Shrink to fit * Complex conditional formatting +## Register custom fonts + +The .NET Excel library allows TrueType (`.ttf`) and OpenType (`.otf`) font files to be registered as custom fonts. The registered fonts are used during Excel-to-Image conversion when the required fonts are not installed on the system. + +The following code example shows how to register custom fonts and use them during Excel-to-Image conversion. + +{% tabs %} +{% highlight c# tabtitle="C# [Cross-platform]" playgroundButtonLink="https://raw.githubusercontent.com/SyncfusionExamples/XlsIO-Examples/master/Worksheet%20to%20Image/Custom%20Font/.NET/Custom%20Font/Custom%20Font/Program.cs,180" %} +// Create a collection to store the custom font streams +List fontStreams = new List(); + +// Retrieve all font files from the specified directory +foreach (string file in Directory.GetFiles(Path.GetFullPath(@"Data/MyFonts"))) +{ + string extension = Path.GetExtension(file); + + // Load only TrueType and OpenType font files + if (extension.Equals(".ttf", StringComparison.OrdinalIgnoreCase) || + extension.Equals(".otf", StringComparison.OrdinalIgnoreCase)) + { + // Read the font file and copy its content to a memory stream + FileStream fileStream = new FileStream(file, FileMode.OpenOrCreate, FileAccess.Read); + Stream stream = new MemoryStream(); + fileStream.CopyTo(stream); + stream.Position = 0; + fontStreams.Add(stream); + } +} + +// Register the custom fonts for use during Excel-to-Image conversion +FontManager.RegisterFonts(fontStreams); + +using (ExcelEngine excelEngine = new ExcelEngine()) +{ + IApplication application = excelEngine.Excel; + application.DefaultVersion = ExcelVersion.Xlsx; + + IWorkbook workbook = application.Workbooks.Open(Path.GetFullPath(@"Data/Input.xlsx")); + IWorksheet sheet = workbook.Worksheets[0]; + + application.XlsIORenderer = new XlsIORenderer(); + + using (FileStream outputStream = new FileStream(Path.GetFullPath("Output/Image.png"), FileMode.Create, FileAccess.Write)) + { + sheet.ConvertToImage(sheet.UsedRange, outputStream); + } +} + +// Clear the registered fonts and dispose their associated streams +FontManager.ClearRegisteredFonts(true); +{% endhighlight %} + +{% highlight c# tabtitle="C# [Windows-specific]" %} +// Create a collection to store the custom font streams +List fontStreams = new List(); + +// Retrieve all font files from the specified directory +foreach (string file in Directory.GetFiles(Path.GetFullPath(@"Data/MyFonts"))) +{ + string extension = Path.GetExtension(file); + + // Load only TrueType and OpenType font files + if (extension.Equals(".ttf", StringComparison.OrdinalIgnoreCase) || + extension.Equals(".otf", StringComparison.OrdinalIgnoreCase)) + { + // Read the font file and copy its content to a memory stream + FileStream fileStream = new FileStream(file, FileMode.OpenOrCreate, FileAccess.Read); + Stream stream = new MemoryStream(); + fileStream.CopyTo(stream); + stream.Position = 0; + fontStreams.Add(stream); + } +} + +// Register the custom fonts for use during Excel-to-Image conversion +FontManager.RegisterFonts(fontStreams); + +using (ExcelEngine excelEngine = new ExcelEngine()) +{ + IApplication application = excelEngine.Excel; + application.DefaultVersion = ExcelVersion.Xlsx; + + IWorkbook workbook = application.Workbooks.Open(Path.GetFullPath("Data/Input.xlsx")); + + IWorksheet worksheet = workbook.Worksheets[0]; + + // Convert the specified Excel range to an image + Image image = worksheet.ConvertToImage(1, 1, 20, 7); + + image.Save(Path.GetFullPath("Output/Sample.jpeg"),ImageFormat.Jpeg); + + workbook.Close(); +} + +// Clear the registered fonts and dispose their associated streams +FontManager.ClearRegisteredFonts(true); +{% endhighlight %} + +{% highlight vb.net tabtitle="VB.NET [Windows-specific]" %} +' Create a collection to store the custom font streams +Dim fontStreams As New List(Of Stream)() + +' Retrieve all font files from the specified directory +For Each file As String In Directory.GetFiles(Path.GetFullPath("Data/MyFonts")) + + Dim extension As String = Path.GetExtension(file) + + ' Load only TrueType and OpenType font files. + If extension.Equals(".ttf", StringComparison.OrdinalIgnoreCase) OrElse +extension.Equals(".otf", StringComparison.OrdinalIgnoreCase) Then + + ' Read the font file and copy its content to a memory stream + Dim fileStream As New FileStream(file, FileMode.OpenOrCreate, FileAccess.Read) + + Dim stream As Stream = New MemoryStream() + fileStream.CopyTo(stream) + stream.Position = 0 + fontStreams.Add(stream) + + fileStream.Dispose() + End If +Next + +' Register the custom fonts for use during Excel-to-image conversion +FontManager.RegisterFonts(fontStreams) + +Using excelEngine As New ExcelEngine() + Dim application As IApplication = excelEngine.Excel + application.DefaultVersion = ExcelVersion.Xlsx + + Dim workbook As IWorkbook = application.Workbooks.Open(Path.GetFullPath("Data/Input.xlsx")) + + Dim worksheet As IWorksheet = workbook.Worksheets(0) + + ' Convert the specified Excel range to an image + Dim image As Image = worksheet.ConvertToImage(1, 1, 20, 7) + + ' Save the converted image in JPEG format + image.Save(Path.GetFullPath("Output/Sample.jpeg"), ImageFormat.Jpeg) + + workbook.Close() +End Using + +' Clear the registered fonts and dispose their associated streams +FontManager.ClearRegisteredFonts(True) +{% endhighlight %} +{% endtabs %} + +A complete working example of registering custom fonts in C# is present on this GitHub page. + ## See also * [Worksheet to Image conversion in .NET](https://help.syncfusion.com/document-processing/excel/conversions/excel-to-image/net/worksheet-to-image-conversion) — runnable code samples for each platform. diff --git a/Document-Processing/Excel/Conversions/Excel-to-PDF/NET/Excel-to-PDF-Conversion.md b/Document-Processing/Excel/Conversions/Excel-to-PDF/NET/Excel-to-PDF-Conversion.md index 672f281b1d..66c353d6f3 100644 --- a/Document-Processing/Excel/Conversions/Excel-to-PDF/NET/Excel-to-PDF-Conversion.md +++ b/Document-Processing/Excel/Conversions/Excel-to-PDF/NET/Excel-to-PDF-Conversion.md @@ -1646,6 +1646,151 @@ Malgun Gothic, Batang +## Register custom fonts + +The .NET Excel library allows TrueType (`.ttf`) and OpenType (`.otf`) font files to be registered as custom fonts. The registered fonts are used during Excel-to-PDF conversion when the required fonts are not installed on the system. + +The following code example shows how to register custom fonts and use them during Excel-to-PDF conversion. + +{% tabs %} +{% highlight c# tabtitle="C# [Cross-platform]" playgroundButtonLink="https://raw.githubusercontent.com/SyncfusionExamples/XlsIO-Examples/master/Excel%20to%20PDF/Custom%20Font/.NET/Custom%20Font/Custom%20Font/Program.cs,180" %} +// Create a collection to store the custom font streams +List fontStreams = new List(); + +// Retrieve all font files from the specified directory +foreach (string file in Directory.GetFiles(Path.GetFullPath(@"Data/MyFonts"))) +{ + string extension = Path.GetExtension(file); + + // Load only TrueType and OpenType font files + if (extension.Equals(".ttf", StringComparison.OrdinalIgnoreCase) || + extension.Equals(".otf", StringComparison.OrdinalIgnoreCase)) + { + // Read the font file and copy its content to a memory stream + FileStream fileStream = new FileStream(file, FileMode.OpenOrCreate, FileAccess.Read); + Stream stream = new MemoryStream(); + fileStream.CopyTo(stream); + stream.Position = 0; + fontStreams.Add(stream); + } +} + +// Register the custom fonts for use during Excel-to-PDF conversion +FontManager.RegisterFonts(fontStreams); + +using (ExcelEngine excelEngine = new ExcelEngine()) +{ + IApplication application = excelEngine.Excel; + application.DefaultVersion = ExcelVersion.Xlsx; + + IWorkbook workbook = application.Workbooks.Open(Path.GetFullPath(@"Data/Input.xlsx")); + + XlsIORenderer renderer = new XlsIORenderer(); + + PdfDocument pdfDocument = renderer.ConvertToPDF(workbook); + + pdfDocument.Save(Path.GetFullPath("Output/WorkbookToPDF.pdf")); + + pdfDocument.Close(); + workbook.Close(); +} + +// Clear the registered fonts and dispose their associated streams +FontManager.ClearRegisteredFonts(true); +{% endhighlight %} + +{% highlight c# tabtitle="C# [Windows-specific]" %} +// Create a collection to store the custom font streams +List fontStreams = new List(); + +// Retrieve all font files from the specified directory +foreach (string file in Directory.GetFiles(Path.GetFullPath(@"Data/MyFonts"))) +{ + string extension = Path.GetExtension(file); + + // Load only TrueType and OpenType font files + if (extension.Equals(".ttf", StringComparison.OrdinalIgnoreCase) || + extension.Equals(".otf", StringComparison.OrdinalIgnoreCase)) + { + // Read the font file and copy its content to a memory stream + FileStream fileStream = new FileStream(file, FileMode.OpenOrCreate, FileAccess.Read); + Stream stream = new MemoryStream(); + fileStream.CopyTo(stream); + stream.Position = 0; + fontStreams.Add(stream); + } +} + +// Register the custom fonts for use during Excel-to-PDF conversion +FontManager.RegisterFonts(fontStreams); +using (ExcelEngine excelEngine = new ExcelEngine()) +{ + Telemetry.Disable(); + IApplication application = excelEngine.Excel; + application.DefaultVersion = ExcelVersion.Xlsx; + IWorkbook workbook = application.Workbooks.Open(Path.GetFullPath(@"Data/Input.xlsx")); + + ExcelToPdfConverter converter = new ExcelToPdfConverter(workbook); + PdfDocument pdfDocument = converter.Convert(); + + pdfDocument.Save(Path.GetFullPath("Output/WorkbookToPDF.pdf")); + + pdfDocument.Close(); + workbook.Close(); +} +FontManager.ClearRegisteredFonts(true); +{% endhighlight %} + +{% highlight vb.net tabtitle="VB.NET [Windows-specific]" %} +' Create a collection to store the custom font streams +Dim fontStreams As New List(Of Stream)() + +' Retrieve all font files from the specified directory +For Each file As String In Directory.GetFiles(Path.GetFullPath("Data/MyFonts")) + + Dim extension As String = Path.GetExtension(file) + + ' Load only TrueType and OpenType font files + If extension.Equals(".ttf", StringComparison.OrdinalIgnoreCase) OrElse +extension.Equals(".otf", StringComparison.OrdinalIgnoreCase) Then + + ' Read the font file and copy its content to a memory stream + Using fileStream As New FileStream(file, FileMode.Open, FileAccess.Read) + + Dim stream As Stream = New MemoryStream() + fileStream.CopyTo(stream) + stream.Position = 0 + fontStreams.Add(stream) + End Using + End If +Next + +' Register the custom fonts for use during Excel-to-PDF conversion +FontManager.RegisterFonts(fontStreams) + +Using excelEngine As New ExcelEngine() + + Dim application As IApplication = excelEngine.Excel + application.DefaultVersion = ExcelVersion.Xlsx + + Dim workbook As IWorkbook = application.Workbooks.Open(Path.GetFullPath("Data/Input.xlsx")) + + Dim converter As New ExcelToPdfConverter(workbook) + Dim pdfDocument As PdfDocument = converter.Convert() + + pdfDocument.Save(Path.GetFullPath("Output/WorkbookToPDF.pdf")) + + pdfDocument.Close() + workbook.Close() +End Using + +' Clear the registered fonts and dispose their associated streams +FontManager.ClearRegisteredFonts(True) +{% endhighlight %} +{% endtabs %} + +A complete working example of registering custom fonts in C# is present on this GitHub page. + ## Supported elements This feature supports the following elements: @@ -1692,7 +1837,6 @@ The following list contains unsupported elements that presently not preserved in * Sparklines * Pivot charts * SmartArt graphics -* Different first page headers * Different odd and even pages * Tables * Custom styles