DEV Community

Cover image for PDF to Image Conversion Made Easy in .NET MAUI
Zahra Sandra Nasaka for Syncfusion, Inc.

Posted on • Originally published at syncfusion.com on

PDF to Image Conversion Made Easy in .NET MAUI

TL;DR: Quickly implement PDF to Image Conversion in .NET MAUI to render PDF pages as high-quality PNG or JPEG images across mobile and desktop platforms. This guide covers multi-page conversion, password-protected files, custom sizing, and performance optimization using asynchronous processing, all with practical C# examples.

Converting PDF documents into images is a common requirement for modern apps, whether for generating thumbnails, sharing pages, or displaying content in a UI. With .NET MAUI and Syncfusion’s PdfToImageConverter, you can implement this feature quickly and efficiently.

In this guide, you’ll learn:

  • How to convert single and multi-page PDFs to images
  • How to apply custom scaling and sizes
  • How to handle password-protected PDFs
  • How to optimize performance with asynchronous processing

Prerequisites

Before you begin, make sure the latest .NET SDK is installed and your environment is properly set up for .NET MAUI development.

Note: For detailed prerequisites and setup instructions, follow the official .NET MAUI Getting Started Guide to ensure your development environment is properly configured.

Step 1: Create a .NET MAUI app

Begin by creating a new .NET MAUI app by running the commands below via CLI.

dotnet new maui -n PdfToImageConversionSample
cd PdfToImageConversionSample
Enter fullscreen mode Exit fullscreen mode

This creates a new .NET MAUI project with the necessary structure for cross-platform development.

Step 2: Installing Syncfusion PDF-to-image converter

Once the project is created, install the Syncfusion.Maui.PdfToImageConverter package, using the following command via CLI.

dotnet add package Syncfusion.Maui.PdfToImageConverter
Enter fullscreen mode Exit fullscreen mode

Add the namespace to your C# files:

using Syncfusion.Maui.PdfToImageConverter;
Enter fullscreen mode Exit fullscreen mode

This package provides optimized PDF processing for mobile and desktop platforms.

Step 3: Convert a single PDF page to an image

Start by converting a single PDF page to an image (ideal for thumbnails or previews), as shown in the code snippet below:

//Get the PDF file stream to convert it as image.
Stream? pdfFile = typeof(MainPage).GetTypeInfo().Assembly
    .GetManifestResourceStream("PdfToImageConversionSample.Resources.Pdf.input.pdf");

//Initialize the PDF to Image converter
PdfToImageConverter converter = new PdfToImageConverter();

//Load the document
converter.Load(pdfFile!);

//Convert the PDF page as image (0 = first page)
Stream? imageStream = converter.Convert(0);
Enter fullscreen mode Exit fullscreen mode

Step 4: Displaying images in your UI

Once you have converted the PDF page to an image, the next step is to display it in your app’s UI.

1. Define the layout container

First, create a ScrollView and drop a VerticalStackLayout inside it. This layout will act as the container for all your images:

<ScrollView>
    <VerticalStackLayout x:Name="ImageContainer" Spacing="10">
    </VerticalStackLayout>
</ScrollView>

Enter fullscreen mode Exit fullscreen mode

2. Add the image dynamically via stream

Once you have an image stream ready, you can inject the image into the layout using the following code:

this.ImageContainer.Clear(); // Clear previous content if needed

// Add the image to the layout using the stream
ImageContainer.Children.Add(new Image
{
    Source = ImageSource.FromStream(() => imageStream)
});

Enter fullscreen mode Exit fullscreen mode

By executing the above code, you will generate the image displaying the first page of your PDF document.

Converting a PDF page into an image


Converting a PDF page into an image

Step 5: Convert multiple PDF pages to images

Next, let’s extend the functionality to convert every page of a PDF into individual images, a must-have for document viewers or scenarios where full document processing is required.

//Initialize the PDF to Image converter
PdfToImageConverter converter = new PdfToImageConverter();

//Load the document
converter.Load(pdfFile!);

Stream[]? images = converter.Convert();

if (images != null)
{
    this.ImageContainer.Clear(); // Clear previous content if needed

    foreach (Stream imageStream in images)
    {
        ImageContainer.Children.Add(new Image
        {
            Source = ImageSource.FromStream(() => imageStream)
        });
    }
}
//Dispose the converter to free up resources
converter.Dispose();
Enter fullscreen mode Exit fullscreen mode

By executing the above code, you will generate multiple images, each representing a page of the PDF document.

Conversion of multiple pages into images


Conversion of multiple pages into images

Convert pages with custom scaling or size

Want more control over how your images look? You can tweak the quality and dimensions by using scaling factors or setting custom sizes. This is super handy when you need images for different scenarios, like thumbnails or high-resolution previews.

1. Custom scaling

