跳過到頁腳內容
使用IRONPDF

用于.NET轉換器的PDF庫

在 C# .NET 庫中製作 PDF 非常簡單且高效,只要有正確的指南。 使用 IronPDF,我們可以根據應用程式要求以簡單的方式創建和編輯 PDF 功能。 這個教程示例展示了如何在專案中高效使用軟體並只需單擊一個按鈕即可創建 PDF!

class="learnn-how-section">
class="row">
class="col-sm-6">

PDF .NET 庫

  • 安裝 IronPDF C# .NET 庫
  • 一行代碼在 C# 中創建 PDF
  • 一鍵將表單轉換為 PDF
class="col-sm-6">
class="download-card"> Pdf In Csharp No Button related to 用于.NET轉換器的PDF庫

class="tutorial-segment-title">步驟 1

1. 安裝 C# PDF 庫 .NET

訪問該庫的兩種主要方式是:

  1. 下載並解壓縮 [IronPDF Package](https://ironpdf.com/packages/IronPdf.zip) DLL 文件
  2. 導覽到 [NuGet](https://www.nuget.org/packages/IronPdf) 並通過 Visual Studio 安裝該軟體包。


# Use the NuGet package manager to install IronPDF
nuget install IronPdf
# Use the NuGet package manager to install IronPDF
nuget install IronPdf
SHELL


class="tutorial-segment-title">操作教學

2. 使用 PDF .NET 庫

PDF Library for .NET Converter, Figure 1: C# Icon
PDF Library for .NET Converter, Figure 2: Converter Icon
PDF Library for .NET Converter, Figure 3: pen and ruler Icon
PDF Library for .NET Converter, Figure 2: Converter Icon
PDF Library for .NET Converter, Figure 4: Adobe Icon

Now that we have the software, we can generate PDFs, adjust settings, add custom text and images, and manipulate the PDFs to our project requirements.

在下面的程式碼中,我們使用了 C# 表單來簡單展示如何使用 C# .NET 庫創建 PDF。 在此範例中,我們有一個 TextBox 用於輸入我們自己的文字,然後只需按一下按鈕就能製作 PDF。 The class ChromePdfRenderer offers the simplest possible way to generate PDF files from different sources including an HTML string, web URLs, or doc files under another renderer.

// 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 Namespace
$vbLabelText   $csharpLabel

3. C# 表單轉 PDF

我們使用了一個 C# Windows Forms App 向您展示具有自訂文本的完美輸出。 只需單擊一下,TextBox 中的文字就能轉換為自訂 PDF。 這僅需一行代碼功能且易於理解。

class="row">
class="col-md-6">

~ C# 表單 ~

class="center-image-wrapper"> PDF Library for .NET Converter, Figure 4: Windows Forms App to custom PDF file
class="col-md-6">

~ PDF ~

class="center-image-wrapper"> PDF Library for .NET Converter, Figure 5: Pdf File output


class="tutorial-segment-title">快速訪問函式庫

class="tutorial-section">
class="row">
class="col-sm-4">
class="tutorial-image"> Documentation related to 3. C# 表單轉 PDF
class="col-sm-8">

分享 API 參考

閱讀和分享 API 參考,獲取您在 .NET 專案中處理 PDF 所需的所有功能。

IronPDF 的 API 參考

常見問題解答

如何在我的 C# 專案中安裝 PDF 庫?

你可以透過直接下載軟體包 DLL 檔案或使用 NuGet 透過 Visual Studio 安裝 PDF 庫(例如 IronPDF)。

如何使用 PDF 庫從 C# 建立 PDF?

使用 IronPDF 等 PDF 庫,您可以利用ChromePdfRenderer類別將 HTML 字串或 URL 轉換為 PDF,從而以最少的程式碼建立 PDF。

我可以使用 PDF 庫將 C# 表單轉換為 PDF 嗎?

是的,使用 IronPDF,您可以將 C# 表單轉換為 PDF。這包括捕獲表單資料並使用庫的渲染功能將其渲染為 PDF。

使用 PDF 庫產生 PDF 的最簡單方法是什麼?

使用 IronPDF 產生 PDF 最簡單的方法是使用ChromePdfRenderer物件直接將 HTML 內容渲染成 PDF。

如何使用 PDF 庫為 PDF 添加自訂文字和圖像?

您可以使用 IronPDF 的功能,在將 HTML 內容渲染成 PDF 之前對其進行操作,從而為 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 生成和操作的開銷。

Curtis Chau
技術作家

Curtis Chau 擁有卡爾頓大學計算機科學學士學位,專注於前端開發,擅長於 Node.js、TypeScript、JavaScript 和 React。Curtis 熱衷於創建直觀且美觀的用戶界面,喜歡使用現代框架並打造結構良好、視覺吸引人的手冊。

除了開發之外,Curtis 對物聯網 (IoT) 有著濃厚的興趣,探索將硬體和軟體結合的創新方式。在閒暇時間,他喜愛遊戲並構建 Discord 機器人,結合科技與創意的樂趣。