用于.NET轉換器的PDF庫
有了正確的指導,使用 C# .NET 程式庫建立 PDF 檔案既簡單又有效率。 使用 IronPDF,我們可以根據應用程式的需求,以簡單的方式建立和編輯 PDF 功能。 本教學範例展示如何在專案中有效使用該軟體,只需單擊一個按鈕即可建立 PDF!
步驟 1
1. 安裝 C# PDF 函式庫 .NET
進入圖書館主要有兩種方式:
- Download and unpack the [IronPDF Package](https://ironpdf.com/packages/IronPdf.zip) DLL file
- Navigate to [NuGet](https://www.nuget.org/packages/IronPdf) and install the package via Visual Studio.
# Use the NuGet package manager to install IronPDF
nuget install IronPdf# Use the NuGet package manager to install IronPDF
nuget install IronPdf操作指南
2. 使用 PDF .NET 函式庫





現在我們有了這款軟體,我們可以產生 PDF 文件,調整設置,添加自訂文字和圖像,並根據專案要求對PDF 文件進行處理。
下面的程式碼中使用了一個 C# 窗體,簡單地示範如何使用 C# .NET 程式庫建立 PDF。 在這個例子中,我們有一個文本框來輸入我們自己的文本,然後點擊一個按鈕即可生成 PDF 文件。 ChromePdfRenderer類別提供了一種最簡單的方法,可以從不同的來源產生 PDF 文件,包括HTML 字串、 Web URL或另一個渲染器下的 doc 文件。
// C# Program to create PDF from TextBox input using IronPDF
using IronPdf;
using System.Windows.Forms;
namespace readpdf
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
// Event handler for the button click
private void button1_Click(object sender, System.EventArgs e)
{
// Create a ChromePdfRenderer object to convert HTML to PDF
var HtmlLine = new ChromePdfRenderer();
// Retrieve the text from the TextBox
string text = textBox1.Text;
// Render the HTML as a PDF, wrapping the text in an <h1> tag
using var pdf = HtmlLine.RenderHtmlAsPdf("<h1>" + text + "</h1>");
// Save the PDF to a file called "custom.pdf"
pdf.SaveAs("custom.pdf");
// Show a confirmation message to the user
MessageBox.Show("Done!");
}
}
}// C# Program to create PDF from TextBox input using IronPDF
using IronPdf;
using System.Windows.Forms;
namespace readpdf
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
// Event handler for the button click
private void button1_Click(object sender, System.EventArgs e)
{
// Create a ChromePdfRenderer object to convert HTML to PDF
var HtmlLine = new ChromePdfRenderer();
// Retrieve the text from the TextBox
string text = textBox1.Text;
// Render the HTML as a PDF, wrapping the text in an <h1> tag
using var pdf = HtmlLine.RenderHtmlAsPdf("<h1>" + text + "</h1>");
// Save the PDF to a file called "custom.pdf"
pdf.SaveAs("custom.pdf");
// Show a confirmation message to the user
MessageBox.Show("Done!");
}
}
}' C# Program to create PDF from TextBox input using IronPDF
Imports IronPdf
Imports System.Windows.Forms
Namespace readpdf
Partial Public Class Form1
Inherits Form
Public Sub New()
InitializeComponent()
End Sub
' Event handler for the button click
Private Sub button1_Click(ByVal sender As Object, ByVal e As System.EventArgs)
' Create a ChromePdfRenderer object to convert HTML to PDF
Dim HtmlLine = New ChromePdfRenderer()
' Retrieve the text from the TextBox
'INSTANT VB NOTE: The variable text was renamed since Visual Basic does not handle local variables named the same as class members well:
Dim text_Conflict As String = textBox1.Text
' Render the HTML as a PDF, wrapping the text in an <h1> tag
Dim pdf = HtmlLine.RenderHtmlAsPdf("<h1>" & text_Conflict & "</h1>")
' Save the PDF to a file called "custom.pdf"
pdf.SaveAs("custom.pdf")
' Show a confirmation message to the user
MessageBox.Show("Done!")
End Sub
End Class
End Namespace3. C# 表單轉 PDF
我們使用 C# Windows Forms 應用程式向您展示了帶有自訂文字的完美輸出效果。 只需按一下,文字方塊中的文字即可轉換為自訂 PDF。 這只需要一行程式碼函數,而且很容易理解。
圖書館快速訪問
常見問題解答
如何在 C# 專案中安裝 PDF 函式庫?
您可以直接下載套件 DLL 檔案或使用 NuGet 透過 Visual Studio 安裝 PDF 函式庫,例如 IronPDF。
如何使用 PDF 函式庫從 C# 建立 PDF?
使用 IronPDF 之類的 PDF 函式庫,您可以利用 ChromePdfRenderer 類來建立 PDF,以最少的程式碼將 HTML 字串或 URL 轉換成 PDF。
我可以使用 PDF 函式庫將 C# 表單轉換成 PDF 嗎?
是的,通過使用 IronPDF,您可以將 C# 表單轉換為 PDF。這包括擷取表單的資料,並使用函式庫的渲染功能將其渲染為 PDF。
使用 PDF 函式庫產生 PDF 的最簡單方法是什麼?
使用 IronPDF 生成 PDF 的最簡單方法是使用 ChromePdfRenderer 物件直接將 HTML 內容渲染成 PDF。
如何使用 PDF 函式庫將自訂文字和圖片新增至 PDF?
您可以在使用 IronPDF 的功能將 HTML 內容渲染為 PDF 之前,先透過操作 HTML 內容,將自定義的文字和圖片添加到 PDF 中。
是否可以使用 PDF 函式庫編輯現有的 PDF?
是的,IronPDF 提供操作和編輯現有 PDF 的功能,讓您可以根據需要更新內容。
如何使用 PDF 函式庫將 URL 直接轉換成 PDF?
IronPdf 允許您使用 ChromePdfRenderer 物件直接將 Web URL 轉換為 PDF,簡化了轉換過程。
適用於 .NET 的 PDF 函式庫有哪些主要功能?
IronPDF 之類的 PDF 函式庫提供 PDF 建立、編輯、從 HTML 轉換,以及新增自訂文字和圖片等功能,使其成為 .NET 開發人員的多用途工具。
我可以使用 PDF 函式庫自訂 PDF 設定嗎?
是的,IronPDF 允許您自訂各種 PDF 設定,包括頁面大小、方向和頁邊空白,以符合您特定的專案需求。
在 C# 中使用 PDF 函式庫時,如何排除故障?
有關疑難排解,您可以參考 IronPDF 提供的說明文件和資源,或參閱社群論壇,尋找常見問題的解決方案。
IronPDF 是否與 .NET 10 相容,以及 .NET 10 帶來哪些好處?
是的,IronPDF 與 .NET 10 完全相容。它支持 .NET 10 中引入的運行時和語言增強功能,包括改進的記憶體使用、陣列介面方法去虛擬化等性能增益,以及 PDF 生成和操作中的開銷減少。








