Convert custom PDF page to SkiaSharp.SKBitmap in C# and .NET


In today's world, working with PDF documents and graphic images has become an integral part of many applications, particularly in the areas of automation, visualization, and document processing. One common task is obtaining an image of a specific PDF page for further processing or display. In this article, we'll look at how to convert a custom PDF page into an SKBitmap object from the SkiaSharp library in C# and .NET, using the powerful PDF Focus .NET component of the SautinSoft SDK. This approach allows you to create efficient and flexible solutions that easily integrate into your projects.

Converting a PDF page to an SKBitmap is the process of extracting an image of a specific PDF document page and converting it into a graphic object that can be easily used in graphical interfaces, such as displaying it in UI elements or processing it programmatically. Using this conversion method eliminates the need to manually write complex PDF rendering code, enabling quick and reliable implementation.

SKBitmap is a class from the SkiaSharp library that provides the ability to work with, draw, process, and save images of various formats.

Conversion allows you to:

  • Display the contents of a specific PDF page in UI controls.
  • Perform subsequent image processing (e.g., adding annotations and filters).
  • Create thumbnails or preview images.

This method is indispensable for developers who need to:

  • Quickly and reliably obtain images of specific PDF pages without the complexity of manually implementing rendering.
  • Integrate conversion into preview systems, image processing systems, and document automation platforms.
  • Create solutions for digital libraries, content management systems, and document processing applications.

The need to extract page images from PDFs is widespread in business applications, financial systems, and legal and administrative document management systems. This task is especially relevant in automated systems that require quick document thumbnails, display documents in a preview style, and integrate graphic display material. Therefore, such solutions are in demand in virtually any field related to PDF processing.

Complete code

using SkiaSharp;
using System;
using System.IO;

namespace Sample
{
    class Sample
    {
        static void Main(string[] args)
        {
            // Before starting, we recommend to get a free key:
            // https://sautinsoft.com/start-for-free/

            // Apply the key here:
            // SautinSoft.PdfFocus.SetLicense("...");

            //Convert custom PDF page to Image object
            SautinSoft.PdfFocus f = new SautinSoft.PdfFocus();


            string pdfPath = Path.GetFullPath(@"..\..\..\parkmap.pdf");
            string imagePath = "Result.jpg";

            f.OpenPdf(pdfPath);

            if (f.PageCount > 0)
            {
                //Let's convert 1st page into SKBitmap object, 300 dpi
                f.ImageOptions.Dpi = 300;
                f.ImageOptions.SelectedPages = new int[] { 0 };
                SKBitmap img = f.ToSKBitmap();

                //Save to file
                if (img != null)
                {
                    img.Encode(new FileStream(imagePath, FileMode.Create), SKEncodedImageFormat.Jpeg, 90);
                    System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo(imagePath) { UseShellExecute = true });
                }
            }
        }
    }
}

Download

Imports SkiaSharp
Imports System
Imports System.IO

Namespace Sample
	Friend Class Sample
		Shared Sub Main(ByVal args() As String)
			' Before starting, we recommend to get a free key:
			' https://sautinsoft.com/start-for-free/

			' Apply the key here:
			' SautinSoft.PdfFocus.SetLicense("...");

			'Convert custom PDF page to Image object
			Dim f As New SautinSoft.PdfFocus()


			Dim pdfPath As String = Path.GetFullPath("..\..\..\parkmap.pdf")
			Dim imagePath As String = "Result.jpg"

			f.OpenPdf(pdfPath)

			If f.PageCount > 0 Then
				'Let's convert 1st page into SKBitmap object, 300 dpi
				f.ImageOptions.Dpi = 300
				f.ImageOptions.SelectedPages = New Integer() { 0 }
				Dim img As SkiaSharp.SKBitmap = f.ToSKBitmap()

				'Save to file
				If img IsNot Nothing Then
					img.Encode(New FileStream(imagePath, FileMode.Create), SKEncodedImageFormat.Jpeg, 90)
					System.Diagnostics.Process.Start(New System.Diagnostics.ProcessStartInfo(imagePath) With {.UseShellExecute = True})
				End If
			End If
		End Sub
	End Class
End Namespace

Download


If you need a new code example or have a question: email us at support@sautinsoft.com or ask at Online Chat (right-bottom corner of this page) or use the Form below:


Captcha

Questions and suggestions from you are always welcome!

We are developing .Net components since 2002. We know PDF, DOCX, RTF, HTML, XLSX and Images formats. If you need any assistance with creating, modifying or converting documents in various formats, we can help you. We will write any code example for you absolutely free.