using IronPdf;
// Disable local disk access or cross-origin requests
Installation.EnableWebSecurity = true;
// Instantiate Renderer
var renderer = new ChromePdfRenderer();
// Create a PDF from a HTML string using C#
var pdf = renderer.RenderHtmlAsPdf("<h1>Hello World</h1>");
// Export to a file or Stream
pdf.SaveAs("output.pdf");
// Advanced Example with HTML Assets
// Load external html assets: Images, CSS and JavaScript.
// An optional BasePath 'C:\site\assets\' is set as the file location to load assets from
var myAdvancedPdf = renderer.RenderHtmlAsPdf("<img src='icons/iron.png'>", @"C:\site\assets\");
myAdvancedPdf.SaveAs("html-with-assets.pdf");
// ZetPDF: 手動 positioning nightmare
graphics.DrawString("Name:", font, brush, new XPoint(50, 100));
graphics.DrawString("John Doe", font, brush, new XPoint(100, 100));
graphics.DrawString("Address:", font, brush, new XPoint(50, 120));
graphics.DrawString("123 Main St", font, brush, new XPoint(100, 120));
// ... hundreds of lines for a simple form
// ZetPDF: 手動 positioning nightmare
graphics.DrawString("Name:", font, brush, new XPoint(50, 100));
graphics.DrawString("John Doe", font, brush, new XPoint(100, 100));
graphics.DrawString("Address:", font, brush, new XPoint(50, 120));
graphics.DrawString("123 Main St", font, brush, new XPoint(100, 120));
// ... hundreds of lines for a simple form
' ZetPDF: 手動 positioning nightmare
graphics.DrawString("Name:", font, brush, New XPoint(50, 100))
graphics.DrawString("John Doe", font, brush, New XPoint(100, 100))
graphics.DrawString("Address:", font, brush, New XPoint(50, 120))
graphics.DrawString("123 Main St", font, brush, New XPoint(100, 120))
' ... hundreds of lines for a simple form
$vbLabelText $csharpLabel
IronPDF 使用 HTML/CSS—布局引擎處理一切:
// IronPDF: Simple HTML
var html = @"
<p><strong>Name:</strong> John Doe</p>
<p><strong>Address:</strong> 123 Main St</p>";
var pdf = renderer.RenderHtmlAsPdf(html);
// IronPDF: Simple HTML
var html = @"
<p><strong>Name:</strong> John Doe</p>
<p><strong>Address:</strong> 123 Main St</p>";
var pdf = renderer.RenderHtmlAsPdf(html);
' IronPDF: Simple HTML
Dim html As String = "
<p><strong>Name:</strong> John Doe</p>
<p><strong>Address:</strong> 123 Main St</p>"
Dim pdf = renderer.RenderHtmlAsPdf(html)
# Remove the ZetPDF.dll reference from your csproj manually, then:
dotnet add package IronPdf
# Remove the ZetPDF.dll reference from your csproj manually, then:
dotnet add package IronPdf
SHELL
步驟 2:更新命名空間
將 ZetPDF 命名空間替換為IronPDF命名空間:
// Before (ZetPDF)
using ZetPDF;
using ZetPDF.Drawing;
using ZetPDF.Fonts;
// After (IronPDF)
using IronPdf;
// Before (ZetPDF)
using ZetPDF;
using ZetPDF.Drawing;
using ZetPDF.Fonts;
// After (IronPDF)
using IronPdf;
Imports IronPdf
$vbLabelText $csharpLabel
步驟 3:初始化授權
在應用啓動時新增授權初始化:
IronPdf.License.LicenseKey = "YOUR-LICENSE-KEY";
IronPdf.License.LicenseKey = "YOUR-LICENSE-KEY";
IronPdf.License.LicenseKey = "YOUR-LICENSE-KEY"
$vbLabelText $csharpLabel
程式碼遷移範例
將HTML轉換為PDF
ZetPDF 發佈的功能列表中不包括原生 HTML-to-PDF 或 URL-to-PDF 轉換器——其功能僅包含 PDF 查看/列印、註解、AES256 加密、表單欄位和文字擷取。 以下虛擬包裝展示了團隊通常如何在 ZetPDF 上建立 HTML-to-PDF 入口點; 實際操作中,工作轉而依賴於外部 HTML 渲染器或低階 XGraphics 繪圖。
ZetPDF 方法(示範——無原生 HTML-to-PDF):
// ZetPDF is NOT on NuGet — install via SDK ZIP from https://zetpdf.com/download/
using ZetPDF;
using System;
class Program
{
static void Main()
{
// 否 native HtmlToPdfConverter exists in ZetPDF — illustrative call only.
var converter = new HtmlToPdfConverter();
var htmlContent = "<html><body><h1>Hello World</h1></body></html>";
converter.ConvertHtmlToPdf(htmlContent, "output.pdf");
Console.WriteLine("PDF created successfully");
}
}
// ZetPDF is NOT on NuGet — install via SDK ZIP from https://zetpdf.com/download/
using ZetPDF;
using System;
class Program
{
static void Main()
{
// 否 native HtmlToPdfConverter exists in ZetPDF — illustrative call only.
var converter = new HtmlToPdfConverter();
var htmlContent = "<html><body><h1>Hello World</h1></body></html>";
converter.ConvertHtmlToPdf(htmlContent, "output.pdf");
Console.WriteLine("PDF created successfully");
}
}
Imports ZetPDF
Imports System
Class Program
Shared Sub Main()
' 否 native HtmlToPdfConverter exists in ZetPDF — illustrative call only.
Dim converter As New HtmlToPdfConverter()
Dim htmlContent As String = "<html><body><h1>Hello World</h1></body></html>"
converter.ConvertHtmlToPdf(htmlContent, "output.pdf")
Console.WriteLine("PDF created successfully")
End Sub
End Class
$vbLabelText $csharpLabel
IronPDF 方法:
// NuGet: Install-Package IronPdf
using IronPdf;
using System;
class Program
{
static void Main()
{
IronPdf.License.LicenseKey = "YOUR-LICENSE-KEY";
var renderer = new ChromePdfRenderer();
var htmlContent = "<html><body><h1>Hello World</h1></body></html>";
var pdf = renderer.RenderHtmlAsPdf(htmlContent);
pdf.SaveAs("output.pdf");
Console.WriteLine("PDF created successfully");
}
}
// NuGet: Install-Package IronPdf
using IronPdf;
using System;
class Program
{
static void Main()
{
IronPdf.License.LicenseKey = "YOUR-LICENSE-KEY";
var renderer = new ChromePdfRenderer();
var htmlContent = "<html><body><h1>Hello World</h1></body></html>";
var pdf = renderer.RenderHtmlAsPdf(htmlContent);
pdf.SaveAs("output.pdf");
Console.WriteLine("PDF created successfully");
}
}
Imports IronPdf
Imports System
Module Program
Sub Main()
IronPdf.License.LicenseKey = "YOUR-LICENSE-KEY"
Dim renderer As New ChromePdfRenderer()
Dim htmlContent As String = "<html><body><h1>Hello World</h1></body></html>"
Dim pdf = renderer.RenderHtmlAsPdf(htmlContent)
pdf.SaveAs("output.pdf")
Console.WriteLine("PDF created successfully")
End Sub
End Module
// ZetPDF is NOT on NuGet — install via SDK ZIP from https://zetpdf.com/download/
using ZetPDF;
using System;
class Program
{
static void Main()
{
// 否 native ConvertUrlToPdf exists in ZetPDF — illustrative call only.
var converter = new HtmlToPdfConverter();
var url = "https://www.example.com";
converter.ConvertUrlToPdf(url, "webpage.pdf");
Console.WriteLine("PDF from URL created successfully");
}
}
// ZetPDF is NOT on NuGet — install via SDK ZIP from https://zetpdf.com/download/
using ZetPDF;
using System;
class Program
{
static void Main()
{
// 否 native ConvertUrlToPdf exists in ZetPDF — illustrative call only.
var converter = new HtmlToPdfConverter();
var url = "https://www.example.com";
converter.ConvertUrlToPdf(url, "webpage.pdf");
Console.WriteLine("PDF from URL created successfully");
}
}
Imports ZetPDF
Imports System
Class Program
Shared Sub Main()
' 否 native ConvertUrlToPdf exists in ZetPDF — illustrative call only.
Dim converter As New HtmlToPdfConverter()
Dim url As String = "https://www.example.com"
converter.ConvertUrlToPdf(url, "webpage.pdf")
Console.WriteLine("PDF from URL created successfully")
End Sub
End Class
$vbLabelText $csharpLabel
IronPDF 方法:
// NuGet: Install-Package IronPdf
using IronPdf;
using System;
class Program
{
static void Main()
{
IronPdf.License.LicenseKey = "YOUR-LICENSE-KEY";
var renderer = new ChromePdfRenderer();
var url = "https://www.example.com";
var pdf = renderer.RenderUrlAsPdf(url);
pdf.SaveAs("webpage.pdf");
Console.WriteLine("PDF from URL created successfully");
}
}
// NuGet: Install-Package IronPdf
using IronPdf;
using System;
class Program
{
static void Main()
{
IronPdf.License.LicenseKey = "YOUR-LICENSE-KEY";
var renderer = new ChromePdfRenderer();
var url = "https://www.example.com";
var pdf = renderer.RenderUrlAsPdf(url);
pdf.SaveAs("webpage.pdf");
Console.WriteLine("PDF from URL created successfully");
}
}
Imports IronPdf
Imports System
Module Program
Sub Main()
IronPdf.License.LicenseKey = "YOUR-LICENSE-KEY"
Dim renderer As New ChromePdfRenderer()
Dim url As String = "https://www.example.com"
Dim pdf = renderer.RenderUrlAsPdf(url)
pdf.SaveAs("webpage.pdf")
Console.WriteLine("PDF from URL created successfully")
End Sub
End Module
// ZetPDF is NOT on NuGet — install via SDK ZIP from https://zetpdf.com/download/
using ZetPDF;
using System;
using System.Collections.Generic;
class Program
{
static void Main()
{
// ZetPDF has no built-in PdfMerger — illustrative call only.
var merger = new PdfMerger();
var files = new List<string> { "document1.pdf", "document2.pdf" };
merger.MergeFiles(files, "merged.pdf");
Console.WriteLine("PDFs merged successfully");
}
}
// ZetPDF is NOT on NuGet — install via SDK ZIP from https://zetpdf.com/download/
using ZetPDF;
using System;
using System.Collections.Generic;
class Program
{
static void Main()
{
// ZetPDF has no built-in PdfMerger — illustrative call only.
var merger = new PdfMerger();
var files = new List<string> { "document1.pdf", "document2.pdf" };
merger.MergeFiles(files, "merged.pdf");
Console.WriteLine("PDFs merged successfully");
}
}
Imports ZetPDF
Imports System
Imports System.Collections.Generic
Module Program
Sub Main()
' ZetPDF has no built-in PdfMerger — illustrative call only.
Dim merger As New PdfMerger()
Dim files As New List(Of String) From {"document1.pdf", "document2.pdf"}
merger.MergeFiles(files, "merged.pdf")
Console.WriteLine("PDFs merged successfully")
End Sub
End Module
$vbLabelText $csharpLabel
IronPDF 方法:
// NuGet: Install-Package IronPdf
using IronPdf;
using System;
using System.Collections.Generic;
class Program
{
static void Main()
{
IronPdf.License.LicenseKey = "YOUR-LICENSE-KEY";
var pdfs = new List<PdfDocument>
{
PdfDocument.FromFile("document1.pdf"),
PdfDocument.FromFile("document2.pdf")
};
var merged = PdfDocument.Merge(pdfs);
merged.SaveAs("merged.pdf");
Console.WriteLine("PDFs merged successfully");
}
}
// NuGet: Install-Package IronPdf
using IronPdf;
using System;
using System.Collections.Generic;
class Program
{
static void Main()
{
IronPdf.License.LicenseKey = "YOUR-LICENSE-KEY";
var pdfs = new List<PdfDocument>
{
PdfDocument.FromFile("document1.pdf"),
PdfDocument.FromFile("document2.pdf")
};
var merged = PdfDocument.Merge(pdfs);
merged.SaveAs("merged.pdf");
Console.WriteLine("PDFs merged successfully");
}
}
Imports IronPdf
Imports System
Imports System.Collections.Generic
Module Program
Sub Main()
IronPdf.License.LicenseKey = "YOUR-LICENSE-KEY"
Dim pdfs As New List(Of PdfDocument) From {
PdfDocument.FromFile("document1.pdf"),
PdfDocument.FromFile("document2.pdf")
}
Dim merged As PdfDocument = PdfDocument.Merge(pdfs)
merged.SaveAs("merged.pdf")
Console.WriteLine("PDFs merged successfully")
End Sub
End Module
using ZetPDF;
using ZetPDF.Drawing;
var document = new PdfDocument();
var page = document.AddPage();
page.Width = XUnit.FromMillimeter(210);
page.Height = XUnit.FromMillimeter(297);
var graphics = XGraphics.FromPdfPage(page);
var titleFont = new XFont("Arial", 24, XFontStyle.Bold);
var bodyFont = new XFont("Arial", 12);
graphics.DrawString("Company Report", titleFont, XBrushes.Navy,
new XPoint(50, 50));
graphics.DrawString("This is the introduction paragraph.", bodyFont, XBrushes.Black,
new XPoint(50, 80));
graphics.DrawString("Generated: " + DateTime.Now.ToString(), bodyFont, XBrushes.Gray,
new XPoint(50, 100));
document.Save("report.pdf");
using ZetPDF;
using ZetPDF.Drawing;
var document = new PdfDocument();
var page = document.AddPage();
page.Width = XUnit.FromMillimeter(210);
page.Height = XUnit.FromMillimeter(297);
var graphics = XGraphics.FromPdfPage(page);
var titleFont = new XFont("Arial", 24, XFontStyle.Bold);
var bodyFont = new XFont("Arial", 12);
graphics.DrawString("Company Report", titleFont, XBrushes.Navy,
new XPoint(50, 50));
graphics.DrawString("This is the introduction paragraph.", bodyFont, XBrushes.Black,
new XPoint(50, 80));
graphics.DrawString("Generated: " + DateTime.Now.ToString(), bodyFont, XBrushes.Gray,
new XPoint(50, 100));
document.Save("report.pdf");
Imports ZetPDF
Imports ZetPDF.Drawing
Dim document As New PdfDocument()
Dim page = document.AddPage()
page.Width = XUnit.FromMillimeter(210)
page.Height = XUnit.FromMillimeter(297)
Dim graphics = XGraphics.FromPdfPage(page)
Dim titleFont As New XFont("Arial", 24, XFontStyle.Bold)
Dim bodyFont As New XFont("Arial", 12)
graphics.DrawString("Company Report", titleFont, XBrushes.Navy, New XPoint(50, 50))
graphics.DrawString("This is the introduction paragraph.", bodyFont, XBrushes.Black, New XPoint(50, 80))
graphics.DrawString("Generated: " & DateTime.Now.ToString(), bodyFont, XBrushes.Gray, New XPoint(50, 100))
document.Save("report.pdf")
$vbLabelText $csharpLabel
IronPDF HTML 方法:
using IronPdf;
var html = $@"
<html>
<head>
<style>
body {{font-family: Arial, sans-serif; padding: 50px;}}
h1 {{color: navy;}}
.date {{color: gray;}}
</style>
</head>
<body>
<h1>Company Report</h1>
<p>This is the introduction paragraph.</p>
<p class='date'>Generated: {DateTime.Now}</p>
</body>
</html>";
var renderer = new ChromePdfRenderer();
renderer.RenderingOptions.PaperSize = PdfPaperSize.A4;
var pdf = renderer.RenderHtmlAsPdf(html);
pdf.SaveAs("report.pdf");
using IronPdf;
var html = $@"
<html>
<head>
<style>
body {{font-family: Arial, sans-serif; padding: 50px;}}
h1 {{color: navy;}}
.date {{color: gray;}}
</style>
</head>
<body>
<h1>Company Report</h1>
<p>This is the introduction paragraph.</p>
<p class='date'>Generated: {DateTime.Now}</p>
</body>
</html>";
var renderer = new ChromePdfRenderer();
renderer.RenderingOptions.PaperSize = PdfPaperSize.A4;
var pdf = renderer.RenderHtmlAsPdf(html);
pdf.SaveAs("report.pdf");
Imports IronPdf
Dim html As String = $"
<html>
<head>
<style>
body {{font-family: Arial, sans-serif; padding: 50px;}}
h1 {{color: navy;}}
.date {{color: gray;}}
</style>
</head>
<body>
<h1>Company Report</h1>
<p>This is the introduction paragraph.</p>
<p class='date'>Generated: {DateTime.Now}</p>
</body>
</html>"
Dim renderer As New ChromePdfRenderer()
renderer.RenderingOptions.PaperSize = PdfPaperSize.A4
Dim pdf = renderer.RenderHtmlAsPdf(html)
pdf.SaveAs("report.pdf")
$vbLabelText $csharpLabel
ZetPDF 方法需要建立字體物件、計算精確的像素位置,並手動管理圖形操作。IronPDF方法使用網頁開發者已經熟悉的標準 HTML 和 CSS——字體、顏色和布局通過熟悉的 CSS 屬性進行處理。