使用IRON PDF在C#中生成PDF文件的5個步驟
使用 IronPDF 在 C# 中產生 PDF
C# 開發人員可以使用 IronPDF 從 HTML 產生 PDF。 本文將使用基於 .NET Framework 建立的 C# Windows Forms 應用程式來示範這個過程。
請依照下列步驟在 Visual Studio 2019 中建立專案。
步驟 1:建立 Visual Studio 項目
首先,開啟Visual Studio 2019 。
點擊"建立新項目"。
現在,從範本中選擇"Windows 窗體應用程式",然後按"下一步"。 將出現以下視窗:
! 建立新項目視窗
輸入項目名稱"使用 IronPDF 建立 PDF"。
點擊"創建"按鈕。 項目將按如下所示建立。
步驟 2. 使用 NuGet 安裝 IronPDF
- 首先,點擊選單列上的"工具"按鈕。 將會打開一個選單。 現在點選 NuGet 套件管理器選項。
- 將開啟另一個子選單。 現在點選名為"軟體包管理器控制台"的選項。
命令列下方會彈出一個新視窗。在該視窗中,輸入安裝IronPdf軟體包的命令。
Install-Package IronPdf
! 進入命令視窗
輸入指令後按回車鍵。 請確保您的電腦/筆記型電腦已連接到網路。 IronPdf套件將自動新增至您現有的專案中。
上面的螢幕截圖顯示軟體包已成功添加到您的專案中。
步驟 3. 設計使用者輸入表單
現在新增一個標籤,並將文字設定為"使用 IronPDF 從 HTML 建立 PDF"。
接下來,從工具箱中新增一個富文本框和一個按鈕。 將按鈕文字設定為"轉換"。
步驟 4. 編寫後端程式碼以建立 PDF 文件
雙擊"轉換"按鈕。 將開啟一個包含轉換按鈕點擊事件的程式碼視窗。
在.cs檔案的頂部新增導入 IronPDF 庫的程式碼。
首先,新增以下程式碼以使用 IronPDF 庫方法。
using IronPdf;using IronPdf;Imports IronPdf以下是btnConvert_Click事件的初始程式碼,目前為空:
private void btnConvert_Click(object sender, EventArgs e)
{
}private void btnConvert_Click(object sender, EventArgs e)
{
}Private Sub btnConvert_Click(ByVal sender As Object, ByVal e As EventArgs)
End Sub現在在按鈕點擊事件中編寫以下程式碼:
private void btnConvert_Click(object sender, EventArgs e)
{
// Declare an HtmlToPdf object
var HtmlLine = new HtmlToPdf();
// Get HTML text from the user
string strHtml = txtHtml.Text;
// Create SaveFileDialog to choose the save path for the PDF file
SaveFileDialog saveFileDialog = new SaveFileDialog
{
InitialDirectory = @"D:\",
Title = "Save PDF",
CheckPathExists = true,
DefaultExt = "pdf",
Filter = "pdf files (*.pdf)|*.pdf",
FilterIndex = 2,
RestoreDirectory = true
};
// If the user presses Save
if (saveFileDialog.ShowDialog() == DialogResult.OK)
{
// Get the file path from the user
string filePath = saveFileDialog.FileName;
// Convert HTML to PDF and save at the specified path
using var PDF = HtmlLine.RenderHtmlAsPdf(strHtml);
PDF.SaveAs(filePath);
// Clear the TextBox and show a message confirming the successful creation
txtHtml.Text = "";
MessageBox.Show("File created successfully.");
}
}private void btnConvert_Click(object sender, EventArgs e)
{
// Declare an HtmlToPdf object
var HtmlLine = new HtmlToPdf();
// Get HTML text from the user
string strHtml = txtHtml.Text;
// Create SaveFileDialog to choose the save path for the PDF file
SaveFileDialog saveFileDialog = new SaveFileDialog
{
InitialDirectory = @"D:\",
Title = "Save PDF",
CheckPathExists = true,
DefaultExt = "pdf",
Filter = "pdf files (*.pdf)|*.pdf",
FilterIndex = 2,
RestoreDirectory = true
};
// If the user presses Save
if (saveFileDialog.ShowDialog() == DialogResult.OK)
{
// Get the file path from the user
string filePath = saveFileDialog.FileName;
// Convert HTML to PDF and save at the specified path
using var PDF = HtmlLine.RenderHtmlAsPdf(strHtml);
PDF.SaveAs(filePath);
// Clear the TextBox and show a message confirming the successful creation
txtHtml.Text = "";
MessageBox.Show("File created successfully.");
}
}Private Sub btnConvert_Click(ByVal sender As Object, ByVal e As EventArgs)
' Declare an HtmlToPdf object
Dim HtmlLine = New HtmlToPdf()
' Get HTML text from the user
Dim strHtml As String = txtHtml.Text
' Create SaveFileDialog to choose the save path for the PDF file
Dim saveFileDialog As New SaveFileDialog With {
.InitialDirectory = "D:\",
.Title = "Save PDF",
.CheckPathExists = True,
.DefaultExt = "pdf",
.Filter = "pdf files (*.pdf)|*.pdf",
.FilterIndex = 2,
.RestoreDirectory = True
}
' If the user presses Save
If saveFileDialog.ShowDialog() = DialogResult.OK Then
' Get the file path from the user
Dim filePath As String = saveFileDialog.FileName
' Convert HTML to PDF and save at the specified path
Dim PDF = HtmlLine.RenderHtmlAsPdf(strHtml)
PDF.SaveAs(filePath)
' Clear the TextBox and show a message confirming the successful creation
txtHtml.Text = ""
MessageBox.Show("File created successfully.")
End If
End Sub解釋:
- 建立一個
HtmlToPdf對象,以利用 IronPDF 的轉換功能。 - HTML 輸入是從文字方塊中取得的。
- 使用
SaveFileDialog提示使用者指定產生的 PDF 檔案應儲存到哪裡。 - 如果使用者選擇檔案位置並按下"儲存"按鈕,則會取得檔案路徑。 然後使用
RenderHtmlAsPdf將 HTML 輸入渲染成 PDF,並儲存到選定的路徑。 - 儲存後,文字方塊將被清空,並顯示一個訊息方塊以確認 PDF 建立。
步驟 5:運行項目
運行專案後,您將看到以下畫面:
在富文本方塊中輸入 HTML 程式碼,例如:
<h1>A Simple PDF File</h1><br><h6>Heading 6</h6><h1>A Simple PDF File</h1><br><h6>Heading 6</h6>點擊"轉換"。 您將看到一個儲存檔案對話框。
點擊儲存按鈕後,檔案將以您指定的名稱和位置儲存到您指定的路徑。
輸出檔案
輸出的PDF文件將如下所示:
結論
上面的教學說明如何使用 IronPDF 庫從 HTML 建立 PDF。
欲了解更多信息,請訪問IronPDF 官方網站。 該庫還提供了其他功能,支援完全可自訂的 PDF 文件、以程式設計方式合併和分割文件,或只是查看演示各種功能的範例程式碼。
您可以使用30 天試用金鑰進行評估。 目前有一個非常優惠的活動,您可以用兩件 Iron Software 產品的價格購買五件。 請造訪IronPDF 許可資訊頁面,以了解更多許可資訊。
常見問題解答
如何在 C# 中將 HTML 轉換為 PDF?
您可以使用 IronPDF 的 RenderHtmlAsPdf 方法將 HTML 字串轉換為 PDF。只需建立一個 HtmlToPdf 物件,並呼叫該方法即可將 HTML 渲染成 PDF 文件。
設定 Visual Studio 專案以產生 PDF 需要哪些步驟?
首先開啟 Visual Studio 2019,選擇「建立新專案」,選擇「Windows Forms App」,並設定專案名稱。然後,透過 NuGet 安裝 IronPDF,開始整合 PDF 生成功能。
如何在 Visual Studio 中安裝 PDF 生成函式庫?
您可以導航到 Visual Studio 中的套件管理員控制台,並執行指令來安裝 IronPdf:Install-Package IronPdf。
生成 PDF 的表單應包含哪些元件?
包括一個用於引導的標籤、一個用於 HTML 輸入的富文本框,以及一個用於用戶點擊以產生 PDF 的標有「轉換」的按鈕。
如何實作 PDF 檔案建立的後端程式碼?
使用 IronPDF 來宣告一個 HtmlToPdf 物件。從文字方塊擷取 HTML 輸入,提示使用者儲存 PDF,並使用 RenderHtmlAsPdf 方法渲染 HTML。
PDF 函式庫中的 HtmlToPdf 物件有何功能?
IronPDF 中的 HtmlToPdf 物件用於使用庫的全面轉換功能將 HTML 內容轉換為 PDF 文件。
如何驗證我的 PDF 生成專案是否正常運作?
在 Visual Studio 中執行專案,在 RichTextBox 中輸入 HTML,然後按一下「轉換」。然後,使用 SaveFileDialog 選擇 PDF 檔案的位置,確保轉換成功完成。
PDF 函式庫提供哪些進階功能?
IronPDF 允許建立完全客製化的 PDF,以及以程式化的方式合併與分割檔案。該程式庫還提供各種功能的範例程式碼。
在購買之前,我可以試用 PDF 函式庫嗎?
是的,IronPDF 的 30 天試用金鑰可在其官方網站取得,讓您在承諾購買之前先探索其功能。
在哪裡可以找到 PDF 函式庫的授權詳細資訊?
IronPDF 的詳細授權資訊可在其網站上的 IronPDF 授權資訊頁面找到,包括選項和目前的優惠。
IronPDF 是否與 .NET 10 相容?
是的 - IronPDF 已經支援所有現代 .NET 版本,並符合即將於 2025 年 11 月發行的 .NET 10 版本。IronPDF 開箱即可使用 .NET 10 專案,不需要額外的變通。(ironpdf.com/blog/using-ironpdf/5-steps-to-generate-a-pdf-file-in-c-sharp/)






