使用IRON SUITE

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

介紹

從HTML內容創建PDF文件已成為各種應用的基本需求,從生成發票到存檔網頁內容。 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。

IronPDF介紹

IronPDF 是一個.NET程式庫,讓開發者可以輕鬆地將HTML轉換為PDF。 它支持廣泛的功能,包括CSS、JavaScript,甚至是嵌入的圖像。 使用IronPDF,您可以創建看起來與HTML網頁完全相同的PDF,確保格式之間的無縫銜接。 此程式庫特別適用於需要即時生成動態PDF文件的網頁應用程序。

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

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

IronPDF C#創建PDF文件的關鍵特性

  1. HTML到PDF轉換
  • HTML渲染IronPDF 能將HTML文檔或網頁(包括帶CSS、圖像和JavaScript的HTML)直接轉換為PDF文檔,只需幾行代碼,使其非常適合從動態網頁內容生成PDF。

  • 支持現代HTML/CSS:IronPDF 處理現代HTML5、CSS3和JavaScript,確保您的基於網頁的內容準確地呈現為PDF,保留佈局、字體和交互元素。

  • 高級渲染:它使用Chrome的渲染引擎(通過Chromium)進行準確、高質量的PDF生成,使其比許多其他HTML到PDF程式庫更可靠。

  • 網站網址轉換為PDFIronPDF 可以將網站的字符串URL作為輸入並轉換為PDF。
  1. 自定義頁眉和頁腳
  • IronPDF 允許開發者為PDF文檔添加自定義頁眉和頁腳,可包括動態內容,例如頁碼、文件標題或自定義文本。

頁眉和頁腳可以添加到單個頁面或作為整個文檔的一致元素。

  1. 支持PDF中的JavaScript

IronPDF 在PDF生成之前允許HTML內容中執行JavaScript。 這允許動態內容渲染,例如表單計算或在生成的PDF中處理交互。

  1. 編輯現有PDF
  • IronPDF 提供編輯現有PDF的能力。 您可以修改文本、圖像,並在現有的PDF文件中添加註釋。 這一功能對於為文件添加水印、添加簽名或更新PDF文件中的內容非常有用。

  • 提取和修改文本允許您以程式化的方式操控PDF文檔中的內容。
  1. 合併和分割PDF
  • IronPDF 允許您將多個PDF文件合併為一個文檔,或將大PDF分割成較小的文件,這在需要將文件合併或分解為更易管理的部分的工作流程中非常理想。
  1. 支持互動式表單

您可以使用IronPDF創建、填寫和操控PDF表單。 它為互動式表單(如文本框、複選框和單選按鈕)提供全面支持,允許您預先填充表單數據。

  1. 頁面操作
  • IronPDF 提供操控PDF文檔中各個頁面的方法,例如旋轉頁面、刪除頁面或重新排序頁面。
  1. 安全和加密
  • IronPDF 允許您為PDF應用密碼保護和加密,確保您的文檔安全。 您可以設定使用者權限,以防止PDF的打印、複製或編輯。
  1. 添加水印和品牌
  • 使用IronPDF,為PDF文件添加水印變得輕而易舉。 您可以在頁面上疊加文本或圖像作為水印,為您的文檔提供防止未經授權的複製或傳播的保障。
  1. 文本和圖像提取
  • IronPDF 可從PDF文檔中提取文字和圖像,使開發者得以提取數據進行處理或再利用。
  1. 支持Unicode和多語言
  • IronPDF 具有強大的Unicode支持,意味著它可以處理國際字符和字體,使其非常適合於生成多語言的PDF。
  1. 針對效能優化
  • IronPDF 進行了效能優化,能夠處理大型PDF文檔和大量的請求。 即便是大型數據集或圖像,這個程式庫也能確保快速而高效的PDF生成。
  1. API和開發者友好的工具
  • IronPDF 配備全面且易於使用的API。 開發者可以通過簡單的方法呼叫快速入門,以執行複雜的任務。
  1. 跨平台支持
  • IronPDF 是跨平台兼容的,意味著它可以在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程式庫。

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

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文檔:然後,程式創建了一個新的PDF文檔,尺寸為270x270點。 一個點是印刷中的一個測量單位,相當於1/72吋。 因此,這會創建一個大約3.75英寸 x 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>標籤,文本為"Awesome IronPDF Library",通過RenderHtmlAsPdf方法將其轉換為封面頁PDF。

  4. 插入封面頁:使用PrependPdf方法將封面頁PDF(從HTML生成)插入到現有PDF文檔的開頭。

  5. 保存修改後的PDF:將修改後的PDF(帶有新封面頁)保存成文件名"simple_WithTitle.pdf"。 原始"simple.pdf"中的內容被保留,但現在以新添加的標題頁開頭。

輸出PDF

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

步驟5:添加頁眉和頁尾

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 {page} of {total-pages}"的頁碼,對齊於右側。
    • 頁腳包含文本"Confidential",居中對齊於每頁。
  5. 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執行JavaScript以在生成PDF之前渲染內容。

輸出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

代碼解釋

此程序演示了如何使用IronPDF程式庫在C#中將現有的HTML文件轉換為PDF文檔。

  1. 實例化渲染器:創建一個負責將HTML內容渲染為PDF的ChromePdfRenderer實例。

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

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

步驟9:網址轉換成PDF

以下代碼片段展示了如何使用IronPDF將網址轉換為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. 將網址或本地文件轉換為PDF:使用RenderUrlAsPdf從指定的網址或本地文件路徑創建PDF。

  3. 保存PDF:使用SaveAs方法將結果PDF文檔保存為"url.pdf"。

IronPDF的使用案例

IronPDF 是一個用於在C#中操作PDF的強大程式庫。 它允許開發者輕鬆生成、修改和操作PDF文件。 以下是一些在C#應用程序中常見的使用案例

1. 從HTML內容生成PDF報告

  • 使用案例:使用動態或靜態HTML內容(如發票、財務報表和產品目錄)創建PDF報告。
  • 範例:業務應用程序生成每週銷售報告並作為PDF附件發送。

2. 將網頁轉換為PDF

  • 使用案例:將網頁或網址轉換為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證書的網路服務。

9. 為PDF文檔添加數位簽名

  • 使用案例:為PDF添加數位簽名以進行安全的文檔簽署。
  • 範例:合同電子簽署解決方案。

10. 自動化PDF文檔生成

  • 使用案例:自動生成大量PDF文檔。
  • 範例:一個網絡應用程序自動生成月度PDF發票。

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需求的可靠解決方案。