Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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<Stream> fontStreams = new List<Stream>();

// 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<Stream> fontStreams = new List<Stream>();

// 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 <a href="https://github.com/SyncfusionExamples/XlsIO-Examples/tree/master/Worksheet%20to%20Image/Custom%20Font/.NET/Custom%20Font" aria-label="GitHub example for registering custom fonts during worksheet-to-image conversion">this GitHub page</a>.

## See also

* [Assemblies Information](https://help.syncfusion.com/document-processing/excel/excel-library/net/assemblies-required#converting-excel-worksheet-to-image) — manual assembly references.
Expand Down
150 changes: 150 additions & 0 deletions Document-Processing/Excel/Conversions/Excel-to-Image/overview.md
Original file line number Diff line number Diff line change
Expand Up @@ -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<Stream> fontStreams = new List<Stream>();

// 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<Stream> fontStreams = new List<Stream>();

// 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 <a href="https://github.com/SyncfusionExamples/XlsIO-Examples/tree/master/Worksheet%20to%20Image/Custom%20Font/.NET/Custom%20Font" aria-label="GitHub example for registering custom fonts during worksheet-to-image conversion">this GitHub page</a>.

## 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.
Expand Down
Loading