使用IRON SUITE

C#中創建PDF - 使用IronPDF的完整指南

介紹

從 HTML 內容建立 PDF 文件已成為各種應用程式的必要需求,從產生發票到存檔 Web 內容,無所不包。 IronPDF是一個功能強大且用途廣泛的 .NET 程式庫,它簡化了將 HTML 轉換為 PDF 的過程,使開發人員能夠輕鬆地在 C# 中建立高品質的 PDF 文件。 在本文中,我們將探討IronPDF的功能,並提供使用該軟體建立 PDF 的逐步指南。

如何使用 IronPDF 庫在 C# 中建立 PDF 文件

  1. 建立一個新的 Visual Studio 項目
  2. 從 NuGet 套件管理器新增 IronPDF 庫
  3. 建立一個簡單的 PDF 檔案。
  4. 修改 PDF 檔案。
  5. 新增頁首和頁尾
  6. 包含外部樣式表和腳本
  7. 包括影像和點陣圖。
  8. HTML 文件轉 PDF。
  9. PDF轉換的URL。

IronPDF 簡介

IronPDF是一個 .NET 程式庫,它允許開發人員輕鬆地將 HTML 轉換為 PDF。 它支援多種功能,包括 CSS、JavaScript,甚至嵌入式圖片。 使用IronPDF ,您可以建立與 HTML 網頁外觀完全相同的 PDF 文件,確保格式之間的無縫過渡。 該程式庫對於需要動態生成 PDF 文件的 Web 應用程式尤其有用。

IronPDF允許開發人員將 PDF 功能無縫整合到 .NET 應用程式中,而無需手動管理 PDF 文件結構。 IronPDF 利用基於 Chrome 的渲染引擎將 HTML 頁面(包括複雜的 CSS、JavaScript 和圖像)轉換為結構良好的 PDF 文件。 它可用於產生報告、發票、電子書或任何需要以 PDF 格式呈現的文件。

IronPDF 功能多樣,不僅可以渲染 PDF,還提供各種 PDF 操作選項,例如編輯、表單處理、加密等等。

IronPDF C# 建立 PDF 檔案的主要功能

  1. HTML 轉 PDF

    • HTML 渲染IronPDF只需幾行程式碼即可將 HTML 文件或網頁(包括帶有 CSS、圖像和 JavaScript 的 HTML)直接轉換為 PDF 文檔,使其成為從動態 Web 內容生成 PDF 的理想選擇。

    *支援現代 HTML/CSS :IronPDF 可處理現代 HTML5、CSS3 和 JavaScript,確保您的 Web 內容能夠準確地渲染為 PDF,並保留佈局、字體和互動元素。

    *進階渲染:它使用 Chrome 的渲染引擎(透過 Chromium)產生精確、高品質的 PDF,使其比許多其他 HTML 轉 PDF 庫更可靠。

    *網站 URL 轉 PDFIronPDF可以接受網站的 URL 字串作為輸入,並將其轉換為 PDF。

2.自訂頁首和頁尾

  • IronPDF允許開發人員在 PDF 文件中添加自訂頁首和頁腳,其中可以包含動態內容,例如頁碼、文件標題或自訂文字。

  • 頁首和頁尾可以新增至單一頁面,也可以作為整個文件中的統一元素。
  1. PDF 中對 JavaScript 的支持

    • IronPDF 允許在生成 PDF 之前在 HTML 內容中執行 JavaScript。 這樣就可以在產生的 PDF 中實現動態內容渲染,例如表單計算或互動功能。

4.編輯現有PDF文件

IronPDF提供編輯現有 PDF 檔案的功能。 您可以修改現有 PDF 檔案中的文字、圖像,並新增註釋。 此功能可用於在文件中新增浮水印、新增簽名或更新 PDF 文件內的內容。

  • 文字擷取和修改功能可讓您以程式設計方式操作 PDF 文件中的內容。

5.合併和拆分 PDF

  • IronPDF可讓您將多個 PDF 文件合併到一個文件中,或將一個大的 PDF 文件分割成更小的文件,非常適合需要合併文件或將其分解成更易於管理的部分的工作流程。

