Using SkiaImageFormatProvider
| Minimum Version | R3 2022 |
|---|---|
| Target Framework | .NET Standard / .NET (Target OS: None) |
RadPdfProcessing supports converting entire documents to images through the third-party library SkiaSharp. You can convert to various formats with synchronous or asynchronous export.
This feature is available only in the .NET Standard version of the suite. For other versions, refer to the following articles:
Requirements
To enable the image export functionality in your application, add references to the following packages:
- The
Telerik.Documents.Fixed.FormatProviders.Image.SkiaNuGet package. - The
SkiaSharpNuGet package. - The
SkiaSharp.NativeAssets.*NuGet package. This package may differ depending on the target platform. Versions are available for Windows, macOS, Linux, WebAssembly, Android, iOS, and others.
A FontsProvider implementation is required to read the document fonts and draw the image.
Using the SkiaImageFormatProvider
To convert document pages to images, use the Export method. The Export method does not accept a document but a page. Iterate all pages and save each page in a separate file.
Example 1: Export RadFixedDocument to Image
PdfFormatProvider pdfFormatProvider = new PdfFormatProvider();
RadFixedDocument fixedDocument = pdfFormatProvider.Import(File.ReadAllBytes("Sample.pdf"), TimeSpan.FromSeconds(10));
SkiaImageFormatProvider imageProvider = new SkiaImageFormatProvider();
int count = 1;
foreach (RadFixedPage page in fixedDocument.Pages)
{
byte[] resultImage = imageProvider.Export(page, TimeSpan.FromSeconds(10));
File.WriteAllBytes(@"C:\Temp\Page " + count++ + ".png", resultImage);
}
Exporting Asynchronously
The ExportAsync method allows you to perform the conversion asynchronously.
Example 2: Export RadFixedDocument to Image Async
public async void ExportAsync()
{
PdfFormatProvider pdfFormatProvider = new PdfFormatProvider();
RadFixedDocument fixedDocument = pdfFormatProvider.Import(File.ReadAllBytes("Sample.pdf"), TimeSpan.FromSeconds(10));
SkiaImageFormatProvider imageProvider = new SkiaImageFormatProvider();
int count = 0;
await Parallel.ForEachAsync(fixedDocument.Pages, async (page, token) =>
{
int currentCount = Interlocked.Increment(ref count);
byte[]? result = await imageProvider.ExportAsync(page, TimeSpan.FromSeconds(10));
File.WriteAllBytes(@"C:\my_temp\Page" + currentCount + ".png", result);
});
}
Export Settings
The SkiaImageFormatProvider exposes the SkiaImageExportSettings, which allow you to control the export options.