How to Convert XAML to PDF in MAUI
.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.
How to Convert XAML to PDF in MAUI
- Download the IronPdf C# library for MAUI
- Modify the MainPage.xaml.cs file to use the
RenderContentPageToPdf
method - Update the button in the MainPage.xaml file to trigger the new function
- Export the PDF document or view it in the MAUI app using a PDF viewer
- Download the sample MAUI project for a quick start
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
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
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
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" />
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.