使用IRON SUITE

HTML到PDF C# - 使用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. URL到PDF的轉換。

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文件,只需幾行程式碼,這使得從動態網頁內容生成PDF具有理想的效果。

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

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

    • 網站URL到PDFIronPDF 可以將網站的字串URL作為輸入並轉換為PDF。
  2. 自定義頁眉和頁腳

    • IronPDF 允許開發者將自定義頁眉和頁腳新增到PDF文件中,它們可以包含動態內容如頁碼、文件標題或自定義文字。

    • 頁眉和頁腳可以新增到各個頁面或整個文件中的一致元素。
  3. 支持PDF中的JavaScript

    • IronPDF允許在生成PDF之前在HTML內容中執行JavaScript。 這使得動態內容渲染成為可能,例如表單計算或在生成的PDF中的互動性。
  4. 編輯現有的PDFs

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

    • 文字提取和修改允許您以程式方式操控PDF文件中的內容。
  5. 合併和拆分PDFs

    • IronPDF 允許您將多個PDF文件合併為一個文件或將大型PDF拆分為較小的文件,這對於需要組合文件或者分解為更易於管理部分的工作流程非常理想。
  6. 支持互動表單

    • 您可以使用IronPDF建立、填充和操作PDF表單。 它提供對互動表單(如文字字段、複選框和單選按鈕)的完整支持,並允許您使用資料預填表單。
  7. 頁面操作

    • IronPDF 提供方法來操控PDF文件中的個別頁面,例如旋轉頁面、刪除頁面或重新排序。
  8. 安全性和加密

    • IronPDF 允許您對PDF應用密碼保護和加密,確保您的文件安全。 您可以設置使用者權限,以防止列印、複製或編輯PDF。
  9. 水印和品牌標識

    • 借助IronPDF,可以輕鬆將水印新增到PDF文件中。 您可以將文字或圖像作為水印疊加到頁面上,從而保護文件免受未經授權的複製或分發。
  10. 文字和圖像提取

    • IronPDF 允許從PDF文件中提取文字和圖像,讓開發者能夠提取資料進行處理或重用。
  11. Unicode和多語言支持

    • IronPDF 擁有強大的Unicode支持,意即它能夠處理國際字元和字體,使其成為生成多語言PDF的理想選擇。
  12. 性能優化

    • IronPDF 經過性能優化,可以處理大型PDF文件和高流量的請求。 即使是大型資料集或圖像,該程式庫也可確保快速、高效的PDF生成。
  13. API和對開發者友好的工具

    • IronPDF 附帶一個全面且易於使用的API。 開發人員可以通過使用簡單的方法來快速開始進行複雜的任務。
  14. 多平台支持

    • 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 英寸乘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 方法將從HTML生成的封面頁PDF插入到現有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在生成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通過將圖像轉換為Base64字串並嵌入到HTML內容中,將圖像嵌入到PDF文件中。

  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:URL到PDF的轉換

以下程式碼片段演示了使用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文件。 以下是在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文件。
  • 範例:一個Web服務生成可下載的PDF證書。

9. 為PDF文件新增數位簽名

  • 使用案例:為PDF新增數位簽名以實現安全文件簽名。
  • 範例:一個解決方案允許以電子方式簽署合同。

10. 自動化PDF文件生成

  • 使用案例:自動生成大量的PDF文件。
  • 範例:一個Web應用程式每月生成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需求的可靠解決方案。