Best common practice to export PDF page to image

1 Answer 31 Views
PdfViewer and PdfViewerNavigator
Mihajlo
Top achievements
Rank 1
Iron
Mihajlo asked on 07 Aug 2024, 05:23 PM

How to convert PDF pages to images in .NET Framework app? I've scoured the site, and it seems you recommend something like this:


RadPdfViewer pdfViewer = new RadPdfViewer();

pdfViewer.DocumentLoaded += (sender, e) =>
{
	if (sender is RadPdfViewerElement pdfViewerElement)
	{
		for (int i = 1; i <= pdfViewerElement.Document.Pages.Count; i++)
		{
			Image pageImage = pdfViewerElement.ExportPage(i, 1, true, ImageFormat.Jpeg);

			// Use pageImage
		}
	}
};

pdfViewer.LoadDocument(pdfPath);
pdfViewer.LoadElementTree();
Application.DoEvents();

Is this really the best way for .NET Framework apps? There is an example that uses PdfProcessing instead of PdfViewer, but requires the assembly Telerik.Documents.Fixed.FormatProviders.Image.Skia, which I don't have available in my Visual Studio with latest update of Telerik UI for WinForms 2024.3.806.462. Nor is it available on nuget, or I don't know how to find it.

Mihajlo
Top achievements
Rank 1
Iron
commented on 07 Aug 2024, 05:50 PM

Also, how in the example above to get to RadPdfViewer root object starting from pdfViewerElement object?

1 Answer, 1 is accepted

Sort by
0
Accepted
Dinko | Tech Support Engineer
Telerik team
answered on 08 Aug 2024, 01:26 PM

Hi Mihajlo,

You are in the right direction with the code snippet. Exporting an image is possible through the ExporPage() method of the RadPdfViewerElement. The same approach is demonstrated in the How to Export Each Page as an Image in PDF Documents KB article for our RadPdfViewer control for WinForms. 

As for the approach which includes using Skia, you will need to add the SkiaSharp and SkiaSharp.NativeAssets.* NuGet package. These packages are third party and they are not located in our NuGet server. After adding these packages, you will need the Telerik.Documents.Fixed.FormatProviders.Image.Skia assembly.

The Telerik.Documents.Fixed.FormatProviders.Image.Skia is a NuGet not an assembly. It is wrongly written in the documentation. Accept my apologies for that.

However, keep in mind that this functionality is only available in the NET Standard version of the suite. If you want to use it, you will need to avoid adding any other NuGet which includes libraries from our document processing suite. Otherwise, the project won't be buildable.  

In general, in WinForms, I would suggest using the approached mentioned in the KB article mentioned above.

Regarding your second question, you can use the following approach to access the control from its element:

private void RadPdfViewer1_DocumentLoaded(object sender, EventArgs e)
{
    if (sender is RadPdfViewerElement pdfViewerElement)
    {
        var pdfViewer = pdfViewerElement.ElementTree.Control as RadPdfViewer;
        . . . . . .
    }
}

Regards,
Dinko | Tech Support Engineer
Progress Telerik

Love the Telerik and Kendo UI products and believe more people should try them? Invite a fellow developer to become a Progress customer and each of you can get a $50 Amazon gift voucher.

Tags
PdfViewer and PdfViewerNavigator
Asked by
Mihajlo
Top achievements
Rank 1
Iron
Answers by
Dinko | Tech Support Engineer
Telerik team
Share this question
or