Using SkiaImageFormatProvider
Since R3 2022 the RadPdfProcessing library supports converting entire documents to images. This is achieved by using the third-party library SkiaSharp. You can convert to various formats, using synchronous or asynchronous export.
This feature is only available in the NET Standard version of the suite. For other versions check the following articles:
Requirements
To enable the image exporting functionality in your application, you must add references to the following packages:
- The Telerik.Documents.Fixed.FormatProviders.Image.Skia assembly.
- The SkiaSharp Nuget package.
- The SkiaSharp.NativeAssets.* Nuget package. This package may differ according to the used platform. There are versions 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 your documents' pages to images, use the Export method. Note that the export method does not accept a document but a page. This is why you need to iterate all pages. In this example, each page is saved in a separate file.
Example 1: Export RadFixedDocument to Image
PdfFormatProvider pdfFormatProvider = new PdfFormatProvider();
RadFixedDocument fixedDocument = pdfFormatProvider.Import(File.ReadAllBytes("Sample.pdf"));
SkiaImageFormatProvider imageProvider = new SkiaImageFormatProvider();
int count = 1;
foreach (RadFixedPage page in fixedDocument.Pages)
{
//byte[] resultImage = imageProvider.Export(page); //This method is obsolete since Q4 2024.
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"));
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); //This method is obsolete since Q4 2024.
byte[]? result = await imageProvider.ExportAsync(page, TimeSpan.FromSeconds(10));
File.WriteAllBytes(@"C:\my_temp\Page" + currentCount + ".png", result);
});
}
Export Settings
The SkiaImageFormatProvider exposes the following settings:
- ImageFormat: Allows you to set the image format, the supported values are JPEG, PNG, and Webp.
- ScaleFactor: Allows you to set the scale factor. The minimum value is 0.1.
- Quality: The value range is 1 (lowest quality) to 100 (highest quality) inclusive. The default value is 75.
- IsAntialiased: Gets or sets a value indicating whether the image will be antialiased. The default value is true.
Example 3: Set the Settings
PdfFormatProvider pdfFormatProvider = new PdfFormatProvider();
RadFixedDocument fixedDocument = pdfFormatProvider.Import(File.ReadAllBytes("Sample.pdf"));
SkiaImageFormatProvider imageProvider = new SkiaImageFormatProvider();
imageProvider.ExportSettings.ImageFormat = SkiaImageFormat.Png;
imageProvider.ExportSettings.ScaleFactor = 0.5;
imageProvider.ExportSettings.Quality = 50;
imageProvider.ExportSettings.IsAntialiased = false;
int count = 1;
foreach (RadFixedPage page in fixedDocument.Pages)
{
//byte[] resultImage = imageProvider.Export(page); //This method is obsolete since Q4 2024.
byte[] resultImage = imageProvider.Export(page, TimeSpan.FromSeconds(10));
File.WriteAllBytes(@"C:\Temp\Page " + count++ + ".png", resultImage);
}