在 C# .NET 的 MAUI 中查看 PDF

查克尼思·賓
查克尼思·賓
2023年9月4日
已更新 2025年1月13日
分享:
This article was translated from English: Does it need improvement?
Translated
View the article in English

IronPDF Viewer 橫幅

在當今跨平台開發的時代,讓用戶能夠在您的應用程序中直接查看PDF文件不僅是一種便利,更是一種必需。 使用IronPDF Viewer,您可以將 PDF 檢視功能嵌入到您的 MAUI 應用程式中。

在本文中,我們將學習如何在 MAUI 應用程式中整合IronPDF Viewer,以允許使用者查看、儲存和列印 PDF。


概述


下載並安裝 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應用程序中。

設置

在將 IronPDF Viewer 添加到您的 MAUI 項目之前,請先確保它不針對 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 查看器將在視圖的右下角顯示橫幅。 若要移除此視圖,請將您的 IronPDF(或 Iron Suite)許可證金鑰新增至 ConfigureIronPdfView,如下所示:

: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 查看器頁面

在本節中,我們將學習如何在 MAUI 應用程式中創建一個 PDF 閱讀器頁面,集成 IronPDF Viewer,並為其創建一個標籤頁。 我們將展示如何使用 XAML 和 C# ContentPage 來執行此操作。

步驟

  1. 在專案中新增一個頁面,右鍵點擊你的專案,然後導航到加入 > 新增項目...

    新增項目

  2. 導航至.NET MAUI部分。 若要建立 XAML 頁面,請選擇 .NET MAUI ContentPage (XAML)。 對於 C# 文件,選擇 .NET MAUI ContentPage (C#)。 將您的檔案命名為 PdfViewerPage,然後點擊Add

    .NET MAUI `ContentPage`

  3. 在XAML檔案中,新增以下代碼並保存:
<?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 文件中,添加以下內容:
<?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 Viewer”標籤應該會打開IronPDF Viewer。

    IronPDF Viewer Default

啟動時載入PDF

在應用程式啟動時,IronPDF Viewer 將預設提示使用者開啟 PDF 文件。 它也可以在啟動時自動打開PDF。 有三種方式可以在啟動時加載PDF:通過文件名稱、通過字節數組和通過流。

透過檔名載入

若要通過檔名載入 PDF,您可以在 XAML 檔案中的 IronPdfView 標籤中指定 PDF 檔案的來源。以下範例顯示了這一點:

<?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 Viewer,您可以選擇工具欄中要顯示的選項。 可用的選項包括:

  • 縮圖視圖
  • 檔案名稱顯示
  • 文字搜尋
  • 頁碼導航
  • Zoom
  • 適合寬度
  • 適合高度
  • 順時針旋轉
  • 逆時針旋轉
  • 開啟檔案
  • 下載檔案
  • 列印檔案
  • 顯示註解
  • 雙頁面顯示

    預設情況下,IronPDF Viewer 將顯示以下工具列:

    默認工具列

    在預設視圖中,檔案名稱顯示、文字搜尋和逆時針旋轉選項都被禁用了。 要顯示所有內容,請將 XAML 中 IronPdfView 標籤的 Option 參數設置為 All

<?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 參數,如下所示:

<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)有任何一般問題,請聯絡我們的支援團隊。 我们将非常乐意协助您。

查克尼思·賓
軟體工程師
Chaknith 致力於 IronXL 和 IronBarcode。他在 C# 和 .NET 方面擁有豐富的專業知識,協助改進軟體並支持客戶。他從用戶互動中獲得的洞察力有助於提高產品、文檔和整體體驗。