如何在 .NET MAUI 中將 XAML 轉換為 PDF

How to Convert XAML to PDF in MAUI

This article was translated from English: Does it need improvement?
Translated
View the article in English

.NET MAUI(多平台應用程序 UI)是一個用於構建本機設備應用程序的跨平台框架。 它繼承自 Xamarin.Forms,並且是統一 .NET 6 生態系統的一部分。 它使 .NET 應用程序開發人員能夠使用通用的 UI 元件和單一代碼庫為桌面、網頁和移動平台創建應用程序。 MAUI 也允許您在必要時添加平台特定的代碼和資源。

IronPdf 允許您從 MAUI 頁面生成 PDF 文檔,讓這些應用程序中的 PDF 文件/頁面創建成為可能。 然而,IronPdf 目前不支持移動平台。

快速開始:在 .NET MAUI 中使用 IronPDF 將 XAML 轉換為 PDF

使用 IronPDF 在 .NET MAUI 中輕鬆將您的 XAML 頁面轉換為 PDF。 只需幾行代碼,您就可以將您的 MAUI 應用程序內容轉換為專業質量的 PDF 文檔。 本指南提供了一個簡單的示例,可快速開始,利用 IronPDF 的高效渲染能力。 請遵循以下步驟,無縫整合 PDF 生成到您的桌面和網頁應用程序中,輕鬆提升用戶體驗。

Nuget IconGet started making PDFs with NuGet now:

  1. Install IronPDF with NuGet Package Manager

    PM > Install-Package IronPdf

  2. Copy and run this code snippet.

    var pdf = new IronPdf.ChromePdfRenderer().RenderContentPageToPdf<MainPage,App>().SaveAs("page.pdf");
  3. Deploy to test on your live environment

    Start using IronPDF in your project today with a free trial
    arrow pointer
class="hsg-featured-snippet">

簡易工作流程(5 步驟)

  1. 下載用于 MAUI 的 IronPdf C# 庫
  2. 修改 MainPage.xaml.cs 文件以使用 RenderContentPageToPdf 方法
  3. 更新 MainPage.xaml 文件中的按鈕以觸發新功能
  4. 使用 PDF 查看器導出 PDF 文檔或在 MAUI 應用程序中查看
  5. 下載示例 MAUI 項目以便快速開始

IronPdf 擴展包

IronPdf.Extensions.Maui 包是 IronPdf 主包的擴展。 由於它是一個擴展,因此仍然需要 IronPdf 主包來將 MAUI 應用程序的內容頁渲染為 PDF 文檔。

Install-Package IronPdf.Extensions.Maui

class="products-download-section">
data-modal-id="trial-license-after-download">
class="product-image"> C# NuGet 庫用于 PDF
class="product-info">

使用 NuGet 安裝

data-original-title="點擊即可複製">
class="copy-nuget-row">
Install-Package IronPdf.Extensions.Maui
class="copy-button">
class="nuget-link">nuget.org/packages/IronPdf.Extensions.Maui/

將 MAUI 頁面渲染為 PDF

編輯 MainPage.xaml.cs 文件

  • 從 MainPage.xaml 文件切換到其代碼文件 MainPage.xaml.cs。
  • 將名為 OnCounterClicked 的函數更改為 PrintToPdf。 請使用以下代碼示例。

要將您的 MAUI 頁面轉換為 PDF,請使用 RenderContentPageToPdf 方法。 可以通過實例化 ChromePdfRenderer 類來訪問該方法。 此方法將為您提供一個 PdfDocument 對象,您可以使用 SaveAs 方法保存或使用 PDF 查看器查看,參見在 MAUI 中查看 PDF

請注意RenderContentPageToPdf 方法尚不支持數據綁定。

: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
$vbLabelText   $csharpLabel

此外,正如您可能已經注意到的,從 XAML 渲染還允許您完全訪問 RenderingOptions 中可用的所有功能。 這包括添加文本和 HTML 頁眉和頁腳。 You can also stamp images, add page numbers, and even customize the size and layout of the page. 使用此方法創建 PDF 時可以使用所有這些選項。

編輯 MainPage.xaml 文件

在 MainPage.xaml 文件中,將默認的 OnCounterClicked 函數替換為新的 PrintToPdf 函數。 點擊此按鈕將運行 PrintToPdf 方法並創建 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" />
XML

輸出 PDF

在保存您的 PDF 文件之前,您可以使用 PdfDocument 提供的方法進行更多更改。 您可以合併頁面,將其拆開,或旋轉它們。 You can also add annotations and bookmarks to your PDF.

下載 .NET MAUI 應用程序項目

您可以下載本指南的完整代碼。它作為一個壓縮文件提供,您可以在 Visual Studio 中以 .NET MAUI 應用程序項目的方式打開。

下載完整的 MAUI 示例項目

準備看看您還能做哪些其他事情嗎? 在這裡查看我們的教程頁面:轉換PDF

常見問題解答

如何在.NET MAUI中將XAML頁面轉換為PDF?

在 .NET MAUI 中,您可以使用 IronPdf 庫將 XAML 頁面轉換為 PDF。首先,下載適用於 MAUI 的 IronPdf C# 庫,並修改MainPage.xaml.cs文件,使其使用RenderContentPageToPdf方法。然後,將MainPage.xaml檔案中的預設函數替換為PrintToPdf函數,以便透過按一下按鈕建立 PDF 檔案。

將 IronPdf 與 .NET MAUI 結合使用有哪些限制?

