使用 IronPDF 在 .NET MAUI 中建立 PDF 文件
.NET 多平台應用程式 UI (.NET MAUI) 是一個跨平台框架,用於使用 C# 和 XAML 建立原生行動和桌面應用程式。 您可以使用 .NET MAUI,透過一套程式碼庫在 Android、iOS、macOS 和 Windows 上運行。 .NET MAUI 是開源的,並且比 Xamarin Forms 有所改進。 為了提高效能和可擴展性,使用者介面控制已從頭開始重寫。 此外,它已從行動平台擴展到包括桌面應用場景。 如果您曾經使用 Xamarin Forms 建立跨平台使用者介面,那麼您會發現 .NET MAUI 框架與它有許多共同之處。
然而,兩者之間也存在一些差異。 使用 .NET MAUI 框架,您可以使用單一專案建立多平台應用程序,但也可以根據需要添加特定於平台的程式碼和資源。 .NET MAUI 的主要目標之一是讓您盡可能在單一程式碼庫中編寫應用程式邏輯和 UI 佈局。 .NET MAUI 將適用於所有平台,並將支援現有的 MVVM 和 XAML 模式。
.NET MAUI 允許開發人員在各種平台上工作,例如行動開發和 Windows 平台。
IronPDF 功能。
IronPDF 是一款功能強大的 PDF 轉換器,幾乎可以處理瀏覽器可以處理的任何任務。 面向開發人員的 .NET 程式庫讓建立、讀取和操作 PDF 檔案變得簡單。 IronPDF 使用 Google Chrome 引擎將 HTML 檔案轉換為 PDF 檔案。 IronPDF 支援多種 Web 技術,包括 HTML、ASPX、使用 IronPDF 將 Razor 頁面轉換為 PDF以及 MVC 視圖。 IronPDF 支援 Microsoft .NET 應用程式(包括 ASP.NET Web 應用程式和傳統 Windows 應用程式)。 IronPDF 也可用於製作吸引人的 PDF 文件。
IronPDF 可以從包含 JavaScript、CSS 和圖像檔案的 HTML 檔案建立 PDF 檔案。
IronPDF不僅可以將HTML檔案轉換為PDF ,還可以將影像檔案轉換為PDF 。
IronPDF 可以協助建立互動式 PDF 文檔,使用 IronPDF 填寫和提交互動式表單,合併和分割 PDF 文件,從 PDF 文件中提取文字和圖像,在 PDF 文件中搜尋文本,將 PDF 頁面柵格化為圖像,將 PDF 轉換為 HTML,並使用 IronPrint 列印 PDF 文件。
IronPDF 可以根據 URL 產生文件。 對於使用 IronPDF 的登入表單在 HTML 後進行登錄,它還允許使用自訂網頁登入憑證、使用者代理程式、代理、cookie、HTTP 標頭和表單變數。
IronPDF 是一個可以讀取和填充 PDF 文檔,並能夠從文檔中提取圖像的庫。 它允許我們為 PDF 文件添加頁眉和頁腳、文字、照片、書籤以便於導航、浮水印等。 它還允許我們在新文檔或現有文檔中合併和分割頁面。 IronPDF 無需使用 Acrobat 檢視器即可將文件轉換為 PDF 物件。 也可以將 CSS 文件轉換為 PDF 文檔,CSS 媒體類型文件也可以轉換為文檔。
如何在 .NET MAUI 中建立 PDF 文件
- 在Visual Studio中創建一個新的.NET MAUI PDF專案
- 使用NuGet安裝IronPDF庫
- 設計.NET MAUI內容頁面的UI
- 使用
ChromePdfRenderer.RenderHtmlAsPdf方法產生 PDF 文件 - 使用平台特定代碼將PDF文件保存到用戶設備。
在 Visual Studio 中建立新專案
首先,分別使用 Visual Studio Code 和 Visual Studio 建立一個專案。 使用 Visual Studio Code 時,需要使用命令列工具來安裝專案類型範本。
開啟 Microsoft Visual Studio 軟體,然後前往"檔案"功能表。 選擇"新專案",然後在新專案視窗中選擇".NET MAUI 應用程式"。本文將使用 .NET MAUI 應用程式產生 PDF 文件並將其儲存到適當的位置。
使用 IronPDF 在 .NET MAUI 中建立 PDF 文件,圖 1:在 Visual Studio 中建立一個新項目 在 Visual Studio 中建立一個新項目
在適當的文字方塊中輸入專案名稱並選擇檔案路徑。 然後,點擊"建立"按鈕,如下圖所示。
在 .NET MAUI 中使用 IronPDF 建立 PDF 文件,圖 2:配置項目 配置專案
Visual Studio 專案現在將依照 .NET MAUI 應用程式平台產生所選應用程式的結構。 現在它將打開MainPage.cs檔案以添加程式碼並建置/運行應用程式。
在 .NET MAUI 中使用 IronPDF 建立 PDF 文件,圖 3:在 MainPage.cs 文件中新增程式碼 在 MainPage.cs 檔案中加入程式碼
接下來,安裝庫以測試程式碼。
安裝 IronPdf Library
IronPDF Library 可以通過四種方式下載和安裝。
這些工具包括
- 使用 Visual Studio。
- 使用 Visual Studio Command-Line。
- 直接從 NuGet 網站下載。
- 直接從 IronPdf 網站下載。
使用 Visual Studio
Visual Studio 軟體提供 NuGet Package Manager 選項,可直接將套件安裝至解決方案。 下面的截圖顯示了如何打開 NuGet Package Manager。
在 .NET MAUI 中使用 IronPDF 建立 PDF 文件,圖 4:使用 NuGet 套件管理器 UI 進行導航 導航至 NuGet 套件管理器使用者介面
這將顯示搜尋框,用於尋找 NuGet 網站上的套件清單。在 NuGet 套件管理器中,搜尋關鍵字"IronPDF",如下圖所示。
圖 5:在 .NET MAUI 中使用 IronPDF 建立 PDF 檔案:從 NuGet 套件管理器 UI 中選擇 IronPDF 套件 透過 NuGet 套件管理器 UI 中的 IronPDF 套件
上圖中列出了相關的搜尋項,選擇第一個選項將軟體包安裝到解決方案中。 它將安裝在所有 .NET MAUI 應用程式平台上,例如 Android、iOS 和 Windows 平台。
使用 Visual Studio Command-Line
在 Visual Studio 中,前往"工具" > "NuGet 套件管理員" > "套件管理員控制台"。
在程式包管理器控制台標籤中輸入以下代碼:
Install-Package IronPdf
現在,該軟體包將下載/安裝到目前專案中的所有 .NET MAUI 平台,即可使用。
在 .NET MAUI 中使用 IronPDF 建立 PDF 文件,圖 6:在程式包管理器控制台中安裝程式包 在軟體包管理器控制台中安裝軟體包
直接從 NuGet 網站下載
第三種方法是透過造訪IronPDF NuGet repository page ,直接從他們的網站下載 NuGet 套件。
- 請從右側功能表中選擇下載套件選項。
- 雙擊下載的套件。 它會自動安裝。
- 重新載入解決方案後,該軟體包應該就可以在您的專案中使用了。
直接從 IronPdf 網站下載。
直接從IronPDF 官網下載最新的 ZIP 軟體包。下載完成後,請按照以下步驟將該軟體包新增至您的專案。
- 在解決方案視窗中以滑鼠右鍵按一下專案的"依賴項"選項。
- 選擇特定平台,然後選擇選項參考,並瀏覽下載參考文件的位置。
- 按一下"確定"以新增引用。
您還需要新增對所有 .NET MAUI 支援的平台的參考。
使用 IronPDF 在 .NET MAUI 應用程式中建立 PDF
建立專案時,會自動產生名為MainPage.xaml的檔案。 這裡是放置 .NET MAUI 應用程式使用者介面的地方。
請按照以下代碼操作。
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="MAUI_PDF.MainPage"
>
<ScrollView>
<VerticalStackLayout
Spacing="25"
Padding="30,0"
VerticalOptions="Center">
<Image
Source="dotnet_bot.png"
SemanticProperties.Description="Cute .NET bot waving hi to you!"
HeightRequest="200"
HorizontalOptions="Center" />
<Label
Text="Welcome to IronPDF!"
SemanticProperties.HeadingLevel="Level1"
FontSize="32"
HorizontalOptions="Center" />
<Button
x:Name="PdfBtn"
Text="Click me to generate PDF"
SemanticProperties.Hint="Click button to generate PDF"
Clicked="GeneratePDF"
HorizontalOptions="Center" />
</VerticalStackLayout>
</ScrollView>
</ContentPage><?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="MAUI_PDF.MainPage"
>
<ScrollView>
<VerticalStackLayout
Spacing="25"
Padding="30,0"
VerticalOptions="Center">
<Image
Source="dotnet_bot.png"
SemanticProperties.Description="Cute .NET bot waving hi to you!"
HeightRequest="200"
HorizontalOptions="Center" />
<Label
Text="Welcome to IronPDF!"
SemanticProperties.HeadingLevel="Level1"
FontSize="32"
HorizontalOptions="Center" />
<Button
x:Name="PdfBtn"
Text="Click me to generate PDF"
SemanticProperties.Hint="Click button to generate PDF"
Clicked="GeneratePDF"
HorizontalOptions="Center" />
</VerticalStackLayout>
</ScrollView>
</ContentPage>將上述程式碼加入MainPage.xaml檔案後,開啟MainPage.xaml.cs文件,並在 MainPage 類別中包含以下方法:
private void GeneratePDF(object sender, EventArgs e)
{
ChromePdfRenderer renderer = new ChromePdfRenderer();
var doc = renderer.RenderHtmlAsPdf("<h1>Hello IronPDF!</h1> <p>I'm using IronPDF MAUI!</p>");
// Saves the memory stream as file.
SaveService saveService = new SaveService();
saveService.SaveAndView("IronPDF HTML string.pdf", "application/pdf", doc.Stream);
}private void GeneratePDF(object sender, EventArgs e)
{
ChromePdfRenderer renderer = new ChromePdfRenderer();
var doc = renderer.RenderHtmlAsPdf("<h1>Hello IronPDF!</h1> <p>I'm using IronPDF MAUI!</p>");
// Saves the memory stream as file.
SaveService saveService = new SaveService();
saveService.SaveAndView("IronPDF HTML string.pdf", "application/pdf", doc.Stream);
}GeneratePDF方法產生一個新的 PDF 文檔,其中包含文字"Hello, IronPDF!...",並將其儲存到使用者電腦上的指定位置。
在上述方法中,建立了一個新的ChromePdfRenderer對象,並呼叫RenderHtmlAsPdf方法產生一個新的 PDF 文檔,其中包含"Hello IronPDF!"字樣。 ……從一段 HTML 標記字串中提取。接下來,將文件儲存到使用者裝置上的任務委託給一個名為 SaveService 的單獨類別。該類別將在下一步中建立。
完整的MainPage.xaml.cs檔案如下所示。 請確保文件內容與下方所示內容一致,然後再進行下一步:
// Change the namespace as desired, but make sure that all source files use this same namespace,
// or there will be errors!
namespace MAUI_IronPDF;
// This namespace is required to make use of IronPDF functionality
using IronPdf;
public partial class MainPage : ContentPage
{
public MainPage()
{
InitializeComponent();
}
private void GeneratePDF(object sender, EventArgs e)
{
ChromePdfRenderer renderer = new ChromePdfRenderer();
var doc = renderer.RenderHtmlAsPdf("<h1>Hello IronPDF!</h1> <p>I'm using IronPDF MAUI!</p>");
// Saves the memory stream as file.
SaveService saveService = new SaveService();
saveService.SaveAndView("IronPDF HTML string.pdf", "application/pdf", doc.Stream);
}
}// Change the namespace as desired, but make sure that all source files use this same namespace,
// or there will be errors!
namespace MAUI_IronPDF;
// This namespace is required to make use of IronPDF functionality
using IronPdf;
public partial class MainPage : ContentPage
{
public MainPage()
{
InitializeComponent();
}
private void GeneratePDF(object sender, EventArgs e)
{
ChromePdfRenderer renderer = new ChromePdfRenderer();
var doc = renderer.RenderHtmlAsPdf("<h1>Hello IronPDF!</h1> <p>I'm using IronPDF MAUI!</p>");
// Saves the memory stream as file.
SaveService saveService = new SaveService();
saveService.SaveAndView("IronPDF HTML string.pdf", "application/pdf", doc.Stream);
}
}現在,在專案根目錄下建立一個名為SaveService.cs的新類別文件,並新增以下原始程式碼:
// Change the namespace as desired, but make sure that all source files use this same namespace,
// or there will be errors!
namespace MAUI_IronPDF
{
// SaveService partial class declaration ... this allows a layer of abstraction
// as we implement the save file details specially for each platform on which this app will
// operate!
public partial class SaveService
{
public void SaveAndView(string filename, string contentType, MemoryStream stream)
{
SaveFile(filename, contentType, stream);
}
// Additional partial files will provide implementations for this method specifically.
partial void SaveFile(string filename, string contentType, MemoryStream stream);
}
}// Change the namespace as desired, but make sure that all source files use this same namespace,
// or there will be errors!
namespace MAUI_IronPDF
{
// SaveService partial class declaration ... this allows a layer of abstraction
// as we implement the save file details specially for each platform on which this app will
// operate!
public partial class SaveService
{
public void SaveAndView(string filename, string contentType, MemoryStream stream)
{
SaveFile(filename, contentType, stream);
}
// Additional partial files will provide implementations for this method specifically.
partial void SaveFile(string filename, string contentType, MemoryStream stream);
}
}由於在使用者裝置上保存內容的實作細節因平台(Windows、Android、macOS 等)而異,因此必須為應用程式將支援的每種裝置類型編寫特定於平台的程式碼。 為了實現這一點,我們將SaveService定義為一個部分類別(為了抽象的目的),其中包含一個名為SaveAndView部分方法。 接下來,此方法的實作將在單獨的SaveService.cs分部類別中定義,該分部類別適用於解決方案資源管理器中 Platforms 資料夾下的一個或多個資料夾(見下圖):
!在 .NET MAUI 中使用 IronPDF 建立 PDF 文件,圖 7:實作跨平台的資料夾結構 實作跨平台的資料夾結構
為簡單起見,本教學將僅為 Windows 平台定義上述部分檔案。 在 Windows 平台資料夾下建立一個新的SaveService.cs文件,並新增以下程式碼:
using Windows.Storage;
using Windows.Storage.Pickers;
using Windows.Storage.Streams;
using Windows.UI.Popups;
namespace MAUI_IronPDF;
public partial class SaveService
{
async partial void SaveFile(string filename, string contentType, MemoryStream stream)
{
StorageFile stFile;
string extension = Path.GetExtension(filename);
// Gets process windows handle to open the dialog in application process.
IntPtr windowHandle = System.Diagnostics.Process.GetCurrentProcess().MainWindowHandle;
if (!Windows.Foundation.Metadata.ApiInformation.IsTypePresent("Windows.Phone.UI.Input.HardwareButtons"))
{
// Creates file save picker to save a file.
FileSavePicker savePicker = new();
savePicker.DefaultFileExtension = ".pdf";
savePicker.SuggestedFileName = filename;
// Saves the file as PDF file.
savePicker.FileTypeChoices.Add("PDF", new List<string>() { ".pdf" });
WinRT.Interop.InitializeWithWindow.Initialize(savePicker, windowHandle);
stFile = await savePicker.PickSaveFileAsync();
}
else
{
StorageFolder local = ApplicationData.Current.LocalFolder;
stFile = await local.CreateFileAsync(filename, CreationCollisionOption.ReplaceExisting);
}
if (stFile != null)
{
using (IRandomAccessStream zipStream = await stFile.OpenAsync(FileAccessMode.ReadWrite))
{
// Writes compressed data from memory to file.
using Stream outstream = zipStream.AsStreamForWrite();
outstream.SetLength(0);
// Saves the stream as file.
byte [] buffer = stream.ToArray();
outstream.Write(buffer, 0, buffer.Length);
outstream.Flush();
}
// Create message dialog box.
MessageDialog msgDialog = new("Do you want to view the document?", "File has been created successfully");
UICommand yesCmd = new("Yes");
msgDialog.Commands.Add(yesCmd);
UICommand noCmd = new("No");
msgDialog.Commands.Add(noCmd);
WinRT.Interop.InitializeWithWindow.Initialize(msgDialog, windowHandle);
// Showing a dialog box.
IUICommand cmd = await msgDialog.ShowAsync();
if (cmd.Label == yesCmd.Label)
{
// Launch the saved file.
await Windows.System.Launcher.LaunchFileAsync(stFile);
}
}
}
}using Windows.Storage;
using Windows.Storage.Pickers;
using Windows.Storage.Streams;
using Windows.UI.Popups;
namespace MAUI_IronPDF;
public partial class SaveService
{
async partial void SaveFile(string filename, string contentType, MemoryStream stream)
{
StorageFile stFile;
string extension = Path.GetExtension(filename);
// Gets process windows handle to open the dialog in application process.
IntPtr windowHandle = System.Diagnostics.Process.GetCurrentProcess().MainWindowHandle;
if (!Windows.Foundation.Metadata.ApiInformation.IsTypePresent("Windows.Phone.UI.Input.HardwareButtons"))
{
// Creates file save picker to save a file.
FileSavePicker savePicker = new();
savePicker.DefaultFileExtension = ".pdf";
savePicker.SuggestedFileName = filename;
// Saves the file as PDF file.
savePicker.FileTypeChoices.Add("PDF", new List<string>() { ".pdf" });
WinRT.Interop.InitializeWithWindow.Initialize(savePicker, windowHandle);
stFile = await savePicker.PickSaveFileAsync();
}
else
{
StorageFolder local = ApplicationData.Current.LocalFolder;
stFile = await local.CreateFileAsync(filename, CreationCollisionOption.ReplaceExisting);
}
if (stFile != null)
{
using (IRandomAccessStream zipStream = await stFile.OpenAsync(FileAccessMode.ReadWrite))
{
// Writes compressed data from memory to file.
using Stream outstream = zipStream.AsStreamForWrite();
outstream.SetLength(0);
// Saves the stream as file.
byte [] buffer = stream.ToArray();
outstream.Write(buffer, 0, buffer.Length);
outstream.Flush();
}
// Create message dialog box.
MessageDialog msgDialog = new("Do you want to view the document?", "File has been created successfully");
UICommand yesCmd = new("Yes");
msgDialog.Commands.Add(yesCmd);
UICommand noCmd = new("No");
msgDialog.Commands.Add(noCmd);
WinRT.Interop.InitializeWithWindow.Initialize(msgDialog, windowHandle);
// Showing a dialog box.
IUICommand cmd = await msgDialog.ShowAsync();
if (cmd.Label == yesCmd.Label)
{
// Launch the saved file.
await Windows.System.Launcher.LaunchFileAsync(stFile);
}
}
}
}建置並運行 MAUI 應用程式。 將出現一個窗口,其中包含如下所示的介面:
使用 IronPDF 在 .NET MAUI 中建立 PDF 文件,圖 8:MAUI 應用程式的使用者介面 MAUI應用程式的使用者介面
點擊"生成PDF"按鈕。 片刻之後,將彈出一個窗口,供用戶選擇生成的 PDF 文件的位置。
在 .NET MAUI 中使用 IronPDF 建立 PDF 文件,圖 9:選擇儲存 PDF 文件的位置 選擇儲存 PDF 檔案的位置
結論
IronPDF 是最常用的 PDF 轉換庫之一,它允許您產生、讀取、編輯和格式化 PDF 文件。 IronPDF 庫提供了許多優勢和功能,包括一個瀏覽器引擎,可以幫助將給定的 URL 轉換為 PDF 文件,讓您可以向 HTML 字串添加 CSS 並將其轉換為 PDF 文件,還允許您填寫 PDF 表單。 IronPDF 的所有功能都包含在一個資料庫中。
IronPdf 有多種價格結構。 IronPDF 的基本價格從 $799 開始。 產品支援和更新服務也提供一年期付費服務。 還可以額外購買免版稅再分送保障。
總而言之,IronPDF 性能出色,功能豐富,是開發 PDF 文件的理想選擇,值得推薦。 它支援 .NET MAUI 等通用平台。 它還提供了優秀的幫助和文檔,讓您可以充分利用 IronPDF 庫的各種功能和功能。
常見問題解答
如何在.NET MAUI應用程式中產生PDF檔案?
要在 .NET MAUI 應用程式中產生 PDF 文件,可以使用 IronPDF。首先在 Visual Studio 中設定一個 .NET MAUI 項目,透過 NuGet 安裝 IronPDF,然後使用ChromePdfRenderer.RenderHtmlAsPdf方法將 HTML 內容轉換為 PDF 文件。
是什麼讓 .NET MAUI 適合跨平台應用程式開發?
.NET MAUI 非常適合跨平台應用程式開發,因為它允許開發人員使用 C# 和 XAML 從單一程式碼庫為 Android、iOS、macOS 和 Windows 建立原生行動和桌面應用程式。
IronPDF 如何處理 PDF 轉換過程中的 HTML、CSS 和 JavaScript?
IronPDF 使用 Google Chrome 引擎以 PDF 格式精確渲染 HTML、CSS 和 JavaScript,確保網頁轉換時不會遺失格式。
在.NET MAUI應用程式中,如何將PDF檔案保存到不同的平台上?
在使用 IronPDF 的 .NET MAUI 應用程式中, SaveService類別用於保存 PDF 檔案。它包含針對不同平台的實現,以處理在 Windows 和 Android 等作業系統上的檔案保存操作。
IronPDF for .NET 應用程式有哪些進階功能?
IronPDF 提供進階功能,例如建立互動式 PDF、填寫和提交表單、合併和分割文件、提取文字和圖像以及新增頁首、頁尾和浮水印。
如何在 Visual Studio 中將 IronPDF 整合到 .NET MAUI 專案中?
若要將 IronPDF 整合到 .NET MAUI 專案中,請使用 Visual Studio 中的 NuGet 套件管理器。搜尋「IronPDF」並將其安裝到您的專案中,即可開始產生和管理 PDF 文件。
IronPDF 能否在 .NET MAUI 應用程式中將網頁 URL 轉換為 PDF?
是的,IronPDF 可以利用其網頁渲染功能,準確地捕獲 HTML、CSS 和 JavaScript 內容,並將其渲染成 PDF 格式,從而將網頁 URL 轉換為 PDF。
XAML 在 .NET MAUI 應用程式的 UI 設計中扮演什麼角色?
在 .NET MAUI 中,XAML 用於設計應用程式的使用者介面。它允許開發人員創建響應迅速且視覺效果出色的佈局,這些佈局可以在多個平台上無縫運行。
在.NET MAUI專案中使用IronPDF有哪些好處?
IronPDF 具有易用性、豐富的文件、高效能以及處理各種 PDF 任務的能力等優點,使其成為在 .NET MAUI 專案中處理 PDF 的開發人員的可靠選擇。
IronPDF 是否完全相容於 .NET 10?相容 .NET 10 能帶來哪些優勢?
是的,IronPDF 完全相容於 .NET 10,並開箱即用地支援 .NET 9、8、7、6、Core、Standard 和 Framework 版本。將 IronPDF 與 .NET 10 結合使用,您可以充分利用效能改進,例如減少堆疊分配、改進 JIT/執行時間行為以及新的 C# 語言特性,從而使 PDF 的生成和處理更加快速高效。







