如何將多頁掃描為一個PDF文件
.NET MAUI 是 .NET的下一代,它讓開發者可以使用單一程式碼庫建置跨平台的桌面、Web和行動應用程式,包括 Xamarin.Forms。 通過.NET MAUI,您可以撰寫一次應用程式並將其部署到多個平台,包括 Windows、macOS、iOS、Android 和 tvOS,並使用相同的專案名稱。 .NET MAUI 還能讓您利用各平台的最新 UI 功能,例如 macOS 的深色模式和觸控支援,或 Windows 10 的語音識別。
本文將說明如何在 .NET MAUI 應用程式中使用 IronPDF 來建立具有諸多優勢的 PDF 文件。
如何在 .NET MAUI 中檢視 PDF 文件
- 安裝 IronPDF 以檢視 .NET MAUI 中的 PDF 文件
- 設置 MAUI 專案的前端設計
- 處理在本地儲存中儲存文件並檢視 PDF
- 對 URL、HTML 字串或文件使用渲染方法
- 將渲染的 PDF 傳遞給步驟3中的自定義處理程式
IronPDF: C# PDF程式庫
IronPDF 是一個 .NET 程式庫,允許您生成和編輯 PDF 文件。 它非常適合在 .NET MAUI 應用程式中使用,因為它提供了廣泛的功能範圍,可以根據您的特定需求量身定制。 IronPDF 擁有易用的 API,讓您輕鬆將 PDF 功能整合到您的 .NET MAUI 專案中。
先決條件
在使用 IronPDF 建立 PDF 和 PDF 檢視器於 .NET MAUI 時,有一些先決條件:
- Visual Studio的最新版本
- .NET Framework 6 或 7
- 在 Visual Studio 中安裝的 MAUI 程式包
- 在 Visual Studio 中運行的 .NET MAUI 應用程式
步驟 1: 安裝 IronPDF
在新專案中安裝 IronPDF 最佳的方式之一是通過 Visual Studio 中的 NuGet 套件管理器控制台。 使用此方法安裝 IronPDF 有一些優勢。
- 這很容易做到,並且
- 您可以確信您使用的是 IronPDF 的最新版本。
安裝 IronPDF 的步驟
首先,打開套件管理器控制台,依次選擇 工具 > NuGet 套件管理器 > 套件管理器控制台。
套件管理器控制台
接下來,輸入以下命令:
Install-Package IronPdf
這將安裝套件及其所有依賴項,例如 assets 資料夾。
IronPDF 安裝
您現在可以在您的 MAUI 專案中開始使用 IronPDF。
步驟 2: 在 .NET MAUI 中設置前端設計
首先,為 IronPDF 的三種功能建立一個佈局。
URL 到 PDF 佈局
對於 URL 到 PDF 佈局,使用 .NET MAUI 標籤控制項建立一個帶有文字"輸入 URL 以轉換為 PDF"的標籤。 之後,應用水平堆疊佈局以水平排列 Entry 控制項和按鈕。 然後在控制項之後放一條線,以分隔下一部分的控制項。
<Label
Text="Enter URL to Convert PDF"
SemanticProperties.HeadingLevel="Level1"
FontSize="18"
HorizontalOptions="Center"
/>
<HorizontalStackLayout
HorizontalOptions="Center">
<Border Stroke="White"
StrokeThickness="2"
StrokeShape="RoundRectangle 5,5,5,5"
HorizontalOptions="Center">
<Entry
x:Name="URL"
HeightRequest="50"
WidthRequest="300"
HorizontalOptions="Center"
/>
</Border>
<Button
x:Name="urlPDF"
Text="Convert URL to PDF"
Margin="30,0,0,0"
Clicked="UrlToPdf"
HorizontalOptions="Center" />
</HorizontalStackLayout>
<Line Stroke="White" X2="1500" />
<Label
Text="Enter URL to Convert PDF"
SemanticProperties.HeadingLevel="Level1"
FontSize="18"
HorizontalOptions="Center"
/>
<HorizontalStackLayout
HorizontalOptions="Center">
<Border Stroke="White"
StrokeThickness="2"
StrokeShape="RoundRectangle 5,5,5,5"
HorizontalOptions="Center">
<Entry
x:Name="URL"
HeightRequest="50"
WidthRequest="300"
HorizontalOptions="Center"
/>
</Border>
<Button
x:Name="urlPDF"
Text="Convert URL to PDF"
Margin="30,0,0,0"
Clicked="UrlToPdf"
HorizontalOptions="Center" />
</HorizontalStackLayout>
<Line Stroke="White" X2="1500" />
HTML 到 PDF 佈局
對於 HTML 到 PDF 部分的佈局,建立一個 Editor 控制項和一個按鈕。 Editor 控制項將用於接收來自使用者的 HTML 內容字串。 此外,新增一條線作為分隔。
<Label
Text="Enter HTML to Convert to PDF"
SemanticProperties.HeadingLevel="Level2"
FontSize="18"
HorizontalOptions="Center" />
<Border
Stroke="White"
StrokeThickness="2"
StrokeShape="RoundRectangle 5,5,5,5"
HorizontalOptions="Center">
<Editor
x:Name="HTML"
HeightRequest="200"
WidthRequest="300"
HorizontalOptions="Center"
/>
</Border>
<Button
x:Name="htmlPDF"
Text="Convert HTML to PDF"
Clicked="HtmlToPdf"
HorizontalOptions="Center" />
<Line Stroke="White" X2="1500" />
<Label
Text="Enter HTML to Convert to PDF"
SemanticProperties.HeadingLevel="Level2"
FontSize="18"
HorizontalOptions="Center" />
<Border
Stroke="White"
StrokeThickness="2"
StrokeShape="RoundRectangle 5,5,5,5"
HorizontalOptions="Center">
<Editor
x:Name="HTML"
HeightRequest="200"
WidthRequest="300"
HorizontalOptions="Center"
/>
</Border>
<Button
x:Name="htmlPDF"
Text="Convert HTML to PDF"
Clicked="HtmlToPdf"
HorizontalOptions="Center" />
<Line Stroke="White" X2="1500" />
HTML 文件到 PDF 佈局
針對 HTML 文件到 PDF,只需新增一個按鈕。 該按鈕將幫助使用 IronPDF 將 HTML 文件轉換為 PDF 文件。
<Label
Text="Convert HTML file to PDF"
SemanticProperties.HeadingLevel="Level2"
FontSize="18"
HorizontalOptions="Center" />
<Button
x:Name="htmlFilePDF"
Text="Convert HTML file to PDF"
Clicked="FileToPdf"
HorizontalOptions="Center" />
<Label
Text="Convert HTML file to PDF"
SemanticProperties.HeadingLevel="Level2"
FontSize="18"
HorizontalOptions="Center" />
<Button
x:Name="htmlFilePDF"
Text="Convert HTML file to PDF"
Clicked="FileToPdf"
HorizontalOptions="Center" />
完整的 UI 程式碼
以下是 .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="PDF_Viewer.MainPage">
<ScrollView>
<VerticalStackLayout
Spacing="25"
Padding="30,0"
VerticalOptions="Center">
<Label
Text="Enter URL to Convert PDF"
SemanticProperties.HeadingLevel="Level1"
FontSize="18"
HorizontalOptions="Center"
/>
<HorizontalStackLayout
HorizontalOptions="Center">
<Border Stroke="White"
StrokeThickness="2"
StrokeShape="RoundRectangle 5,5,5,5"
HorizontalOptions="Center">
<Entry
x:Name="URL"
HeightRequest="50"
WidthRequest="300"
HorizontalOptions="Center"
/>
</Border>
<Button
x:Name="urlPDF"
Text="Convert URL to PDF"
Margin="30,0,0,0"
Clicked="UrlToPdf"
HorizontalOptions="Center" />
</HorizontalStackLayout>
<Line Stroke="White" X2="1500" />
<Label
Text="Enter HTML to Convert to PDF"
SemanticProperties.HeadingLevel="Level2"
FontSize="18"
HorizontalOptions="Center" />
<Border
Stroke="White"
StrokeThickness="2"
StrokeShape="RoundRectangle 5,5,5,5"
HorizontalOptions="Center">
<Editor
x:Name="HTML"
HeightRequest="200"
WidthRequest="300"
HorizontalOptions="Center"
/>
</Border>
<Button
x:Name="htmlPDF"
Text="Convert HTML to PDF"
Clicked="HtmlToPdf"
HorizontalOptions="Center" />
<Line Stroke="White" X2="1500" />
<Label
Text="Convert HTML file to PDF"
SemanticProperties.HeadingLevel="Level2"
FontSize="18"
HorizontalOptions="Center" />
<Button
x:Name="htmlFilePDF"
Text="Convert HTML file to PDF"
Clicked="FileToPdf"
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="PDF_Viewer.MainPage">
<ScrollView>
<VerticalStackLayout
Spacing="25"
Padding="30,0"
VerticalOptions="Center">
<Label
Text="Enter URL to Convert PDF"
SemanticProperties.HeadingLevel="Level1"
FontSize="18"
HorizontalOptions="Center"
/>
<HorizontalStackLayout
HorizontalOptions="Center">
<Border Stroke="White"
StrokeThickness="2"
StrokeShape="RoundRectangle 5,5,5,5"
HorizontalOptions="Center">
<Entry
x:Name="URL"
HeightRequest="50"
WidthRequest="300"
HorizontalOptions="Center"
/>
</Border>
<Button
x:Name="urlPDF"
Text="Convert URL to PDF"
Margin="30,0,0,0"
Clicked="UrlToPdf"
HorizontalOptions="Center" />
</HorizontalStackLayout>
<Line Stroke="White" X2="1500" />
<Label
Text="Enter HTML to Convert to PDF"
SemanticProperties.HeadingLevel="Level2"
FontSize="18"
HorizontalOptions="Center" />
<Border
Stroke="White"
StrokeThickness="2"
StrokeShape="RoundRectangle 5,5,5,5"
HorizontalOptions="Center">
<Editor
x:Name="HTML"
HeightRequest="200"
WidthRequest="300"
HorizontalOptions="Center"
/>
</Border>
<Button
x:Name="htmlPDF"
Text="Convert HTML to PDF"
Clicked="HtmlToPdf"
HorizontalOptions="Center" />
<Line Stroke="White" X2="1500" />
<Label
Text="Convert HTML file to PDF"
SemanticProperties.HeadingLevel="Level2"
FontSize="18"
HorizontalOptions="Center" />
<Button
x:Name="htmlFilePDF"
Text="Convert HTML file to PDF"
Clicked="FileToPdf"
HorizontalOptions="Center" />
</VerticalStackLayout>
</ScrollView>
</ContentPage>
步驟 3: 保存和檢視 PDF 文件的程式碼
.NET MAUI 沒有任何預製的功能可以將文件保存到本地儲存。 因此,有必要自行編寫程式碼。 為了建立保存和檢視功能,建立一個名為 SaveService 的局部類,其中包含一個名為 SaveAndView 並帶有三個參數的局部 void 函式:文件名、文件內容型別和用於寫入文件的記憶體流。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace PDF_Viewer
{
public partial class SaveService
{
public partial void SaveAndView(string filename, string contentType, MemoryStream stream);
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace PDF_Viewer
{
public partial class SaveService
{
public partial void SaveAndView(string filename, string contentType, MemoryStream stream);
}
}
Imports System
Imports System.Collections.Generic
Imports System.Linq
Imports System.Text
Imports System.Threading.Tasks
Namespace PDF_Viewer
Partial Public Class SaveService
Public Partial Private Sub SaveAndView(ByVal filename As String, ByVal contentType As String, ByVal stream As MemoryStream)
End Sub
End Class
End Namespace
需要為每個需要支持的平台(例如 Android、macOS 和/或 Windows)實現保存和檢視功能。 針對 Windows 平台,建立一個名為"SaveWindows.cs"的文件並實現局部方法 SaveAndView:
using Windows.Storage;
using Windows.Storage.Pickers;
using Windows.Storage.Streams;
using Windows.UI.Popups;
namespace PDF_Viewer
{
public partial class SaveService
{
public async partial void SaveAndView(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 FileSavePicker();
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 PDF_Viewer
{
public partial class SaveService
{
public async partial void SaveAndView(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 FileSavePicker();
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);
}
}
}
}
}
Imports Windows.Storage
Imports Windows.Storage.Pickers
Imports Windows.Storage.Streams
Imports Windows.UI.Popups
Namespace PDF_Viewer
Partial Public Class SaveService
Public Async Sub SaveAndView(ByVal filename As String, ByVal contentType As String, ByVal stream As MemoryStream)
Dim stFile As StorageFile
Dim extension As String = Path.GetExtension(filename)
'Gets process windows handle to open the dialog in application process.
Dim windowHandle As IntPtr = System.Diagnostics.Process.GetCurrentProcess().MainWindowHandle
If Not Windows.Foundation.Metadata.ApiInformation.IsTypePresent("Windows.Phone.UI.Input.HardwareButtons") Then
'Creates file save picker to save a file.
Dim savePicker As New FileSavePicker()
savePicker.DefaultFileExtension = ".pdf"
savePicker.SuggestedFileName = filename
'Saves the file as PDF file.
savePicker.FileTypeChoices.Add("PDF", New List(Of String)() From {".pdf"})
WinRT.Interop.InitializeWithWindow.Initialize(savePicker, windowHandle)
stFile = Await savePicker.PickSaveFileAsync()
Else
Dim local As StorageFolder = ApplicationData.Current.LocalFolder
stFile = Await local.CreateFileAsync(filename, CreationCollisionOption.ReplaceExisting)
End If
If stFile IsNot Nothing Then
Using zipStream As IRandomAccessStream = Await stFile.OpenAsync(FileAccessMode.ReadWrite)
'Writes compressed data from memory to file.
Using outstream As Stream = zipStream.AsStreamForWrite()
outstream.SetLength(0)
'Saves the stream as file.
Dim buffer() As Byte = stream.ToArray()
outstream.Write(buffer, 0, buffer.Length)
outstream.Flush()
End Using
End Using
'Create message dialog box.
Dim msgDialog As New MessageDialog("Do you want to view the document?", "File has been created successfully")
Dim yesCmd As New UICommand("Yes")
msgDialog.Commands.Add(yesCmd)
Dim noCmd As New UICommand("No")
msgDialog.Commands.Add(noCmd)
WinRT.Interop.InitializeWithWindow.Initialize(msgDialog, windowHandle)
'Showing a dialog box.
Dim cmd As IUICommand = Await msgDialog.ShowAsync()
If cmd.Label = yesCmd.Label Then
'Launch the saved file.
Await Windows.System.Launcher.LaunchFileAsync(stFile)
End If
End If
End Sub
End Class
End Namespace
對於 Android 和 macOS,您需要建立單獨的文件並實現類似的 SaveAndView。 您可以從這個MAUI PDF Viewer GitHub倉庫獲取一個可運行的範例。
步驟 4: PDF 功能的程式碼
現在,是撰寫 PDF 功能程式碼的時候了。 讓我們從 URL 到 PDF 功能開始。
URL 到 PDF 功能
為 URL 到 PDF 功能建立 UrlToPdf 函式。 在函式內,實例化 ChromePdfRenderer 物件並利用 RenderUrlAsPdf 函式將 URL 轉換為 PDF 文件。 RenderUrlAsPdf 函式從 Web 伺服器獲取 URL 的資料並處理以轉換為 PDF 文件。 在參數中,將 URL 項目控制中的文字傳入,建立 SaveService 類的物件,並使用 SaveAndView 函式。 在 SaveAndView 函式的參數中傳入生成的 PDF 文件的流。
SaveAndView 函式幫助以任何自定義路徑保存文件,並提供檢視 PDF 文件的選項。 最後,顯示一個資訊框告知建立 PDF 文件。如果使用者嘗試用空的輸入控件建立 PDF 文件,它將顯示一個帶有錯誤資訊和警告的警示框。
private void UrlToPdf(object sender, EventArgs e)
{
if (!string.IsNullOrEmpty(URL.Text))
{
var renderer = new IronPdf.ChromePdfRenderer();
var pdf = renderer.RenderUrlAsPdf(URL.Text.Trim());
SaveService saveService = new SaveService();
saveService.SaveAndView("URLtoPDF.pdf", "application/pdf", pdf.Stream);
DisplayAlert("Success", "PDF from URL Created!", "OK");
}
else
{
DisplayAlert("Error", "Field can't be empty! \nPlease enter URL!", "OK");
}
}
private void UrlToPdf(object sender, EventArgs e)
{
if (!string.IsNullOrEmpty(URL.Text))
{
var renderer = new IronPdf.ChromePdfRenderer();
var pdf = renderer.RenderUrlAsPdf(URL.Text.Trim());
SaveService saveService = new SaveService();
saveService.SaveAndView("URLtoPDF.pdf", "application/pdf", pdf.Stream);
DisplayAlert("Success", "PDF from URL Created!", "OK");
}
else
{
DisplayAlert("Error", "Field can't be empty! \nPlease enter URL!", "OK");
}
}
Imports Microsoft.VisualBasic
Private Sub UrlToPdf(ByVal sender As Object, ByVal e As EventArgs)
If Not String.IsNullOrEmpty(URL.Text) Then
Dim renderer = New IronPdf.ChromePdfRenderer()
Dim pdf = renderer.RenderUrlAsPdf(URL.Text.Trim())
Dim saveService As New SaveService()
saveService.SaveAndView("URLtoPDF.pdf", "application/pdf", pdf.Stream)
DisplayAlert("Success", "PDF from URL Created!", "OK")
Else
DisplayAlert("Error", "Field can't be empty! " & vbLf & "Please enter URL!", "OK")
End If
End Sub
HTML 到 PDF 功能
為將 HTML 轉換為 PDF 的功能,建立 HtmlToPdf 函式,並使用 RenderHtmlAsPdf 函式。 使用 Editor 控制項的文字並將其傳入 RenderHtmlAsPdf 函式的參數中。 類似於上述函式,使用 SaveAndView 函式以在保存後啟用檢視 PDF 文件的功能。
private void HtmlToPdf(object sender, EventArgs e)
{
if (!string.IsNullOrEmpty(HTML.Text))
{
var renderer = new IronPdf.ChromePdfRenderer();
var pdf = renderer.RenderHtmlAsPdf(HTML.Text);
SaveService saveService = new SaveService();
saveService.SaveAndView("IronPDF HTML string.pdf", "application/pdf", pdf.Stream);
DisplayAlert("Success", "PDF from HTML Created!", "OK");
}
else
{
DisplayAlert("Error", "Field can't be empty! \nPlease enter valid HTML!", "OK");
}
}
private void HtmlToPdf(object sender, EventArgs e)
{
if (!string.IsNullOrEmpty(HTML.Text))
{
var renderer = new IronPdf.ChromePdfRenderer();
var pdf = renderer.RenderHtmlAsPdf(HTML.Text);
SaveService saveService = new SaveService();
saveService.SaveAndView("IronPDF HTML string.pdf", "application/pdf", pdf.Stream);
DisplayAlert("Success", "PDF from HTML Created!", "OK");
}
else
{
DisplayAlert("Error", "Field can't be empty! \nPlease enter valid HTML!", "OK");
}
}
Imports Microsoft.VisualBasic
Private Sub HtmlToPdf(ByVal sender As Object, ByVal e As EventArgs)
If Not String.IsNullOrEmpty(HTML.Text) Then
Dim renderer = New IronPdf.ChromePdfRenderer()
Dim pdf = renderer.RenderHtmlAsPdf(HTML.Text)
Dim saveService As New SaveService()
saveService.SaveAndView("IronPDF HTML string.pdf", "application/pdf", pdf.Stream)
DisplayAlert("Success", "PDF from HTML Created!", "OK")
Else
DisplayAlert("Error", "Field can't be empty! " & vbLf & "Please enter valid HTML!", "OK")
End If
End Sub
HTML 文件到 PDF 功能
為將 HTML 文件轉換為 PDF 文件建立 FileToPdf 函式。 使用 RenderHtmlFileAsPdf 函式並將 HTML 文件路徑作為參數傳入。 它將所有 HTML 內容轉換為 PDF 並保存輸出文件。
private void FileToPdf(object sender, EventArgs e)
{
var renderer = new IronPdf.ChromePdfRenderer();
var pdf = renderer.RenderHtmlFileAsPdf(@"C:\Users\Administrator\Desktop\index.html");
SaveService saveService = new SaveService();
saveService.SaveAndView("HTML File to PDF.pdf", "application/pdf", pdf.Stream);
DisplayAlert("Success", "PDF from File Created!", "OK");
}
private void FileToPdf(object sender, EventArgs e)
{
var renderer = new IronPdf.ChromePdfRenderer();
var pdf = renderer.RenderHtmlFileAsPdf(@"C:\Users\Administrator\Desktop\index.html");
SaveService saveService = new SaveService();
saveService.SaveAndView("HTML File to PDF.pdf", "application/pdf", pdf.Stream);
DisplayAlert("Success", "PDF from File Created!", "OK");
}
Private Sub FileToPdf(ByVal sender As Object, ByVal e As EventArgs)
Dim renderer = New IronPdf.ChromePdfRenderer()
Dim pdf = renderer.RenderHtmlFileAsPdf("C:\Users\Administrator\Desktop\index.html")
Dim saveService As New SaveService()
saveService.SaveAndView("HTML File to PDF.pdf", "application/pdf", pdf.Stream)
DisplayAlert("Success", "PDF from File Created!", "OK")
End Sub
輸出
運行專案後,輸出將如下所示。
輸出
在此部分輸入 Microsoft 網站 URL 並點擊按鈕。
URL 到 PDF
建立 PDF 文件後,會顯示一個對話框以將文件保存到自定義的目的地。
保存文件
保存文件後,會顯示此彈出框並提供選擇 PDF 檢視器以查看 PDF 文件的選項。
PDF 檢視器彈出框
IronPDF 將 URL 卓越地轉換為 PDF。 它保留了所有顏色和圖片的原始形狀和格式。
PDF 檢視器彈出框
所有其他功能需要遵循相同的程式。 查看此Blazor 中的 IronPDF 部落格文章以了解有關 IronPDF 在 Blazor 中工作的更多資訊。
要了解如何將 MAUI 頁面作為 XAML 轉換為 PDF 文件,請參閱"如何在 MAUI 中將 XAML 轉換為 PDF"。
摘要
本教程在 .NET MAUI 應用程式中使用 IronPDF 建立 PDF 文件和 PDF 檢視器。 .NET MAUI 是一個很好的工具,可以使用單一程式碼庫來建立多平台應用程式。 IronPDF 有助於在任何 .NET 應用程式中輕鬆建立和自訂 PDF 文件。 IronPDF 完全與 .NET MAUI 平台相容。
IronPDF在開發中是免費的。 您可以獲取免費試用鍵來測試 IronPDF 在生產中的效果。 有關 IronPDF 及其功能的更多資訊,請存取IronPDF 官方網站。
常見問題
如何在.NET MAUI應用程式中整合PDF查看器?
要在.NET MAUI應用程式中整合PDF查看器,您可以使用IronPDF來處理PDF文件的渲染和查看。IronPDF允許您從URL、HTML字串和HTML文件渲染PDF,然後可以使用.NET MAUI中的各種PDF查看工具保存和顯示。
配置IronPDF for .NET MAUI需要哪些步驟?
配置IronPDF for .NET MAUI涉及通過Visual Studio中的NuGet包管理器安裝IronPDF包,配置您的專案以處理PDF渲染,並使用IronPDF的方法將HTML、URL或HTML文件轉換為PDF文件。
如何確保我的PDF佈局在.NET MAUI中得到保留?
IronPDF提供了在.NET MAUI應用程式中保留PDF佈局的強大功能。通過使用像RenderHtmlAsPdf或RenderUrlAsPdf這樣的方法,您可以將您的內容轉換為PDF,同時保持原始格式和佈局。
在.NET MAUI中查看PDF時,有哪些常見問題及如何解決?
在.NET MAUI中查看PDF時的常見問題包括特定於平台的渲染錯誤和文件存取權限。這些問題可以通過使用IronPDF的跨平台功能並確保在應用程式程式碼基礎中正確處理文件權限來解決。
我能否在.NET MAUI應用程式中將HTML內容轉換為PDF?
是的,您可以在.NET MAUI應用程式中使用IronPDF的RenderHtmlAsPdf方法將HTML內容轉換為PDF。這使您能夠有效地將HTML字串轉換為完全格式化的PDF文件。
如何在.NET MAUI中處理文件保存和查看?
在.NET MAUI中,您可以使用IronPDF生成PDF文件,然後使用特定於平台的文件處理API實現文件保存和查看。IronPDF支持將PDF文件保存到本地儲存,然後可以用PDF查看器打開。
IronPDF是否與.NET MAUI的所有目標平台相容?
是的,IronPDF與.NET MAUI針對的所有平台相容,包括Windows、macOS、iOS、Android和tvOS,提供無縫的PDF建立和查看體驗。
如何在全面部署之前測試我的.NET MAUI專案中的IronPDF?
您可以使用其免費開發授權測試您的.NET MAUI專案中的IronPDF。這允許您在決定購買完整的生產授權之前,將PDF功能整合和測試到您的應用程式中。
IronPDF是否支持.NET 10及其帶來的好處是什麼?
是的,IronPDF完全支持.NET 10。在使用.NET 10構建應用程式(包括MAUI、網頁、桌面和基於雲的應用程式)時,無需任何自定義方法即可開箱運行。使用.NET 10可以讓您存取最新的平台改進、性能增強和更新的API。