Think of scaling as zooming in or out. A higher scaling factor gives you bigger, sharper images. A lower factor makes smaller files perfect for quick previews or thumbnails.

Here’s an example that creates an image with a scaling value of 0.2:

//Initialize the PDF to Image converter
PdfToImageConverter converter = new PdfToImageConverter();

//Load the document
converter.Load(pdfFile!);

Stream[]? images = converter.Convert(0.2f);

//Dispose the converter
converter.Dispose();
Enter fullscreen mode Exit fullscreen mode

After running this code, the generated images will reflect the specified scaling factor, adjusting quality and resolution.

Images generated with custom scaling values


Images generated with custom scaling values

2. Custom size

Need your images to fit a specific size? No problem! You can set exact dimensions so your images look just right for your layout.

Here’s how you can generate images with a fixed size of 1200×1800 pixels:

//Initialize the PDF to Image converter
PdfToImageConverter converter = new PdfToImageConverter();

//Load the document
converter.Load(pdfFile!);

Stream[]? images = converter.Convert(new SizeF(1200, 1800));
Enter fullscreen mode Exit fullscreen mode

By executing the above code, you will generate images with the exact dimensions specified (1200×1800 pixels).

Images generated with custom dimensions


Images generated with custom dimensions

Asynchronous image conversion for responsive apps

Large PDFs can slow down your app if processed synchronously. To keep things responsive, use the ConvertAsync method from Syncfusion’s .NET MAUI PDF-to-image converter. It even supports CancellationToken for better control over long-running tasks.

//Initialize the PDF to Image converter
using (PdfToImageConverter converter = new PdfToImageConverter())
{
    //Load the document
    converter.Load(pdfFile!);

    //Convert the PDF pages as images asynchronously.
    Stream[]? images = await converter.ConvertAsync();
}
Enter fullscreen mode Exit fullscreen mode

This way, your app stays smooth while the conversion happens in the background.

Handling password-protected PDFs

Some PDFs are secured with passwords. The converter supports these files, but on Android, direct password loading isn’t possible due to platform restrictions. The solution is to use the Syncfusion.Pdf.NET package to remove the password before conversion.

//Initialize the PDF to Image converter
using (PdfToImageConverter converter = new PdfToImageConverter())
{
#if ANDROID
    //Load the document with a password using Syncfusion.Pdf.NET
    PdfLoadedDocument document = new PdfLoadedDocument(pdfFile, "syncfusion");

    //Remove the password protection
    document.Security.UserPassword = string.Empty;
    document.Security.OwnerPassword = string.Empty;

    MemoryStream stream = new MemoryStream();
    document.Save(stream);
    stream.Position = 0;
    document.Close(true);

    converter.Load(stream);
#else
    //Load the document with a password directly
    converter.Load(pdfFile!, "syncfusion");
#endif

    //Convert the PDF pages as images asynchronously.
    Stream[]? images = await converter.ConvertAsync();

    ImageContainer.Children.Clear();

    if (images != null)
    {
        this.ImageContainer.Clear(); // Clear previous content if needed

        foreach (Stream imageStream in images)
        {
            ImageContainer.Children.Add(new Image
            {
                Source = ImageSource.FromStream(() => imageStream)
            });

            // Add a light gray divider line
            ImageContainer.Children.Add(new BoxView
            {
                HeightRequest = 1.5,
                BackgroundColor = Colors.LightGray,
                HorizontalOptions = LayoutOptions.Fill
            });
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

After running the above code, password-protected PDFs will be successfully converted to images across all supported platforms.

Conversion of password-protected PDF pages into images


Conversion of password-protected PDF pages into images

Real-world use cases

The PDF to image conversion functionality opens up numerous possibilities for your applications:

  • PDF thumbnails for file browsers
  • Sharing pages as images
  • Previews for email attachments
  • Processing reports into image workflows
  • Annotation tools using image representations
  • Document galleries
  • Print preview functionality

GitHub reference

For complete implementation details and a working sample, refer to the GitHub demo.

Conclusion

Converting PDF documents to images in .NET MAUI is easy and adds powerful features to your apps. This guide covered key techniques, single and multi-page conversion, custom sizes, and handling password-protected files, to help you implement efficient PDF-to-image functionality.

By adding these capabilities to your .NET MAUI projects, you can create dynamic, user-friendly mobile and desktop apps. Combining .NET MAUI’s cross-platform flexibility with Syncfusion’s robust PDF processing tools gives you a strong foundation for document-centric applications.

Explore Syncfusion’s .NET PDF Library today! You can examine our online examples and documentation for more features and functionalities.

If you’re a Syncfusion user, you can download the setup from the license and downloads page. Otherwise, you can download a free 30-day trial.

You can also contact us through our support forum, feedback portal, or support portal for queries. We are always happy to assist you!

Related Blogs

This article was originally published at Syncfusion.com.

Top comments (0)