Viewing PDFs in MAUI for C# .NET

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

IronPDF 檢視器橫幅

在現代跨平台開發的時代,為用戶提供在應用程式中直接查看 PDF 文件的功能不僅是方便,還是必要。 利用 IronPDF 檢視器,您可以將 PDF 檢視功能嵌入到您的 MAUI 應用程式中。

在本文中,我們將學習如何在 MAUI 應用程式中整合 IronPDF 檢視器,讓用戶可以查看、儲存和列印 PDF。

快速入門:在 MAUI 中使用 IronPDF 檢視 PDF

輕鬆集成 IronPDF 到您的 MAUI 應用程式中,開始無縫檢視 PDF。 此簡單代碼片段演示了如何實例化 IronPDF PdfViewer 並加載 PDF 文件以立即查看。 非常適合希望提升應用程式 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.

    new IronPdf.Viewer.Maui.PdfViewer { Source = "document.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. 下載並安裝 IronPDF 檢視庫
  2. 將 IronPDF 檢視器集成到 MAUI 應用程式中
  3. 通過添加 XAML 或 C# ContentPage 添加 PDF 檢視頁面
  4. 通過文件名、字節數組和流在啟動時加載 PDF
  5. 配置工具欄


下載並安裝 IronPDF Viewer 庫

立即開始在您的項目中使用 IronPDF 並免費試用。

第一步:
green arrow pointer

Visual Studio - NuGet 套件管理員

在 Visual Studio 中,右鍵單擊解決方案瀏覽器中的項目並選擇 管理 NuGet 套件...。 在那裡,您可以搜尋 IronPdf.Viewer.Maui 並將最新版本安裝到您的解決方案中。 或者,您可以通過導航到 工具 > NuGet 套件管理員 > 套件管理員控制台 打開 NuGet 套件管理器控制台,並輸入以下命令:

Install-Package IronPdf.Viewer.Maui

將 IronPDF 檢視器集成到 MAUI 應用程式中

在以下章節中,我們將演示如何將 IronPDF Viewer 集成到默認的 MAUI 應用程式中。

設置

在向您的 MAUI 專案添加 IronPDF 檢視器之前,請確保它不針對 iOS 和 Android 平台。 您可以通過右鍵單擊項目文件並選擇 屬性 來檢查。 如果尚未取消勾選,請取消勾選“目標 iOS 平台”和“目標 Android 平台”選項框。 為成功實施此更改,您可能需要在取消勾選後保存項目並重啟 Visual Studio。

屬性畫面

取消針對 iOS 和 Android 平台後,轉到您的 MauiProgram.cs 文件並添加以下代碼來初始化查看器:

:path=/static-assets/pdf/content-code-examples/tutorials/pdf-viewing-1.cs
using IronPdf.Viewer.Maui;

public static class MauiProgram
{
    public static MauiApp CreateMauiApp()
    {
        var builder = MauiApp.CreateBuilder();
        builder
            .UseMauiApp<App>()
            // other configuration options ...
            .ConfigureIronPdfView(); // configure the viewer on app start-up

        return builder.Build();
    }
}
Imports IronPdf.Viewer.Maui

Public Module MauiProgram
	Public Function CreateMauiApp() As MauiApp
		Dim builder = MauiApp.CreateBuilder()
		builder.UseMauiApp(Of App)().ConfigureIronPdfView() ' configure the viewer on app start-up

		Return builder.Build()
	End Function
End Module
$vbLabelText   $csharpLabel

默認情況下,IronPDF 檢視器會在視圖的右下角顯示一個橫幅。 要移除此視圖,請在 ConfigureIronPdfViewer 中添加您的 IronPDF(或 Iron Suite)許可證密鑰,如下所示:

:path=/static-assets/pdf/content-code-examples/tutorials/pdf-viewing-2.cs
.ConfigureIronPdfView("YOUR-LICENSE-KEY");
IRON VB CONVERTER ERROR developers@ironsoftware.com
$vbLabelText   $csharpLabel

添加 PDF 檢視器頁面

在本節中,我們將學習如何創建 PDF 檢視器頁面,整合 IronPDF Viewer,並在 MAUI 應用程式中為其創建一個選項卡。 我們將演示如何使用 XAML 和 C# ContentPage 來完成此操作。

步驟

  1. 右鍵單擊您的項目以添加新頁面,然後導航到添加 > 新項目... 添加新項目

  2. 瀏覽到.NET MAUI部分。 若要創建 XAML 頁面,選擇.NET MAUI ContentPage (XAML)。 對於 C# 文件,選擇.NET MAUI ContentPage (C#)。 為您的文件命名為 PdfViewerPage,然後單擊添加.NET MAUI `ContentPage`

  3. 在 XAML 文件中添加以下代碼並保存:
:path=/static-assets/pdf/tutorials/pdf-viewing/pdf-viewing-xaml-1.xml
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage ...
    xmlns:ipv="clr-namespace:IronPdf.Viewer.Maui;assembly=IronPdf.Viewer.Maui"
    ...>
    <ipv:IronPdfView x:Name="pdfView"/>
</ContentPage>
XML

如果您創建的是 C# ContentPage,請添加以下代碼並保存:

:path=/static-assets/pdf/content-code-examples/tutorials/pdf-viewing-3.cs
using IronPdf.Viewer.Maui;

public class MainPage : ContentPage
{
    private readonly IronPdfView pdfView;

    public MainPage()
    {
        InitializeComponent();

        this.pdfView = new IronPdfView { Options = IronPdfViewOptions.All };

        Content = this.pdfView;
    }
}
Imports IronPdf.Viewer.Maui

Public Class MainPage
	Inherits ContentPage

	Private ReadOnly pdfView As IronPdfView

	Public Sub New()
		InitializeComponent()

		Me.pdfView = New IronPdfView With {.Options = IronPdfViewOptions.All}

		Content = Me.pdfView
	End Sub
End Class
$vbLabelText   $csharpLabel
  1. 在您的 AppShell.xaml 文件中添加以下代碼:
:path=/static-assets/pdf/tutorials/pdf-viewing/pdf-viewing-xaml-2.xml
<?xml version="1.0" encoding="UTF-8" ?>
<Shell ...
    xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
    ...>
  <TabBar x:Name="AppTabBar">
      <Tab Title="Home">
        <ShellContent ContentTemplate="{DataTemplate local:MainPage}" Route="MainPage"/>
      </Tab>
      <Tab Title="PDF Viewer">
        <ShellContent ContentTemplate="{DataTemplate local:PdfViewerPage}" Route="PDFViewer"/>
    </Tab>
  </TabBar>
</Shell>
XML
  1. 保存您的項目,然後構建和運行。 您應該會在左上角看到選項卡,如下所示,然後點擊“PDF 檢視器”選項卡應該會打開 IronPDF 檢視器。

IronPDF 檢視器默認

在啟動時加載 PDF

在應用程式啟動時,IronPDF 檢視器會默認提示用戶打開 PDF。 也可以在啟動時自動打開 PDF。 您可以通過三種方式在啟動時加載 PDF:通過文件名,通過字節數組和通過流。

通過文件名加載

要通過文件名加載 PDF,您可以在 XAML 文件中的 IronPdfView 標籤中指定 PDF 文件的來源。下面展示了一個示例:

:path=/static-assets/pdf/tutorials/pdf-viewing/pdf-viewing-xaml-3.xml
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage ...
    xmlns:ipv="clr-namespace:IronPdf.Viewer.Maui;assembly=IronPdf.Viewer.Maui"
    ...>
    <ipv:IronPdfView Source="C:/path/to/my/example.pdf" />
</ContentPage>
XML

或者,您也可以通過 C# ContentPage 使用 IronPdfViewSource.FromFile 方法按文件名加載 PDF:

:path=/static-assets/pdf/content-code-examples/tutorials/pdf-viewing-4.cs
// We assume an IronPdfView instance is created previously called pdfView
pdfView.Source = IronPdfViewSource.FromFile("C:/path/to/my/example.pdf");
' We assume an IronPdfView instance is created previously called pdfView
pdfView.Source = IronPdfViewSource.FromFile("C:/path/to/my/example.pdf")
$vbLabelText   $csharpLabel

通過字節數組加載

在某些用例中,可能更希望加載 PDF 的字節數組。 這在 XAML 中無法實現,但可以在 C# 中實現。 您只需使用 IronPdfViewSource.FromBytes 方法即可實現。 下面展示了如何使用此方法的示例:

:path=/static-assets/pdf/content-code-examples/tutorials/pdf-viewing-5.cs
pdfView.Source = IronPdfViewSource.FromBytes(File.ReadAllBytes("~/Downloads/example.pdf"));
pdfView.Source = IronPdfViewSource.FromBytes(File.ReadAllBytes("~/Downloads/example.pdf"))
$vbLabelText   $csharpLabel

通過流加載

同樣,在某些用例中,更希望通過流加載 PDF。 這在 XAML 中無法實現,但可以在 C# 中實現。 您只需使用 IronPdfViewSource.FromStream 方法即可實現。 下面展示了如何使用此方法的示例:

:path=/static-assets/pdf/content-code-examples/tutorials/pdf-viewing-6.cs
pdfView.Source = IronPdfViewSource.FromStream(File.OpenRead("~/Downloads/example.pdf"));
pdfView.Source = IronPdfViewSource.FromStream(File.OpenRead("~/Downloads/example.pdf"))
$vbLabelText   $csharpLabel

配置工具欄

使用 IronPDF 檢視器,您可以選擇在工具欄中顯示哪些選項。 可用選項有:

  • 縮略圖檢視
  • 文件名顯示
  • 文本搜索
  • 頁碼導航
  • 縮放
  • 適合寬度
  • 適合高度
  • 順時針旋轉
  • 逆時針旋轉
  • 打開文件
  • 下載文件
  • 列印文件
  • 顯示註釋
  • 雙頁檢視

默認情況下,IronPDF 檢視器會顯示如下所示的工具欄:

默認工具欄

在默認視圖中,文件名顯示、文本搜尋和逆時針旋轉選項均被禁用。 要顯示全部內容,請在 XAML 中將 IronPdfView 標籤的Option參數設置為All

:path=/static-assets/pdf/tutorials/pdf-viewing/pdf-viewing-xaml-4.xml
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage ...
    xmlns:ipv="clr-namespace:IronPdf.Viewer.Maui;assembly=IronPdf.Viewer.Maui"
    ...>
    <ipv:IronPdfView x:Name="pdfView" Options="All"/>
</ContentPage>
XML

或者,您可以在 C# 中實現相同效果:

:path=/static-assets/pdf/content-code-examples/tutorials/pdf-viewing-7.cs
pdfView.Options = IronPdfViewOptions.All;
pdfView.Options = IronPdfViewOptions.All
$vbLabelText   $csharpLabel

這將顯示如下內容:

全部工具欄

如果不想顯示任何東西,將選項設置為None。 如果將Options設置為這個,工具欄將不會出現:

無工具欄

您可以選擇希望顯示的特定選項。 例如,如果您只想顯示縮略圖和打開文件選項,請像這樣修改 XAML 中 IronPdfViewOptions參數:

:path=/static-assets/pdf/tutorials/pdf-viewing/pdf-viewing-xaml-5.xml
<ipv:IronPdfView x:Name="pdfView" Options="Thumbs, Open"/>
XML

同樣地,在 C# 中:

:path=/static-assets/pdf/content-code-examples/tutorials/pdf-viewing-8.cs
pdfView.Options = IronPdfViewOptions.Thumbs | IronPdfViewOptions.Open;
pdfView.Options = IronPdfViewOptions.Thumbs Or IronPdfViewOptions.Open
$vbLabelText   $csharpLabel

這將顯示如下內容:

具有縮略圖和打開文件選項的工具欄

結論

在本教程中,我們學習了如何將 IronPDF Viewer 集成到 MAUI 應用程式中,以及如何自定義其工具欄以最好地滿足您的需求。

該檢視器附帶了我們的 IronPDF 產品。 如果您想提出功能請求或有任何關於 IronPDF Viewer(或 IronPDF)的一般問題,請 聯繫我們的支持團隊。 我們將很樂意為您提供協助。

常見問題解答

如何在MAUI應用程式中查看PDF文件?

若要在 MAUI 應用程式中檢視 PDF,您可以透過從 Visual Studio 中的 NuGet 套件管理器安裝 IronPDF Viewer,並將所需的程式碼新增至您的專案中,從而整合 IronPDF Viewer。

在 MAUI 應用中整合 PDF 檢視器需要哪些步驟?

確保您的 MAUI 專案相容,透過 NuGet 下載 IronPDF Viewer 庫,並在您的 _MauiProgram.cs_ 檔案中使用您的 IronPDF 許可證金鑰初始化檢視器。

MAUI應用程式啟動時如何載入PDF檔案?

您可以透過在 XAML 檔案中設定來源或在 C# ContentPage 中使用IronPdfViewSource.FromFileFromBytesFromStream等方法來在啟動時載入 PDF。

如何自訂 MAUI PDF 檢視器中的工具列?

透過在 XAML 或 C# 程式碼中配置「選項」參數來自訂工具欄,以包含縮圖視圖、文字搜尋、縮放等功能,或將其設定為「全部」以獲得完整功能。

是否可以在MAUI PDF檢視器中隱藏工具列?

是的,將“選項”參數設為“無”,即可隱藏工具列並阻止其顯示任何工具。

MAUI 中 PDF 檢視器常見的故障排除步驟有哪些?

確保透過 NuGet 正確安裝了 IronPDF Viewer,檢查專案相容性,並驗證所有必要的程式碼(例如許可證金鑰初始化)是否已在專案文件中正確實作。

我可以在適用於 iOS 或 Android 的 MAUI 應用程式中使用 PDF 檢視器嗎?

IronPDF Viewer 目前不支援 iOS 或 Android 平台的 MAUI 專案。請確保您的專案面向相容的平台。

如何提交功能請求或獲得PDF檢視器的支援?

如需功能請求或支持,請透過其官方網站聯絡 IronPDF 支援團隊,以取得 PDF 檢視器的協助。

IronPDF Viewer 是否相容於 MAUI 專案中的 .NET 10?

是的——IronPDF 完全相容於 .NET 10,就像它相容於先前的版本,例如 .NET 6、.NET 7 和 .NET Core 一樣。這意味著在面向 .NET 10 的 MAUI 應用程式中使用 IronPDF 無需任何特殊配置或變通方法。

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,133,208 | 版本: 2025.11 剛剛發布