Telerik blogs

Check out the new functionality of Telerik PdfProcessing to convert PDF pages to images.

Many of you requested us to provide you with the ability to export your documents or parts of them to images in .NET Standard/.NET Core applications. As we are always listening to your feedback, during the last release we worked hard to provide you with this functionality and I will present it in this blog post.

To make it possible for all platforms using the .NET Standard version of PdfProcessing to create images, the library uses a third-party tool for image generation—SkiaSharp. Multiple image formats are supported, and you can also choose whether to perform the exporting operation synchronously or asynchronously.

Let’s take a closer look at the API related to the new functionality. The entry point making all the magic is the SkiaImageFormatProvider class. If you are already using PdfProcessing, you should be familiar with the format providers and how they work. For all the rest, in short, the format providers expose Import and Export methods that enable you to parse a document into the internal model of the library or save the information from this model into a document file, respectively.

Let’s Create an Example

To better demonstrate the functionality and its usage, I will guide you step by step on creating an application that opens an existing PDF document and converts its pages into images.

First things first—I will create a console application and add the dependencies we need to use the PdfProcessing library. For more convenience, I will directly go with the Telerik.Documents.Fixed.FormatProviders.Image.Skia NuGet package that will download all of its dependencies automatically:

Telerik.Documents.Fixed.FormatProviders.Image.Skia NuGet package in Visual Studio NuGet Manager

In case you prefer adding references to the .dll files, here is the list of required assemblies:

  • Telerik.Documents.Core
  • Telerik.Documents.Fixed
  • Telerik.Zip
  • Telerik.Documents.Fixed.FormatProviders.Image.Skia

As I mentioned earlier, the image exporting functionality depends on SkiaSharp, thus you will need to add dependencies to its binaries as well:

  • 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.

Once we are ready with the setup, we can continue to add some logic to our application. The first step we need to implement is to read the PDF document we already have:

byte[] fileBytes = File.ReadAllBytes("SampleDocument.pdf");
Telerik.Windows.Documents.Fixed.FormatProviders.Pdf.PdfFormatProvider pdfFormatProvider = new Telerik.Windows.Documents.Fixed.FormatProviders.Pdf.PdfFormatProvider();
Telerik.Windows.Documents.Fixed.Model.RadFixedDocument fixedDocument = pdfFormatProvider.Import(fileBytes);

After having the document imported, you can now convert its pages to images through the Export method. In this example, each page is saved in a separate file.

SkiaImageFormatProvider imageProvider = new SkiaImageFormatProvider();
foreach (RadFixedPage page in fixedDocument.Pages)
    byte[] resultImage = imageProvider.Export(page);
    int pageNumber = fixedDocument.Pages.IndexOf(page) + 1;
    File.WriteAllBytes(@"C:\Temp\Images " + pageNumber + ".png", resultImage);

That is all you need to do if you need to read an existing file and export its pages to images. Check out the SkiaImageFormatProvider documentation article if you are curious about other export settings. I hope you will enjoy the new functionality!

Try Out the New Feature & Share Your Feedback

Head over and download a free trial. If you are an active license holder, you can grab the latest bits from Your Telerik Account or update your NuGet package references to the latest ones directly in your .NET solutions.

Share your thoughts with us on our Feedback Portal and help us shape the future of Telerik Document Processing!

About the Author

Tanya Dimitrova

Tanya Dimitrova is a Tech Support Engineer in the Telerik XAML Team. In her work her main responsibility is to assist clients to implement different scenarios using the document processing libraries and editors. She is passionate in finding new adventures and in her free time enjoys travelling, reading, swimming, dancing or just spending time with friends.

Related Posts


Comments are disabled in preview mode.