How to Convert XAML to PDF in MAUI

by Chaknith Bin

.NET MAUI (Multi-platform App UI) is a cross-platform framework for building native device applications. It extends from Xamarin.Forms and is part of the unified .NET 6 ecosystem. It enables .NET application developers to create applications for desktop, web, and mobile platforms using common UI components and a single codebase. MAUI also allows you to add platform-specific code and resources when necessary.

IronPdf allows you to generate PDF documents from MAUI pages, making the creation of PDF files/pages possible in these applications. However, IronPdf currently does not support mobile platforms.

IronPdf Extension Package

The IronPdf.Extensions.Maui package is the extension of the IronPdf main package. Since it is an extension, the IronPdf main package is still needed to render the content page of a MAUI application to a PDF document.

PM > Install-Package IronPdf.Extensions.Maui
C# NuGet Library for PDF

Install with NuGet

Install-Package IronPdf.Extensions.Maui

Render MAUI Page to a PDF

Edit MainPage.xaml.cs File

  • Go from the MainPage.xaml file to its code file, MainPage.xaml.cs.
  • Change the function named OnCounterClicked to PrintToPdf. Use the code sample below.

To turn your MAUI page to a PDF, use the RenderContentPageToPdf method. The method can be accessed by instantiating the ChromePdfRenderer class. This method will give you a PdfDocument object, which you can save or view using the SaveAs method or a PDF viewer with Viewing PDFs in MAUI.

Please note
The RenderContentPageToPdf method does not support data binding yet.

:path=/static-assets/pdf/content-code-examples/how-to/xaml-to-pdf-maui-mainpage-xaml-cs.cs
using IronPdf.Extensions.Maui;

namespace mauiSample;

public partial class MainPage : ContentPage
{
    public MainPage()
    {
        InitializeComponent();
    }

    private void PrintToPdf(object sender, EventArgs e)
    {
        ChromePdfRenderer renderer = new ChromePdfRenderer();

        // Apply HTML header
        renderer.RenderingOptions.HtmlHeader = new HtmlHeaderFooter()
        {
            HtmlFragment = "<h1>Header</h1>",
        };

        // Render PDF from Maui Page
        PdfDocument pdf = renderer.RenderContentPageToPdf<MainPage, App>().Result;

        pdf.SaveAs(@"C:\Users\lyty1\Downloads\contentPageToPdf.pdf");
    }
}
Imports IronPdf.Extensions.Maui

Namespace mauiSample

	Partial Public Class MainPage
		Inherits ContentPage

		Public Sub New()
			InitializeComponent()
		End Sub

		Private Sub PrintToPdf(ByVal sender As Object, ByVal e As EventArgs)
			Dim renderer As New ChromePdfRenderer()

			' Apply HTML header
			renderer.RenderingOptions.HtmlHeader = New HtmlHeaderFooter() With {.HtmlFragment = "<h1>Header</h1>"}

			' Render PDF from Maui Page
			Dim pdf As PdfDocument = renderer.RenderContentPageToPdf(Of MainPage, App)().Result

			pdf.SaveAs("C:\Users\lyty1\Downloads\contentPageToPdf.pdf")
		End Sub
	End Class
End Namespace
VB   C#

Furthermore, as you may have noticed, rendering from XAML also gives you full access to all the features available in RenderingOptions. This includes adding text and HTML headers and footers. You can also stamp images, add page numbers, and even customize the size and layout of the page. All these options are available when you use this method to create a PDF.

Edit MainPage.xaml File

In the MainPage.xaml file, replace the default OnCounterClicked function with the new PrintToPdf function. Clicking this button will run the PrintToPdf method and create the PDF.

<Button
x:Name="PrintToPdfBtn"
Text="Print to pdf"
SemanticProperties.Hint="Click to print page as PDF"
Clicked="PrintToPdf"
HorizontalOptions="Center" />
<Button
x:Name="PrintToPdfBtn"
Text="Print to pdf"
SemanticProperties.Hint="Click to print page as PDF"
Clicked="PrintToPdf"
HorizontalOptions="Center" />
'INSTANT VB TODO TASK: The following line uses invalid syntax:
'<Button x:Name="PrintToPdfBtn" Text="Print to pdf" SemanticProperties.Hint="Click to print page as PDF" Clicked="PrintToPdf" HorizontalOptions="Center" />
VB   C#

Output PDF

Before you save your PDF file, you can make more changes to it using the methods available to PdfDocument. You can merge pages, split them apart, or rotate them. You can also add annotations and bookmarks to your PDF.

Download .NET MAUI App Project

You can download the complete code for this guide. It comes as a zipped file that you can open in Visual Studio as a .NET MAUI App project.

Download the Full MAUI Sample Project

Chaknith related to Download .NET MAUI App Project

Chaknith Bin

Software Engineer

Chaknith is the Sherlock Holmes of developers. It first occurred to him he might have a future in software engineering, when he was doing code challenges for fun. His focus is on IronXL and IronBarcode, but he takes pride in helping customers with every product. Chaknith leverages his knowledge from talking directly with customers, to help further improve the products themselves. His anecdotal feedback goes beyond Jira tickets and supports product development, documentation and marketing, to improve customer’s overall experience.When he isn’t in the office, he can be found learning about machine learning, coding and hiking.