10 .NET API 產品適用於您的辦公文檔
總套件價值:
$7,192 美元
從 HTML 內容創建 PDF 文件已成為各種應用程式的一個基本需求,從生成發票到存檔網頁內容皆是如此。 IronPDF 是一個強大且多功能的 .NET 函式庫,簡化了將 HTML 轉換為 PDF 的過程,使開發者能夠在 C# 中輕鬆創建高品質的 PDF 文件。 在本文中,我們將探索IronPDF的功能,並提供逐步指南來使用它進行PDF創建。
創建一個新的 Visual Studio 專案
從 NuGet 套件管理器中新增 IronPDF 函式庫
創建一個簡單的 PDF 檔案。
修改 PDF 檔案。
添加頁首和頁尾
包含外部樣式表和腳本
包括圖像和位圖。
HTML 文件轉換為 PDF。
IronPDF 是一個 .NET 函式庫,可以讓開發者輕鬆地將 HTML 轉換為 PDF。 它支持多種功能,包括 CSS、JavaScript,甚至嵌入式圖像。 使用 IronPDF,您可以創建外觀與您的 HTML 網頁完全相同的 PDF,確保格式之間的無縫銜接。 這個程式庫對於需要即時生成動態 PDF 文件的網路應用程式特別有用。
IronPDF 允許開發人員將 PDF 功能無縫整合到 .NET 應用程式中,而無需手動管理 PDF 檔案結構。 IronPDF 利用 基於 Chrome 的渲染引擎 將 HTML 頁面(包括複雜的 CSS、JavaScript 和圖像)轉換為結構良好的 PDF 文件。 它可以用於生成報告、發票、電子書或任何需要以 PDF 格式呈現的文件。
IronPDF 非常多功能,不僅可以呈現 PDF,還提供廣泛的 PDF 操作選項,例如編輯、表單處理、加密等。
HTML 轉換為 PDF
HTML 渲染:IronPDF 可以將 HTML 文件或網頁(包括帶有 CSS、圖片和 JavaScript 的 HTML)直接轉換為 PDF 文件,只需幾行代碼。 這非常適合從動態網頁內容生成 PDF。
支援現代 HTML/CSS:IronPDF 支援現代化的 HTML5、CSS3 和 JavaScript 以及各種 PDF 生成任務,確保您的網頁內容能夠準確地渲染成 PDF,保留佈局、字體和互動元素。
自訂頁首和頁尾
PDF 中的 JavaScript 支援
編輯現有 PDF
合併及分割PDF
支援互動表單
頁面操作
安全性和加密
浮水印和品牌設計
文字與圖片擷取
Unicode 和多語言支援
API 和開發者友好工具
現在讓我們開始創建一個新專案,打開 Visual Studio 並按照下圖創建一個新專案。
從Pixabay添加上傳
或將圖片拖放到此處
新增圖片替代文字
選擇建立控制台應用程式。
從Pixabay添加上傳
或將圖片拖放到此處
新增圖片替代文字
提供專案名稱和位置。
從Pixabay添加上傳
或將圖片拖放到此處
新增圖片替代文字
選擇 .NET 版本
從Pixabay添加上傳
或將圖片拖放到此處
新增圖片替代文字
創建一個新專案。
在 Visual Studio 控制台應用程式中使用 NuGet 套件管理器時,可以使用以下命令添加 IronPDF NuGet 函式庫。
Install-Package IronPdf
Install-Package IronPdf
'INSTANT VB TODO TASK: The following line uses invalid syntax:
'Install-Package IronPdf
此外,可以使用 Visual Studio 套件管理器安裝 IronPDF。
從Pixabay添加上傳
或將圖片拖放到此處
新增圖片替代文字
使用 IronPDF 庫輕鬆生成 PDF 文件。 現在讓我們從一個簡單的空白 PDF 文件開始。
class Program
{
static void Main()
{
IronPdf.License.LicenseKey = "your key";
PdfDocument pdf = new PdfDocument(270, 270);
pdf.SaveAs("simple.pdf"); // Generate pdf file
}
}
class Program
{
static void Main()
{
IronPdf.License.LicenseKey = "your key";
PdfDocument pdf = new PdfDocument(270, 270);
pdf.SaveAs("simple.pdf"); // Generate pdf file
}
}
Friend Class Program
Shared Sub Main()
IronPdf.License.LicenseKey = "your key"
Dim pdf As New PdfDocument(270, 270)
pdf.SaveAs("simple.pdf") ' Generate pdf file
End Sub
End Class
本程式示範如何使用IronPDF庫在C#中建立PDF文件。 以下是程式碼中的過程:
許可證金鑰設置:該程式首先為 IronPDF 庫設置許可證金鑰。 這是使用該庫全部功能所必需的,因為授權密鑰可確保您可以使用全部功能(而不僅限於試用版本)。
建立 PDF 文件:然後程式會建立一個新的PDF 文件,尺寸為270x270 點。 點是一個印刷中的測量單位,相當於1/72英寸。 因此,這將創建一個大約3.75 英寸乘 3.75 英寸的正方形文檔。
class Program
{
static void Main()
{
IronPdf.License.LicenseKey = "your key";
var pdf = new PdfDocument("simple.pdf");
var renderer = new ChromePdfRenderer();
var pagePdf = renderer.RenderHtmlAsPdf("<h1>Awesome IronPDF Library</h1>");
pdf.PrependPdf(pagePdf);
pdf.SaveAs("simple_WithTitle.pdf");
}
}
class Program
{
static void Main()
{
IronPdf.License.LicenseKey = "your key";
var pdf = new PdfDocument("simple.pdf");
var renderer = new ChromePdfRenderer();
var pagePdf = renderer.RenderHtmlAsPdf("<h1>Awesome IronPDF Library</h1>");
pdf.PrependPdf(pagePdf);
pdf.SaveAs("simple_WithTitle.pdf");
}
}
Friend Class Program
Shared Sub Main()
IronPdf.License.LicenseKey = "your key"
Dim pdf = New PdfDocument("simple.pdf")
Dim renderer = New ChromePdfRenderer()
Dim pagePdf = renderer.RenderHtmlAsPdf("<h1>Awesome IronPDF Library</h1>")
pdf.PrependPdf(pagePdf)
pdf.SaveAs("simple_WithTitle.pdf")
End Sub
End Class
許可證密鑰設定:
加載現有的PDF:
將 HTML 渲染為 PDF:
為封面頁添加前綴:
儲存已修改的 PDF:
從Pixabay添加上傳
或將圖片拖放到此處
新增圖片替代文字
class Program
{
static void Main()
{
IronPdf.License.LicenseKey = "your code";
// Create a new HtmlToPdf object
var Renderer = new ChromePdfRenderer();
string htmlContent = "<html><body><h1>IronPDF: An Awesome PDF Generation Library</h1><h1>Report</h1><p>This is a sample report.</p></body></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 with headers and footers
var pdfDocument = Renderer.RenderHtmlAsPdf(htmlContent);
pdfDocument.AddHtmlHeadersAndFooters(new ChromePdfRenderOptions
{
HtmlHeader= new HtmlHeaderFooter() { HtmlFragment=headerHtml },
HtmlFooter = new HtmlHeaderFooter() { HtmlFragment=footerHtml }
});
pdfDocument.SaveAs("report.pdf");
}
}
class Program
{
static void Main()
{
IronPdf.License.LicenseKey = "your code";
// Create a new HtmlToPdf object
var Renderer = new ChromePdfRenderer();
string htmlContent = "<html><body><h1>IronPDF: An Awesome PDF Generation Library</h1><h1>Report</h1><p>This is a sample report.</p></body></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 with headers and footers
var pdfDocument = Renderer.RenderHtmlAsPdf(htmlContent);
pdfDocument.AddHtmlHeadersAndFooters(new ChromePdfRenderOptions
{
HtmlHeader= new HtmlHeaderFooter() { HtmlFragment=headerHtml },
HtmlFooter = new HtmlHeaderFooter() { HtmlFragment=footerHtml }
});
pdfDocument.SaveAs("report.pdf");
}
}
Friend Class Program
Shared Sub Main()
IronPdf.License.LicenseKey = "your code"
' Create a new HtmlToPdf object
Dim Renderer = New ChromePdfRenderer()
Dim htmlContent As String = "<html><body><h1>IronPDF: An Awesome PDF Generation Library</h1><h1>Report</h1><p>This is a sample report.</p></body></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 with headers and footers
Dim pdfDocument = Renderer.RenderHtmlAsPdf(htmlContent)
pdfDocument.AddHtmlHeadersAndFooters(New ChromePdfRenderOptions With {
.HtmlHeader= New HtmlHeaderFooter() With {.HtmlFragment=headerHtml},
.HtmlFooter = New HtmlHeaderFooter() With {.HtmlFragment=footerHtml}
})
pdfDocument.SaveAs("report.pdf")
End Sub
End Class
許可證密鑰設定:
創建 PDF 渲染器:
定義 HTML 內容:
定義頁首和頁尾 HTML:
指定自訂的標頭和頁尾 HTML 字串:
HTML 轉換為 PDF:
添加頁眉和頁腳:
儲存 PDF:
從Pixabay添加上傳
或將圖片拖放到此處
新增圖片替代文字
class Program
{
static void Main()
{
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>
<h1>Styled Content</h1>
<p id='dynamic-text'>This content is styled using an external CSS file and JavaScript.</p>
</body>
</html>";
var pdfDocument = Renderer.RenderHtmlAsPdf(htmlContent);
pdfDocument.SaveAs("awesomeIronPDF_styled_content.pdf");
}
}
class Program
{
static void Main()
{
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>
<h1>Styled Content</h1>
<p id='dynamic-text'>This content is styled using an external CSS file and JavaScript.</p>
</body>
</html>";
var pdfDocument = Renderer.RenderHtmlAsPdf(htmlContent);
pdfDocument.SaveAs("awesomeIronPDF_styled_content.pdf");
}
}
Friend Class Program
Shared Sub Main()
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>
<h1>Styled Content</h1>
<p id='dynamic-text'>This content is styled using an external CSS file and JavaScript.</p>
</body>
</html>"
Dim pdfDocument = Renderer.RenderHtmlAsPdf(htmlContent)
pdfDocument.SaveAs("awesomeIronPDF_styled_content.pdf")
End Sub
End Class
/* styles.css */
body {
font-family: Arial, sans-serif;
margin: 20px;
}
h1 {
color: #007BFF;
}
p {
font-size: 14px;
line-height: 1.6;
}
/* styles.css */
body {
font-family: Arial, sans-serif;
margin: 20px;
}
h1 {
color: #007BFF;
}
p {
font-size: 14px;
line-height: 1.6;
}
' styles.css
body
If True Then
font-family: Arial, sans-serif
margin:
20px
End If
h1
If True Then
color:
#007BFF;
End If
p
If True Then
font-size: 14px
line-height: 1.6
End If
// 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.";
});
' script.js
document.addEventListener( 'DOMContentLoaded', @function() { var dynamicText = document.getElementById('dynamic-text'); dynamicText.textContent = "This content has been modified by JavaScript."; });
此代碼展示如何在 C# 中使用 IronPDF 從包含外部 CSS 和 JavaScript 文件鏈接的 HTML 內容生成 PDF。 它展示了如何創建具有樣式內容和動態行為(通過JavaScript)的PDF。
許可證密鑰設定:
使用外部資源定義HTML內容:
HTML 字串定義為:
一個外部CSS文件的連結(styles.css)來樣式化內容。
),此段落由外部CSS設置樣式並可能被JavaScript修改。
將 HTML 渲染為 PDF:
儲存 PDF:
從Pixabay添加上傳
或將圖片拖放到此處
新增圖片替代文字
class Program
{
static void Main()
{
IronPdf.License.LicenseKey = "your key";
ChromePdfRenderer renderer = new ChromePdfRenderer();
// Import image file as byte
byte[] base64Bytes = File.ReadAllBytes("image.jpg"); // Use your own here
// Convert byte to base64
string imgDataUri = @"data:image/png;base64," + Convert.ToBase64String(base64Bytes);
string imgHtml = $"<img src='{imgDataUri}'>";
PdfDocument pdf = renderer.RenderHtmlAsPdf(imgHtml); // document object
pdf.SaveAs("embedded_sample.pdf");
}
}
class Program
{
static void Main()
{
IronPdf.License.LicenseKey = "your key";
ChromePdfRenderer renderer = new ChromePdfRenderer();
// Import image file as byte
byte[] base64Bytes = File.ReadAllBytes("image.jpg"); // Use your own here
// Convert byte to base64
string imgDataUri = @"data:image/png;base64," + Convert.ToBase64String(base64Bytes);
string imgHtml = $"<img src='{imgDataUri}'>";
PdfDocument pdf = renderer.RenderHtmlAsPdf(imgHtml); // document object
pdf.SaveAs("embedded_sample.pdf");
}
}
Friend Class Program
Shared Sub Main()
IronPdf.License.LicenseKey = "your key"
Dim renderer As New ChromePdfRenderer()
' Import image file as byte
Dim base64Bytes() As Byte = File.ReadAllBytes("image.jpg") ' Use your own here
' Convert byte to base64
Dim imgDataUri As String = "data:image/png;base64," & Convert.ToBase64String(base64Bytes)
Dim imgHtml As String = $"<img src='{imgDataUri}'>"
Dim pdf As PdfDocument = renderer.RenderHtmlAsPdf(imgHtml) ' document object
pdf.SaveAs("embedded_sample.pdf")
End Sub
End Class
這個 C# 程式展示如何使用 IronPDF 將圖像嵌入到 PDF 文件中,方法是將圖像轉換為 Base64 字串,然後將其嵌入到 HTML 內容中。 以下是過程的分解:
許可證密鑰設定:
圖像導入和轉換為Base64:
在 HTML 中嵌入圖片:
將 HTML 渲染為 PDF:
儲存 PDF:
從Pixabay添加上傳
或將圖片拖放到此處
新增圖片替代文字
using IronPdf;
// 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;
// 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
' Instantiate Renderer
Private renderer = New ChromePdfRenderer()
' Create a PDF from an existing HTML file using C#
Private pdf = renderer.RenderHtmlFileAsPdf("sample.html")
' Export to a file or Stream
pdf.SaveAs("output.pdf")
本程序演示如何使用IronPDF庫在C#中將現有的HTML文件轉換為PDF文檔。 這是每個步驟的解釋:
實例化渲染器:
將 HTML 轉換為 PDF:
儲存 PDF:
以下代碼片段展示了使用 IronPDF 將 URL 轉換為 PDF。
using IronPdf;
// 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;
// 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");
IRON VB CONVERTER ERROR developers@ironsoftware.com
實例化渲染器:
將 URL 或本地文件轉換為 PDF:
儲存 PDF:
IronPDF 是一個強大的庫,用於在 C# 中處理 PDF。 它允許開發人員輕鬆生成、修改和操作 PDF 文件。 以下是IronPDF在 C# 應用程式中的一些常見使用情境:
IronPdf.License.LicenseKey = "your key";
IronPdf.License.LicenseKey = "your key";
IronPdf.License.LicenseKey = "your key"
IronPDF .NET PDF 庫使在 C# 中生成 PDF 不僅簡單,而且功能強大且多才多藝。無論您是在生成發票、報告或任何其他類型的文件,IronPDF 都提供穩健的功能,如 HTML 到 PDF 的轉換、自訂頁首和頁尾、PDF 編輯、表單處理等。 它提供了一種無縫的方式來處理 PDF,而不必擔心低層次文檔結構的細節。
使用IronPDF,您可以輕鬆在 C# 中創建高品質的 PDF,讓您更專注於為用戶提供出色的功能,而不是煩惱於文檔格式的複雜性。 無論您是處理動態網頁內容還是製作靜態報告,IronPDF 都是滿足您 PDF 需求的可靠解決方案。