目前,IronPdf 僅支援從桌面和 Web 平台上的 MAUI 應用程式產生 PDF 文件,不支援行動平台。這意味著該程式庫可以與運行在 Windows、macOS 和 Web 環境下的 MAUI 應用程式一起使用。

IronPdf.Extensions.Maui 包的用途是什麼?

IronPdf.Extensions.Maui 套件擴展了 IronPdf 主包的功能,可讓您將 MAUI 應用程式的內容頁面渲染為 PDF 文檔,並提供高級渲染選項,例如添加頁眉、頁腳和頁碼。

如何在MAUI應用程式中檢視或儲存產生的PDF檔案?

使用 IronPdf 中的ChromePdfRenderer類別產生 PDF 後,您可以使用SaveAsAsync方法檢視或儲存PdfDocument對象,並指定要儲存的檔案路徑。

我可以自訂從 MAUI 頁面產生的 PDF 的佈局嗎?

是的,您可以使用 IronPdf 的功能自訂產生的 PDF 的佈局,例如修改頁面大小、方向,以及新增自訂文字、HTML 頁首、頁尾和頁碼。

要將 MainPage.xaml 檔案轉換為 PDF,需要做哪些更改?

MainPage.xaml檔案中,您需要將預設的OnCounterClicked函數替換為PrintToPdf函數。此變更將使按鈕能夠觸發PrintToPdf方法,從而方便地建立 PDF 檔案。

是否有將 MAUI 頁面轉換為 PDF 的範例項目?

是的,我們提供完整的 MAUI 範例專案供您下載,其中包含以壓縮檔案格式提供的完整程式碼指南。此範例可以在 Visual Studio 中作為 .NET MAUI 應用程式專案開啟,幫助您在 MAUI 應用程式中實現 PDF 功能。

PdfDocument 類別提供了哪些功能?

IronPdf 的PdfDocument類別提供了合併頁面、分割頁面、旋轉頁面以及新增註解和書籤等功能,以增強 PDF 的互動性和實用性。

IronPdf 是否相容於 .NET 10?這對 MAUI 開發人員意味著什麼?

是的,IronPdf 完全相容於 .NET 10,包括 MAUI 專案。這意味著您可以在 .NET 10 MAUI 應用程式中使用 IronPdf,無需任何變通方法或使用已棄用的 API。它支援最新的 .NET 改進,而 .NET 10 現在已是長期支援 (LTS) 版本。

A PHP Error was encountered

Severity: Warning

Message: Illegal string offset 'name'

Filename: sections/author_component.php

Line Number: 18

Backtrace:

File: /var/www/ironpdf.com/application/views/main/sections/author_component.php
Line: 18
Function: _error_handler

File: /var/www/ironpdf.com/application/libraries/Render.php
Line: 63
Function: view

File: /var/www/ironpdf.com/application/views/products/sections/three_column_docs_page_structure.php
Line: 64
Function: main_view

File: /var/www/ironpdf.com/application/libraries/Render.php
Line: 88
Function: view

File: /var/www/ironpdf.com/application/views/products/how-to/index.php
Line: 2
Function: view

File: /var/www/ironpdf.com/application/libraries/Render.php
Line: 88
Function: view

File: /var/www/ironpdf.com/application/libraries/Render.php
Line: 552
Function: view

File: /var/www/ironpdf.com/application/controllers/Products/Howto.php
Line: 31
Function: render_products_view

File: /var/www/ironpdf.com/index.php
Line: 292
Function: require_once

A PHP Error was encountered

Severity: Warning

Message: Illegal string offset 'title'

Filename: sections/author_component.php

Line Number: 38

Backtrace:

File: /var/www/ironpdf.com/application/views/main/sections/author_component.php
Line: 38
Function: _error_handler

File: /var/www/ironpdf.com/application/libraries/Render.php
Line: 63
Function: view

File: /var/www/ironpdf.com/application/views/products/sections/three_column_docs_page_structure.php
Line: 64
Function: main_view

File: /var/www/ironpdf.com/application/libraries/Render.php
Line: 88
Function: view

File: /var/www/ironpdf.com/application/views/products/how-to/index.php
Line: 2
Function: view

File: /var/www/ironpdf.com/application/libraries/Render.php
Line: 88
Function: view

File: /var/www/ironpdf.com/application/libraries/Render.php
Line: 552
Function: view

File: /var/www/ironpdf.com/application/controllers/Products/Howto.php
Line: 31
Function: render_products_view

File: /var/www/ironpdf.com/index.php
Line: 292
Function: require_once

A PHP Error was encountered

Severity: Warning

Message: Illegal string offset 'comment'

Filename: sections/author_component.php

Line Number: 48

Backtrace:

File: /var/www/ironpdf.com/application/views/main/sections/author_component.php
Line: 48
Function: _error_handler

File: /var/www/ironpdf.com/application/libraries/Render.php
Line: 63
Function: view

File: /var/www/ironpdf.com/application/views/products/sections/three_column_docs_page_structure.php
Line: 64
Function: main_view

File: /var/www/ironpdf.com/application/libraries/Render.php
Line: 88
Function: view

File: /var/www/ironpdf.com/application/views/products/how-to/index.php
Line: 2
Function: view

File: /var/www/ironpdf.com/application/libraries/Render.php
Line: 88
Function: view

File: /var/www/ironpdf.com/application/libraries/Render.php
Line: 552
Function: view

File: /var/www/ironpdf.com/application/controllers/Products/Howto.php
Line: 31
Function: render_products_view

File: /var/www/ironpdf.com/index.php
Line: 292
Function: require_once

準備好開始了嗎?
Nuget 下載 16,154,058 | 版本: 2025.11 剛剛發布