6.對互動表單的支持

  • 您可以使用 IronPDF 建立、填寫和操作 PDF 表單。 它為互動式表單(如文字欄位、複選框和單選按鈕)提供全面支持,並允許您預先填入表單資料。

7.頁面操作

  • IronPDF提供了操作 PDF 文件中單一頁面的方法,例如旋轉頁面、刪除頁面或重新排列頁面。

8.安全性和加密

  • IronPDF可讓您對 PDF 套用密碼保護和加密,確保您的文件安全無虞。 您可以設定使用者權限,以防止列印、複製或編輯 PDF 檔案。

9.水印和品牌推廣

使用 IronPDF 可以輕鬆地為 PDF 文件添加浮水印。 您可以將文字或圖片疊加為浮水印到頁面上,從而防止未經授權的複製或散佈您的文件。

10.文字和圖像提取

* [IronPDF](https://ironpdf.com)允許從 PDF 文件中提取文字和圖像,使開發人員能夠提取資料進行處理或重複使用。
  1. Unicode 和多語言支持

    • IronPDF具有強大的 Unicode 支持,這意味著它可以處理國際字元和字體,使其成為生成多種語言 PDF 的理想選擇。

12.性能優化

* [IronPDF](https://ironpdf.com)針對效能進行了最佳化,能夠處理大型 PDF 文件和大量請求。 即使處理大型資料集或影像,該函式庫也能確保快速且有效率地產生 PDF 檔案。
  1. API 和開發者友善工具

    • IronPDF配備了全面且易於使用的 API。 開發者可以透過簡單的方法呼叫快速上手,執行複雜的任務。

14.跨平台支持

* [IronPDF](https://ironpdf.com)具有跨平台相容性,這意味著它可以在 Windows 和 Linux 環境下使用,讓您在不同的作業系統上產生和操作 PDF 檔案。

步驟 1:建立一個新的 Visual Studio 項目

現在讓我們開始建立一個新項目,打開 Visual Studio 並建立一個新項目,如下所示。

C# 建立 PDF - IronPDF 使用完整指南:圖 1

選擇"建立控制台應用程式"。

C# 建立 PDF - IronPDF 使用完整指南:圖 2

提供專案名稱和地點。

C# 建立 PDF - IronPDF 使用完整指南:圖 3

選擇 .NET 版本

C# 建立 PDF - IronPDF 使用完整指南:圖 4

建立一個新專案。

步驟 2:從 NuGet 套件管理器新增 IronPDF 庫

在 Visual Studio 控制台應用程式中使用 NuGet 套件管理器,您可以使用下列命令新增 IronPDF NuGet 庫。

此外,也可以使用 Visual Studio 套件管理器安裝 IronPDF。

C# 建立 PDF - IronPDF 使用完整指南:圖 5

步驟 3:建立一個簡單的 PDF 檔案。

使用 IronPDF 庫輕鬆產生 PDF 文件。 現在讓我們從一個簡單的空白PDF檔案開始。

using IronPdf;

class Program
{
    static void Main()
    {
        // Set your IronPDF license key
        IronPdf.License.LicenseKey = "your key";

        // Create a new PDF document with specific dimensions (270x270 points)
        PdfDocument pdf = new PdfDocument(270, 270);

        // Save the blank PDF document to disk
        pdf.SaveAs("simple.pdf");
    }
}
using IronPdf;

class Program
{
    static void Main()
    {
        // Set your IronPDF license key
        IronPdf.License.LicenseKey = "your key";

        // Create a new PDF document with specific dimensions (270x270 points)
        PdfDocument pdf = new PdfDocument(270, 270);

        // Save the blank PDF document to disk
        pdf.SaveAs("simple.pdf");
    }
}
Imports IronPdf

Friend Class Program
	Shared Sub Main()
		' Set your IronPDF license key
		IronPdf.License.LicenseKey = "your key"

		' Create a new PDF document with specific dimensions (270x270 points)
		Dim pdf As New PdfDocument(270, 270)

		' Save the blank PDF document to disk
		pdf.SaveAs("simple.pdf")
	End Sub
End Class
$vbLabelText   $csharpLabel

說明程式碼

本程式示範如何使用 IronPDF 庫在 C# 中建立 PDF 文件。 以下是程式碼中發生的情況:

1.許可證密鑰設定:程式首先設定 IronPDF 庫的許可證密鑰。 要使用該程式庫的全部功能,必須使用許可證金鑰,因為許可證金鑰可確保您可以存取全部功能(而不是僅限於試用版)。

2.建立 PDF 文件:程式隨後建立一個大小為270x270 磅的新PDF 文件。 點是印刷中的計量單位,相當於 1/72 英吋。 因此,這將創建一個邊長約為 3.75 英吋的正方形文件。

3.儲存 PDF :建立空白文件後,程式會將 PDF 檔案儲存為"simple.pdf" 。 由於文件中沒有添加任何內容,因此輸出結果將是一個完全空白(黑色)的PDF 文件。

步驟 4:修改 PDF 文件

using IronPdf;

class Program
{
    static void Main()
    {
        // Set your IronPDF license key
        IronPdf.License.LicenseKey = "your key";

        // Load the existing PDF document
        var pdf = new PdfDocument("simple.pdf");

        // Create a renderer for converting HTML to PDF
        var renderer = new ChromePdfRenderer();

        // Render HTML content as a PDF
        var pagePdf = renderer.RenderHtmlAsPdf("<h1>Awesome IronPDF Library</h1>");

        // Prepend the rendered page as the first page of the existing PDF
        pdf.PrependPdf(pagePdf);

        // Save the modified PDF with a new filename
        pdf.SaveAs("simple_WithTitle.pdf");
    }
}
using IronPdf;

class Program
{
    static void Main()
    {
        // Set your IronPDF license key
        IronPdf.License.LicenseKey = "your key";

        // Load the existing PDF document
        var pdf = new PdfDocument("simple.pdf");

        // Create a renderer for converting HTML to PDF
        var renderer = new ChromePdfRenderer();

        // Render HTML content as a PDF
        var pagePdf = renderer.RenderHtmlAsPdf("<h1>Awesome IronPDF Library</h1>");

        // Prepend the rendered page as the first page of the existing PDF
        pdf.PrependPdf(pagePdf);

        // Save the modified PDF with a new filename
        pdf.SaveAs("simple_WithTitle.pdf");
    }
}
Imports IronPdf

Friend Class Program
	Shared Sub Main()
		' Set your IronPDF license key
		IronPdf.License.LicenseKey = "your key"

		' Load the existing PDF document
		Dim pdf = New PdfDocument("simple.pdf")

		' Create a renderer for converting HTML to PDF
		Dim renderer = New ChromePdfRenderer()

		' Render HTML content as a PDF
		Dim pagePdf = renderer.RenderHtmlAsPdf("<h1>Awesome IronPDF Library</h1>")

		' Prepend the rendered page as the first page of the existing PDF
		pdf.PrependPdf(pagePdf)

		' Save the modified PDF with a new filename
		pdf.SaveAs("simple_WithTitle.pdf")
	End Sub
End Class
$vbLabelText   $csharpLabel

程式碼解釋

1.許可證密鑰設定:設定 IronPDF 許可證密鑰以啟用庫的全部功能。

2.載入現有 PDF :將名為"simple.pdf"的現有 PDF 檔案載入到新的 PdfDocument 物件中。 這是將會新增封面頁的PDF檔案。

3.將 HTML 渲染為 PDF :使用 ChromePdfRenderer 物件將 HTML 內容渲染為 PDF。 在這個例子中,HTML 內容很簡單。<h1>使用RenderHtmlAsPdf方法將文字"Awesome IronPDF Library"轉換為封面 PDF,並新增標籤。

4.新增封面頁:使用PrependPdf方法將封面頁 PDF(由 HTML 產生)插入到現有 PDF 文件的開頭。

5.儲存修改後的PDF :將修改過的PDF(附有新的封面)儲存為"simple_WithTitle.pdf"。 "simple.pdf"中的原始內容得以保留,但現在它以新添加的標題頁開頭。

輸出 PDF

C# 建立 PDF - IronPDF 使用完整指南:圖 6

步驟五:新增頁首和頁尾

using IronPdf;

class Program
{
    static void Main()
    {
        // Set your IronPDF license key
        IronPdf.License.LicenseKey = "your key";

        // Create a new HtmlToPdf renderer
        var renderer = new ChromePdfRenderer();

        // Define the HTML content
        string htmlContent = "<html><body><h1>IronPDF: An Awesome PDF Generation Library</h1><h2>Report</h2><p>This is a sample report.</p></body></html>";

        // Define headers and footers as HTML
        string headerHtml = "<div style='text-align: right;'>Page {page} of {total-pages}</div>";
        string footerHtml = "<div style='text-align: center;'>Confidential</div>";

        // Convert the HTML content to a PDF document
        var pdfDocument = renderer.RenderHtmlAsPdf(htmlContent);

        // Add headers and footers to the PDF document
        pdfDocument.AddHtmlHeadersAndFooters(new ChromePdfRenderOptions
        {
            HtmlHeader = new HtmlHeaderFooter() { HtmlFragment = headerHtml },
            HtmlFooter = new HtmlHeaderFooter() { HtmlFragment = footerHtml }
        });

        // Save the PDF document with headers and footers
        pdfDocument.SaveAs("report.pdf");
    }
}
using IronPdf;

class Program
{
    static void Main()
    {
        // Set your IronPDF license key
        IronPdf.License.LicenseKey = "your key";

        // Create a new HtmlToPdf renderer
        var renderer = new ChromePdfRenderer();

        // Define the HTML content
        string htmlContent = "<html><body><h1>IronPDF: An Awesome PDF Generation Library</h1><h2>Report</h2><p>This is a sample report.</p></body></html>";

        // Define headers and footers as HTML
        string headerHtml = "<div style='text-align: right;'>Page {page} of {total-pages}</div>";
        string footerHtml = "<div style='text-align: center;'>Confidential</div>";

        // Convert the HTML content to a PDF document
        var pdfDocument = renderer.RenderHtmlAsPdf(htmlContent);

        // Add headers and footers to the PDF document
        pdfDocument.AddHtmlHeadersAndFooters(new ChromePdfRenderOptions
        {
            HtmlHeader = new HtmlHeaderFooter() { HtmlFragment = headerHtml },
            HtmlFooter = new HtmlHeaderFooter() { HtmlFragment = footerHtml }
        });

        // Save the PDF document with headers and footers
        pdfDocument.SaveAs("report.pdf");
    }
}
Imports IronPdf

Friend Class Program
	Shared Sub Main()
		' Set your IronPDF license key
		IronPdf.License.LicenseKey = "your key"

		' Create a new HtmlToPdf renderer
		Dim renderer = New ChromePdfRenderer()

		' Define the HTML content
		Dim htmlContent As String = "<html><body><h1>IronPDF: An Awesome PDF Generation Library</h1><h2>Report</h2><p>This is a sample report.</p></body></html>"

		' Define headers and footers as HTML
		Dim headerHtml As String = "<div style='text-align: right;'>Page {page} of {total-pages}</div>"
		Dim footerHtml As String = "<div style='text-align: center;'>Confidential</div>"

		' Convert the HTML content to a PDF document
		Dim pdfDocument = renderer.RenderHtmlAsPdf(htmlContent)

		' Add headers and footers to the PDF document
		pdfDocument.AddHtmlHeadersAndFooters(New ChromePdfRenderOptions With {
			.HtmlHeader = New HtmlHeaderFooter() With {.HtmlFragment = headerHtml},
			.HtmlFooter = New HtmlHeaderFooter() With {.HtmlFragment = footerHtml}
		})

		' Save the PDF document with headers and footers
		pdfDocument.SaveAs("report.pdf")
	End Sub
End Class
$vbLabelText   $csharpLabel

程式碼片段解釋

1.許可證密鑰設定:設定 IronPDF 許可證密鑰以啟用全部功能。

2.建立 PDF 渲染器:建立 ChromePdfRenderer 實例,將 HTML 內容渲染成 PDF 格式。

3.定義 HTML 內容:建立一個簡單的 HTML 字串,包括標題、標題和段落。

4.定義頁首和頁尾的HTML程式碼*頁首*包含頁碼,格式為"第 {page} 頁,共 {total-pages} 頁",右對齊。 頁腳**包含居中對齊的"機密"字樣。

  1. HTML 轉 PDF 轉換:使用 RenderHtmlAsPdf 方法將 HTML 內容轉換為 PDF 文件。

6.新增頁首和頁尾:使用 AddHtmlHeadersAndFooters 方法將定義的頁首和頁尾新增至 PDF 中。

7.儲存 PDF :將產生的 PDF 儲存到磁碟上名為"report.pdf"的檔案中。

輸出 PDF

C# 建立 PDF - IronPDF 使用完整指南:圖 7

步驟 6:包含外部樣式表和腳本

using IronPdf;

class Program
{
    static void Main()
    {
        // Set your IronPDF license key
        IronPdf.License.LicenseKey = "your key";

        // Define the HTML content with links to external CSS and JS files
        string htmlContent = @"
        <html>
            <head>
                <link rel='stylesheet' type='text/css' href='styles.css'>
                <script src='script.js'></script>
            </head>
            <body>
                <h1>IronPDF: An Awesome PDF Generation Library</h1>
                <h2>Styled Content</h2>
                <p id='dynamic-text'>This content is styled using an external CSS file and JavaScript.</p>
            </body>
        </html>";

        // Create a PDF renderer instance
        var renderer = new ChromePdfRenderer();

        // Render HTML content to a PDF
        var pdfDocument = renderer.RenderHtmlAsPdf(htmlContent);

        // Save the PDF to disk
        pdfDocument.SaveAs("awesomeIronPDF_styled_content.pdf");
    }
}
using IronPdf;

class Program
{
    static void Main()
    {
        // Set your IronPDF license key
        IronPdf.License.LicenseKey = "your key";

        // Define the HTML content with links to external CSS and JS files
        string htmlContent = @"
        <html>
            <head>
                <link rel='stylesheet' type='text/css' href='styles.css'>
                <script src='script.js'></script>
            </head>
            <body>
                <h1>IronPDF: An Awesome PDF Generation Library</h1>
                <h2>Styled Content</h2>
                <p id='dynamic-text'>This content is styled using an external CSS file and JavaScript.</p>
            </body>
        </html>";

        // Create a PDF renderer instance
        var renderer = new ChromePdfRenderer();

        // Render HTML content to a PDF
        var pdfDocument = renderer.RenderHtmlAsPdf(htmlContent);

        // Save the PDF to disk
        pdfDocument.SaveAs("awesomeIronPDF_styled_content.pdf");
    }
}
Imports IronPdf

Friend Class Program
	Shared Sub Main()
		' Set your IronPDF license key
		IronPdf.License.LicenseKey = "your key"

		' Define the HTML content with links to external CSS and JS files
		Dim htmlContent As String = "
        <html>
            <head>
                <link rel='stylesheet' type='text/css' href='styles.css'>
                <script src='script.js'></script>
            </head>
            <body>
                <h1>IronPDF: An Awesome PDF Generation Library</h1>
                <h2>Styled Content</h2>
                <p id='dynamic-text'>This content is styled using an external CSS file and JavaScript.</p>
            </body>
        </html>"

		' Create a PDF renderer instance
		Dim renderer = New ChromePdfRenderer()

		' Render HTML content to a PDF
		Dim pdfDocument = renderer.RenderHtmlAsPdf(htmlContent)

		' Save the PDF to disk
		pdfDocument.SaveAs("awesomeIronPDF_styled_content.pdf")
	End Sub
End Class
$vbLabelText   $csharpLabel

styles.css

/* styles.css */
body {
    font-family: Arial, sans-serif;
    margin: 20px;
}
h1 {
    color: #007BFF;
}
p {
    font-size: 14px;
    line-height: 1.6;
}

script.js

// script.js
document.addEventListener('DOMContentLoaded', function() {
    var dynamicText = document.getElementById('dynamic-text');
    dynamicText.textContent = "This content has been modified by JavaScript.";
});
// script.js
document.addEventListener('DOMContentLoaded', function() {
    var dynamicText = document.getElementById('dynamic-text');
    dynamicText.textContent = "This content has been modified by JavaScript.";
});
JAVASCRIPT

程式碼解釋

此程式碼示範如何在 C# 中使用 IronPDF 從包含指向外部 CSS 和 JavaScript 檔案的連結的 HTML 內容產生 PDF。

1.許可證密鑰設定:設定 IronPDF 許可證密鑰以啟用全部功能。

2.使用外部資源定義 HTML 內容

  • 使用外部 CSS 檔案(styles.css)的連結來設定內容樣式。
  • 使用外部 JavaScript 檔案 (script.js) 的連結來新增動態功能。

3.將 HTML 渲染為 PDF :使用 RenderHtmlAsPdf 方法將 HTML 內容(包含連結的 CSS 和 JavaScript)轉換為 PDF 文件。

4.儲存 PDF :將產生的 PDF 文件儲存為名為"awesomeIronPDF_styled_content.pdf"的檔案。

筆記

*外部 CSS 和 JS 檔案:確保連結的 styles.css 和 script.js 檔案在程式碼運行的環境中可存取。

  • PDF 中的 JavaScript :IronPDF 會在產生 PDF 之前執行 JavaScript 來渲染內容。

輸出 PDF

C# 建立 PDF - IronPDF 使用完整指南:圖 8

步驟 7:新增影像和點陣圖。

using IronPdf;
using System;
using System.IO;

class Program
{
    static void Main()
    {
        // Set your IronPDF license key
        IronPdf.License.LicenseKey = "your key";

        // Create a PDF renderer instance
        ChromePdfRenderer renderer = new ChromePdfRenderer();

        // Import image file as bytes
        byte[] base64Bytes = File.ReadAllBytes("image.jpg"); // Use your own image file here

        // Convert image bytes to Base64 string
        string imgDataUri = @"data:image/png;base64," + Convert.ToBase64String(base64Bytes);

        // Create HTML content with the embedded Base64 image
        string imgHtml = $"<img src='{imgDataUri}'>";

        // Render HTML content to a PDF
        PdfDocument pdf = renderer.RenderHtmlAsPdf(imgHtml);

        // Save the PDF with the embedded image
        pdf.SaveAs("embedded_sample.pdf");
    }
}
using IronPdf;
using System;
using System.IO;

class Program
{
    static void Main()
    {
        // Set your IronPDF license key
        IronPdf.License.LicenseKey = "your key";

        // Create a PDF renderer instance
        ChromePdfRenderer renderer = new ChromePdfRenderer();

        // Import image file as bytes
        byte[] base64Bytes = File.ReadAllBytes("image.jpg"); // Use your own image file here

        // Convert image bytes to Base64 string
        string imgDataUri = @"data:image/png;base64," + Convert.ToBase64String(base64Bytes);

        // Create HTML content with the embedded Base64 image
        string imgHtml = $"<img src='{imgDataUri}'>";

        // Render HTML content to a PDF
        PdfDocument pdf = renderer.RenderHtmlAsPdf(imgHtml);

        // Save the PDF with the embedded image
        pdf.SaveAs("embedded_sample.pdf");
    }
}
Imports IronPdf
Imports System
Imports System.IO

Friend Class Program
	Shared Sub Main()
		' Set your IronPDF license key
		IronPdf.License.LicenseKey = "your key"

		' Create a PDF renderer instance
		Dim renderer As New ChromePdfRenderer()

		' Import image file as bytes
		Dim base64Bytes() As Byte = File.ReadAllBytes("image.jpg") ' Use your own image file here

		' Convert image bytes to Base64 string
		Dim imgDataUri As String = "data:image/png;base64," & Convert.ToBase64String(base64Bytes)

		' Create HTML content with the embedded Base64 image
		Dim imgHtml As String = $"<img src='{imgDataUri}'>"

		' Render HTML content to a PDF
		Dim pdf As PdfDocument = renderer.RenderHtmlAsPdf(imgHtml)

		' Save the PDF with the embedded image
		pdf.SaveAs("embedded_sample.pdf")
	End Sub
End Class
$vbLabelText   $csharpLabel

說明程式碼

這個 C# 程式示範如何使用 IronPDF 將圖像嵌入到 PDF 文件中,方法是將圖像轉換為 Base64 字串並將其嵌入到 HTML 內容中。

1.許可證密鑰設定:設定 IronPDF 許可證密鑰以啟用全部功能。

2.影像匯入和轉換為 Base64 格式

  • 將圖像檔案(例如 image.jpg)讀取為位元組數組。
  • 將此位元組陣列轉換為 Base64 字串,並添加適當的圖像資料前綴。

3.在 HTML 中嵌入圖片:將表示圖片的 Base64 字串嵌入到 HTML 中。<img>標籤。

4.將 HTML 渲染為 PDF :使用 IronPDF 的 ChromePdfRenderer 將嵌入影像的 HTML 渲染到 PDF 文件中。

5.儲存 PDF :將產生的 PDF(現在包含嵌入的圖像)儲存為"embedded_sample.pdf"。

輸出 PDF

C# 建立 PDF - IronPDF 使用完整指南:圖 9

步驟 8:HTML 檔案轉 PDF。

using IronPdf;

class Program
{
    static void Main()
    {
        // Instantiate Renderer
        var renderer = new ChromePdfRenderer();

        // Create a PDF from an existing HTML file using C#
        var pdf = renderer.RenderHtmlFileAsPdf("sample.html");

        // Export to a file or Stream
        pdf.SaveAs("output.pdf");
    }
}
using IronPdf;

class Program
{
    static void Main()
    {
        // Instantiate Renderer
        var renderer = new ChromePdfRenderer();

        // Create a PDF from an existing HTML file using C#
        var pdf = renderer.RenderHtmlFileAsPdf("sample.html");

        // Export to a file or Stream
        pdf.SaveAs("output.pdf");
    }
}
Imports IronPdf

Friend Class Program
	Shared Sub Main()
		' Instantiate Renderer
		Dim renderer = New ChromePdfRenderer()

		' Create a PDF from an existing HTML file using C#
		Dim pdf = renderer.RenderHtmlFileAsPdf("sample.html")

		' Export to a file or Stream
		pdf.SaveAs("output.pdf")
	End Sub
End Class
$vbLabelText   $csharpLabel

說明程式碼

本程式示範如何使用 C# 中的 IronPDF 庫將現有的 HTML 文件轉換為 PDF 文件。

1.實例化渲染器:建立 ChromePdfRenderer 實例,負責將 HTML 內容渲染成 PDF。

2.將 HTML 轉換為 PDF :使用RenderHtmlFileAsPdf方法將 HTML 檔案 (sample.html) 轉換為 PDF 文件。

3.儲存 PDF :使用SaveAs方法將 PDF 文件儲存為"output.pdf"。

步驟 9:PDF 轉換的 URL。

以下程式碼片段示範如何使用 IronPDF 將 URL 轉換為 PDF。

using IronPdf;

class Program
{
    static void Main()
    {
        // Instantiate Renderer
        var renderer = new ChromePdfRenderer();

        // Create a PDF from a URL or local file path
        var pdf = renderer.RenderUrlAsPdf("https://ironpdf.com/");

        // Export to a file or Stream
        pdf.SaveAs("url.pdf");
    }
}
using IronPdf;

class Program
{
    static void Main()
    {
        // Instantiate Renderer
        var renderer = new ChromePdfRenderer();

        // Create a PDF from a URL or local file path
        var pdf = renderer.RenderUrlAsPdf("https://ironpdf.com/");

        // Export to a file or Stream
        pdf.SaveAs("url.pdf");
    }
}
Imports IronPdf

Friend Class Program
	Shared Sub Main()
		' Instantiate Renderer
		Dim renderer = New ChromePdfRenderer()

		' Create a PDF from a URL or local file path
		Dim pdf = renderer.RenderUrlAsPdf("https://ironpdf.com/")

		' Export to a file or Stream
		pdf.SaveAs("url.pdf")
	End Sub
End Class
$vbLabelText   $csharpLabel

說明程式碼

1.實例化渲染器:創建ChromePdfRenderer的實例。

2.將 URL 或本機檔案轉換為 PDF :使用RenderUrlAsPdf從給定的 URL 或本機檔案路徑建立 PDF。

3.儲存 PDF :使用SaveAs方法將產生的 PDF 文件儲存為"url.pdf"。

IronPDF 的應用案例

IronPDF是一個功能強大的 C# PDF 處理庫。 它允許開發人員輕鬆生成、修改和操作 PDF 文件。 以下是IronPDF在 C# 應用程式中的一些常見用例

1.從 HTML 內容產生 PDF 報告

*使用案例*:從動態或靜態 HTML 內容(例如發票、財務報表和產品目錄)建立 PDF 報告。 例如**:一個商業應用程式產生每週銷售報告,並透過電子郵件以 PDF 附件的形式發送這些報告。

2.將網頁轉換為 PDF

*使用案例*:將網頁或 URL 轉換為 PDF 文檔,以便保存文章、整個網站或特定網頁。 例如**:一個允許使用者將任何網頁轉換為 PDF 的工具。

3.在PDF中嵌入圖像、表格和圖表

*使用場景*:將圖像、圖表和複雜表格嵌入 PDF 文件中。 例如**:一款公司應用程序,用於產生帶有嵌入式公司徽標的個人化發票。

4.將HTML表單轉換為PDF

*使用場景*:將 HTML 表單轉換為可填寫或不可填寫的 PDF 表單。 例如**:一個表單管理應用程序,允許用戶在線填寫表單,並將表單下載或透過電子郵件發送為 PDF 文件。

5.編輯修改現有PDF文件

*使用場景*:在現有 PDF 中新增、刪除或修改元素。 例如**:文件管理系統透過為 PDF 文件添加浮水印來防止未經授權的分發。

6.將多個 PDF 檔案合併為一個

*使用場景*:將多個 PDF 檔案合併為一個文件。 例如**:一款供律師事務所使用的工具,可以將多個頁面合併到一個 PDF 文件中,以便儲存和檢索。

7.從PDF文件中提取文字和數據

*使用場景*:從現有 PDF 檔案中提取文字、表格和其他資料。 範例**:OCR 應用程式從掃描的表單中擷取資料。

8.從範本建立 PDF

*使用案例*:使用預先定義的範本產生一致的、帶有品牌標識的 PDF 文件。 例如**:產生可下載 PDF 憑證的 Web 服務。

9. PDF文件的數位簽名

*使用案例*:為 PDF 新增數位簽名,以實現安全的文件簽名。 例如**:合約以電子方式簽署的解決方案。

10.自動產生PDF文檔

*應用場景*:自動產生大量 PDF 文件。 例如**:一個產生每月 PDF 發票的 Web 應用程式。

11.建立互動式 PDF

*使用案例*:建立帶有互動元素的 PDF。 例如**:表單提交應用程式。

12.將PDF轉換為其他格式

*使用場景*:將 PDF 轉換為 HTML 或 Word 等其他格式。 例如**:文件轉換工具。

13.拆分PDF文檔

*使用場景*:將大型 PDF 檔案拆分為較小的檔案。 例如**:掃描應用程式將文件分割成多個 PDF 檔案。

14.對 PDF 文件進行密碼保護

*使用案例*:加密並密碼保護 PDF 文件。 例如**:財務報告應用程式。

授權資訊(提供試用版)

IronPDF提供免費試用。 在使用庫之前,請按如下方式新增:

IronPdf.License.LicenseKey = "your key";
IronPdf.License.LicenseKey = "your key";
IronPdf.License.LicenseKey = "your key"
$vbLabelText   $csharpLabel

結論

IronPDF是一個 .NET PDF 庫,它使在 C# 中產生 PDF 變得簡單且強大。 無論您是產生發票、報告還是其他類型的文檔,IronPDF 都提供強大的功能,例如 HTML 到 PDF 的轉換、自訂頁首和頁尾、PDF 編輯、表單處理等等。 它提供了一種無縫處理 PDF 文件的方式。

使用IronPDF ,您可以輕鬆在 C# 中建立高品質的 PDF ,讓您可以專注於為使用者提供強大的功能,而無需擔心文件格式的複雜性。 無論是處理動態網頁內容還是建立靜態報告,IronPDF 都是滿足您 PDF 需求的可靠解決方案。