
如何在 C# | IronPDF 中將 HTML ZIP 檔渲染為 PDF
VectSharp(由Giorgio Bianchini開發,arklumpus/VectSharp)是一個輕量的跨平台向量圖形程式庫,適用於.NET Standard 2.0,通常用於科學可視化和技術插圖。 當開發團隊需要生成商業文件、報告、發票或基於HTML的內容時,VectSharp的圖形為先範例會產生摩擦。 它被設計用於繪製圖形和圖表,而不是生成文件。
本指南提供了從VectSharp遷移到IronPDF的完整路線圖,包含逐步指導、程式碼比較以及為評估此轉換的專業.NET開發者提供的實用例子。
為什麼要從VectSharp遷移
VectSharp是一個向量圖形程式庫,設計用來建立圖表、圖示和技術插圖,具有可插入的輸出層,包括VectSharp.Raster。 它不是為了文件生成而設計的 - 這是一個繪圖程式庫,其Document上。 開發團隊考慮遷移的主要原因包括:
僅限科學專注:VectSharp是專為資料可視化和繪圖設計的,而不是商業文件如發票、報告或證書。
不支持HTML:VectSharp無法將HTML或CSS轉換為PDF。 每個元素都必須使用向量圖形命令手動繪製。
基於座標的API:每個元素都必須以精確的X,Y座標定位。 沒有自動佈局、流動或文字換行功能。
不支持CSS樣式:所有的樣式都是通過方法調用編程的。 網頁開發者不能利用他們現有的CSS知識。
不支持JavaScript:VectSharp無法渲染動態網頁內容、交互式圖表或基於JavaScript的可視化。
不支援文字佈局:自動文字換行、分頁和流動佈局不可用。 開發者必須手動計算文字位置和頁面分隔。
**圖形為先: **這個程式庫是為繪圖而設計的,不是為報告或發票設計的。 文件生成需要大量手動工作。
核心問題:圖形程式庫對比文件生成器
VectSharp要求每個元素都進行手動向量繪製:
// VectSharp: 手動 vector drawing for every element
Page page = new Page(595, 842);
Graphics graphics = page.Graphics;
graphics.FillRectangle(50, 50, 200, 100, Colour.FromRgb(0, 0, 255));
graphics.FillText(60, 70, "Invoice",
new Font(FontFamily.ResolveFontFamily(FontFamily.StandardFontFamilies.Helvetica), 20),
Colours.White);
// ... continue drawing every single element manually' VectSharp: 手動 vector drawing for every element
Dim page As New Page(595, 842)
Dim graphics As Graphics = page.Graphics
graphics.FillRectangle(50, 50, 200, 100, Colour.FromRgb(0, 0, 255))
graphics.FillText(60, 70, "Invoice", New Font(FontFamily.ResolveFontFamily(FontFamily.StandardFontFamilies.Helvetica), 20), Colours.White)
' ... continue drawing every single element manually' VectSharp: 手動 vector drawing for every element
Dim page As New Page(595, 842)
Dim graphics As Graphics = page.Graphics
graphics.FillRectangle(50, 50, 200, 100, Colour.FromRgb(0, 0, 255))
graphics.FillText(60, 70, "Invoice", New Font(FontFamily.ResolveFontFamily(FontFamily.StandardFontFamilies.Helvetica), 20), Colours.White)
' ... continue drawing every single element manuallyIronPDF使用HTML - 這是通用的文件格式:
// IronPDF: Declarative HTML for document creation
var html = "<h1>Invoice</h1><p>Customer: Acme Corp</p>";
var pdf = renderer.RenderHtmlAsPdf(html);// IronPDF: Declarative HTML for document creation
var html = "<h1>Invoice</h1><p>Customer: Acme Corp</p>";
var pdf = renderer.RenderHtmlAsPdf(html);' IronPDF: Declarative HTML for document creation
Dim html As String = "<h1>Invoice</h1><p>Customer: Acme Corp</p>"
Dim pdf = renderer.RenderHtmlAsPdf(html)' IronPDF: Declarative HTML for document creation
Dim html As String = "<h1>Invoice</h1><p>Customer: Acme Corp</p>"
Dim pdf = renderer.RenderHtmlAsPdf(html)IronPDF與VectSharp:功能比較
了解結構差異有助於技術決策者評估遷移投資:
| 功能 | VectSharp | IronPDF |
|---|---|---|
| 主要用途 | 向量圖形 | 文件建立 |
| PDF輸出 | 是 | 是 |
| HTML支持 | 否 | 是 |
| 授權 | LGPL-3.0-only | 商業 |
| 開源 | 是 | 部分(商業功能) |
| 最佳用於 | 科學可視化 | 普通PDF文件 |
| 自定義化 | 受限於圖形 | 廣泛的,文件相關的 |
| HTML 到 PDF | 否 | 完整的Chromium |
| URL至PDF | 否 | 是 |
| CSS 支援 | 否 | 完整支持 CSS3 |
| JavaScript | 否 | 完整的ES2024 |
| 自動佈局 | 否 | 是 |
| 自動分頁 | 否 | 是 |
| 文字換行 | 手動 | 自動 |
| 合併PDF | 否 | 是 |
| 分割PDF | 否 | 是 |
| 密碼保護 | 否 | 是 |
| 數位簽名 | 否 | 是 |
| 學習曲線 | 高(座標) | 低(HTML/CSS) |
| 程式碼冗長性 | 非常高 | 低 |
快速開始:從VectSharp到IronPDF的遷移
可以立即透過這些基本步驟開始遷移。
步驟 1:替換 NuGet 套件
移除所有VectSharp包:
# Remove VectSharp packages (PDF first, then core)
dotnet remove package VectSharp.PDF
dotnet remove package VectSharp
安裝 IronPDF:
# Install IronPDF
dotnet add package IronPdf
步驟 2:更新命名空間
用IronPDF命名空間替換VectSharp命名空間:
// Before (VectSharp)
using VectSharp;
using VectSharp.PDF;
// After (IronPDF)
using IronPdf;// Before (VectSharp)
using VectSharp;
using VectSharp.PDF;
// After (IronPDF)
using IronPdf;Imports IronPdfImports IronPdf步驟 3:初始化授權
在應用啓動時新增授權初始化:
IronPdf.License.LicenseKey = "YOUR-LICENSE-KEY";IronPdf.License.LicenseKey = "YOUR-LICENSE-KEY";IronPdf.License.LicenseKey = "YOUR-LICENSE-KEY"IronPdf.License.LicenseKey = "YOUR-LICENSE-KEY"程式碼遷移範例
將HTML轉換為PDF
VectSharp不支持HTML到PDF的轉換。 這個基本功能差異推動了許多遷移決定。
VectSharp方法:
// NuGet: Install-Package VectSharp.PDF
using VectSharp;
using VectSharp.PDF;
using VectSharp.SVG;
using System.IO;
class Program
{
static void Main()
{
// VectSharp doesn't directly support HTML to PDF
// It requires manual creation of graphics objects
Document doc = new Document();
Page page = new Page(595, 842); // A4 size
Graphics graphics = page.Graphics;
graphics.FillText(100, 100, "Hello from VectSharp",
new Font(FontFamily.ResolveFontFamily(FontFamily.StandardFontFamilies.Helvetica), 24),
Colours.Black);
doc.Pages.Add(page);
doc.SaveAsPDF("output.pdf");
}
}// NuGet: Install-Package VectSharp.PDF
using VectSharp;
using VectSharp.PDF;
using VectSharp.SVG;
using System.IO;
class Program
{
static void Main()
{
// VectSharp doesn't directly support HTML to PDF
// It requires manual creation of graphics objects
Document doc = new Document();
Page page = new Page(595, 842); // A4 size
Graphics graphics = page.Graphics;
graphics.FillText(100, 100, "Hello from VectSharp",
new Font(FontFamily.ResolveFontFamily(FontFamily.StandardFontFamilies.Helvetica), 24),
Colours.Black);
doc.Pages.Add(page);
doc.SaveAsPDF("output.pdf");
}
}Imports VectSharp
Imports VectSharp.PDF
Imports VectSharp.SVG
Imports System.IO
Class Program
Shared Sub Main()
' VectSharp doesn't directly support HTML to PDF
' It requires manual creation of graphics objects
Dim doc As New Document()
Dim page As New Page(595, 842) ' A4 size
Dim graphics As Graphics = page.Graphics
graphics.FillText(100, 100, "Hello from VectSharp",
New Font(FontFamily.ResolveFontFamily(FontFamily.StandardFontFamilies.Helvetica), 24),
Colours.Black)
doc.Pages.Add(page)
doc.SaveAsPDF("output.pdf")
End Sub
End ClassImports VectSharp
Imports VectSharp.PDF
Imports VectSharp.SVG
Imports System.IO
Class Program
Shared Sub Main()
' VectSharp doesn't directly support HTML to PDF
' It requires manual creation of graphics objects
Dim doc As New Document()
Dim page As New Page(595, 842) ' A4 size
Dim graphics As Graphics = page.Graphics
graphics.FillText(100, 100, "Hello from VectSharp",
New Font(FontFamily.ResolveFontFamily(FontFamily.StandardFontFamilies.Helvetica), 24),
Colours.Black)
doc.Pages.Add(page)
doc.SaveAsPDF("output.pdf")
End Sub
End ClassIronPDF 方法:
// NuGet: Install-Package IronPdf
using IronPdf;
class Program
{
static void Main()
{
var renderer = new ChromePdfRenderer();
var pdf = renderer.RenderHtmlAsPdf("<h1>Hello from IronPDF</h1><p>This is HTML content.</p>");
pdf.SaveAs("output.pdf");
}
}// NuGet: Install-Package IronPdf
using IronPdf;
class Program
{
static void Main()
{
var renderer = new ChromePdfRenderer();
var pdf = renderer.RenderHtmlAsPdf("<h1>Hello from IronPDF</h1><p>This is HTML content.</p>");
pdf.SaveAs("output.pdf");
}
}Imports IronPdf
Class Program
Shared Sub Main()
Dim renderer = New ChromePdfRenderer()
Dim pdf = renderer.RenderHtmlAsPdf("<h1>Hello from IronPDF</h1><p>This is HTML content.</p>")
pdf.SaveAs("output.pdf")
End Sub
End ClassImports IronPdf
Class Program
Shared Sub Main()
Dim renderer = New ChromePdfRenderer()
Dim pdf = renderer.RenderHtmlAsPdf("<h1>Hello from IronPDF</h1><p>This is HTML content.</p>")
pdf.SaveAs("output.pdf")
End Sub
End ClassVectSharp要求建立Document, Graphics物件,然後用精確的坐標和字體物件手動定位文字。 IronPDF直接渲染HTML,提供完整的CSS樣式支持。
如需進階 HTML 到 PDF 的場景,請參見 HTML 到 PDF 轉換指南。
建立多頁文件
多頁文件揭示了這些.NET PDF庫之間的架構差異。
VectSharp方法:
// NuGet: Install-Package VectSharp.PDF
using VectSharp;
using VectSharp.PDF;
using System;
class Program
{
static void Main()
{
Document doc = new Document();
FontFamily helvetica =
FontFamily.ResolveFontFamily(FontFamily.StandardFontFamilies.Helvetica);
Font titleFont = new Font(helvetica, 24);
Font bodyFont = new Font(helvetica, 14);
// Page 1
Page page1 = new Page(595, 842);
Graphics g1 = page1.Graphics;
g1.FillText(50, 50, "Page 1", titleFont, Colours.Black);
g1.FillText(50, 100, "First page content", bodyFont, Colours.Black);
doc.Pages.Add(page1);
// Page 2
Page page2 = new Page(595, 842);
Graphics g2 = page2.Graphics;
g2.FillText(50, 50, "Page 2", titleFont, Colours.Black);
g2.FillText(50, 100, "Second page content", bodyFont, Colours.Black);
doc.Pages.Add(page2);
doc.SaveAsPDF("multipage.pdf");
}
}// NuGet: Install-Package VectSharp.PDF
using VectSharp;
using VectSharp.PDF;
using System;
class Program
{
static void Main()
{
Document doc = new Document();
FontFamily helvetica =
FontFamily.ResolveFontFamily(FontFamily.StandardFontFamilies.Helvetica);
Font titleFont = new Font(helvetica, 24);
Font bodyFont = new Font(helvetica, 14);
// Page 1
Page page1 = new Page(595, 842);
Graphics g1 = page1.Graphics;
g1.FillText(50, 50, "Page 1", titleFont, Colours.Black);
g1.FillText(50, 100, "First page content", bodyFont, Colours.Black);
doc.Pages.Add(page1);
// Page 2
Page page2 = new Page(595, 842);
Graphics g2 = page2.Graphics;
g2.FillText(50, 50, "Page 2", titleFont, Colours.Black);
g2.FillText(50, 100, "Second page content", bodyFont, Colours.Black);
doc.Pages.Add(page2);
doc.SaveAsPDF("multipage.pdf");
}
}Imports VectSharp
Imports VectSharp.PDF
Imports System
Class Program
Shared Sub Main()
Dim doc As New Document()
Dim helvetica As FontFamily = FontFamily.ResolveFontFamily(FontFamily.StandardFontFamilies.Helvetica)
Dim titleFont As New Font(helvetica, 24)
Dim bodyFont As New Font(helvetica, 14)
' Page 1
Dim page1 As New Page(595, 842)
Dim g1 As Graphics = page1.Graphics
g1.FillText(50, 50, "Page 1", titleFont, Colours.Black)
g1.FillText(50, 100, "First page content", bodyFont, Colours.Black)
doc.Pages.Add(page1)
' Page 2
Dim page2 As New Page(595, 842)
Dim g2 As Graphics = page2.Graphics
g2.FillText(50, 50, "Page 2", titleFont, Colours.Black)
g2.FillText(50, 100, "Second page content", bodyFont, Colours.Black)
doc.Pages.Add(page2)
doc.SaveAsPDF("multipage.pdf")
End Sub
End ClassImports VectSharp
Imports VectSharp.PDF
Imports System
Class Program
Shared Sub Main()
Dim doc As New Document()
Dim helvetica As FontFamily = FontFamily.ResolveFontFamily(FontFamily.StandardFontFamilies.Helvetica)
Dim titleFont As New Font(helvetica, 24)
Dim bodyFont As New Font(helvetica, 14)
' Page 1
Dim page1 As New Page(595, 842)
Dim g1 As Graphics = page1.Graphics
g1.FillText(50, 50, "Page 1", titleFont, Colours.Black)
g1.FillText(50, 100, "First page content", bodyFont, Colours.Black)
doc.Pages.Add(page1)
' Page 2
Dim page2 As New Page(595, 842)
Dim g2 As Graphics = page2.Graphics
g2.FillText(50, 50, "Page 2", titleFont, Colours.Black)
g2.FillText(50, 100, "Second page content", bodyFont, Colours.Black)
doc.Pages.Add(page2)
doc.SaveAsPDF("multipage.pdf")
End Sub
End ClassIronPDF 方法:
// NuGet: Install-Package IronPdf
using IronPdf;
class Program
{
static void Main()
{
var renderer = new ChromePdfRenderer();
string html = @"
<h1>Page 1</h1>
<p>First page content</p>
<div style='page-break-after: always;'></div>
<h1>Page 2</h1>
<p>Second page content</p>
";
var pdf = renderer.RenderHtmlAsPdf(html);
pdf.SaveAs("multipage.pdf");
}
}// NuGet: Install-Package IronPdf
using IronPdf;
class Program
{
static void Main()
{
var renderer = new ChromePdfRenderer();
string html = @"
<h1>Page 1</h1>
<p>First page content</p>
<div style='page-break-after: always;'></div>
<h1>Page 2</h1>
<p>Second page content</p>
";
var pdf = renderer.RenderHtmlAsPdf(html);
pdf.SaveAs("multipage.pdf");
}
}' NuGet: Install-Package IronPdf
Imports IronPdf
Module Program
Sub Main()
Dim renderer = New ChromePdfRenderer()
Dim html As String = "
<h1>Page 1</h1>
<p>First page content</p>
<div style='page-break-after: always;'></div>
<h1>Page 2</h1>
<p>Second page content</p>
"
Dim pdf = renderer.RenderHtmlAsPdf(html)
pdf.SaveAs("multipage.pdf")
End Sub
End Module' NuGet: Install-Package IronPdf
Imports IronPdf
Module Program
Sub Main()
Dim renderer = New ChromePdfRenderer()
Dim html As String = "
<h1>Page 1</h1>
<p>First page content</p>
<div style='page-break-after: always;'></div>
<h1>Page 2</h1>
<p>Second page content</p>
"
Dim pdf = renderer.RenderHtmlAsPdf(html)
pdf.SaveAs("multipage.pdf")
End Sub
End ModuleVectSharp需要建立單獨的Graphics上下文,並手動定位每個頁面上的每個文字元素,使用座標和字體物件。 IronPDF使用單個HTML字串,並用CSS page-break-after: always自動建立多頁文件。
繪製形狀和文字
圖形能力展示了VectSharp的優勢,但同時也展示了網頁標準如何用更少的程式碼提供等效功能。
VectSharp方法:
// NuGet: Install-Package VectSharp.PDF
using VectSharp;
using VectSharp.PDF;
using System;
class Program
{
static void Main()
{
Document doc = new Document();
Page page = new Page(595, 842);
Graphics graphics = page.Graphics;
// Draw rectangle
graphics.FillRectangle(50, 50, 200, 100, Colour.FromRgb(0, 0, 255));
// Draw circle
GraphicsPath circle = new GraphicsPath();
circle.Arc(400, 100, 50, 0, 2 * Math.PI);
graphics.FillPath(circle, Colour.FromRgb(255, 0, 0));
// Add text - FillText requires a colour parameter
graphics.FillText(50, 200, "VectSharp Graphics",
new Font(FontFamily.ResolveFontFamily(FontFamily.StandardFontFamilies.Helvetica), 20),
Colours.Black);
doc.Pages.Add(page);
doc.SaveAsPDF("shapes.pdf");
}
}// NuGet: Install-Package VectSharp.PDF
using VectSharp;
using VectSharp.PDF;
using System;
class Program
{
static void Main()
{
Document doc = new Document();
Page page = new Page(595, 842);
Graphics graphics = page.Graphics;
// Draw rectangle
graphics.FillRectangle(50, 50, 200, 100, Colour.FromRgb(0, 0, 255));
// Draw circle
GraphicsPath circle = new GraphicsPath();
circle.Arc(400, 100, 50, 0, 2 * Math.PI);
graphics.FillPath(circle, Colour.FromRgb(255, 0, 0));
// Add text - FillText requires a colour parameter
graphics.FillText(50, 200, "VectSharp Graphics",
new Font(FontFamily.ResolveFontFamily(FontFamily.StandardFontFamilies.Helvetica), 20),
Colours.Black);
doc.Pages.Add(page);
doc.SaveAsPDF("shapes.pdf");
}
}' NuGet: Install-Package VectSharp.PDF
Imports VectSharp
Imports VectSharp.PDF
Imports System
Module Program
Sub Main()
Dim doc As New Document()
Dim page As New Page(595, 842)
Dim graphics As Graphics = page.Graphics
' Draw rectangle
graphics.FillRectangle(50, 50, 200, 100, Colour.FromRgb(0, 0, 255))
' Draw circle
Dim circle As New GraphicsPath()
circle.Arc(400, 100, 50, 0, 2 * Math.PI)
graphics.FillPath(circle, Colour.FromRgb(255, 0, 0))
' Add text - FillText requires a colour parameter
graphics.FillText(50, 200, "VectSharp Graphics", New Font(FontFamily.ResolveFontFamily(FontFamily.StandardFontFamilies.Helvetica), 20), Colours.Black)
doc.Pages.Add(page)
doc.SaveAsPDF("shapes.pdf")
End Sub
End Module' NuGet: Install-Package VectSharp.PDF
Imports VectSharp
Imports VectSharp.PDF
Imports System
Module Program
Sub Main()
Dim doc As New Document()
Dim page As New Page(595, 842)
Dim graphics As Graphics = page.Graphics
' Draw rectangle
graphics.FillRectangle(50, 50, 200, 100, Colour.FromRgb(0, 0, 255))
' Draw circle
Dim circle As New GraphicsPath()
circle.Arc(400, 100, 50, 0, 2 * Math.PI)
graphics.FillPath(circle, Colour.FromRgb(255, 0, 0))
' Add text - FillText requires a colour parameter
graphics.FillText(50, 200, "VectSharp Graphics", New Font(FontFamily.ResolveFontFamily(FontFamily.StandardFontFamilies.Helvetica), 20), Colours.Black)
doc.Pages.Add(page)
doc.SaveAsPDF("shapes.pdf")
End Sub
End ModuleIronPDF 方法:
// NuGet: Install-Package IronPdf
using IronPdf;
using IronPdf.Rendering;
class Program
{
static void Main()
{
var renderer = new ChromePdfRenderer();
string html = @"
<div style='width: 200px; height: 100px; background-color: blue; margin: 50px;'></div>
<div style='width: 100px; height: 100px; background-color: red;
border-radius: 50%; margin-left: 350px; margin-top: -50px;'></div>
<h2 style='margin-left: 50px;'>IronPDF Graphics</h2>
";
var pdf = renderer.RenderHtmlAsPdf(html);
pdf.SaveAs("shapes.pdf");
}
}// NuGet: Install-Package IronPdf
using IronPdf;
using IronPdf.Rendering;
class Program
{
static void Main()
{
var renderer = new ChromePdfRenderer();
string html = @"
<div style='width: 200px; height: 100px; background-color: blue; margin: 50px;'></div>
<div style='width: 100px; height: 100px; background-color: red;
border-radius: 50%; margin-left: 350px; margin-top: -50px;'></div>
<h2 style='margin-left: 50px;'>IronPDF Graphics</h2>
";
var pdf = renderer.RenderHtmlAsPdf(html);
pdf.SaveAs("shapes.pdf");
}
}' NuGet: Install-Package IronPdf
Imports IronPdf
Imports IronPdf.Rendering
Module Program
Sub Main()
Dim renderer As New ChromePdfRenderer()
Dim html As String = "
<div style='width: 200px; height: 100px; background-color: blue; margin: 50px;'></div>
<div style='width: 100px; height: 100px; background-color: red;
border-radius: 50%; margin-left: 350px; margin-top: -50px;'></div>
<h2 style='margin-left: 50px;'>IronPDF Graphics</h2>
"
Dim pdf = renderer.RenderHtmlAsPdf(html)
pdf.SaveAs("shapes.pdf")
End Sub
End Module' NuGet: Install-Package IronPdf
Imports IronPdf
Imports IronPdf.Rendering
Module Program
Sub Main()
Dim renderer As New ChromePdfRenderer()
Dim html As String = "
<div style='width: 200px; height: 100px; background-color: blue; margin: 50px;'></div>
<div style='width: 100px; height: 100px; background-color: red;
border-radius: 50%; margin-left: 350px; margin-top: -50px;'></div>
<h2 style='margin-left: 50px;'>IronPDF Graphics</h2>
"
Dim pdf = renderer.RenderHtmlAsPdf(html)
pdf.SaveAs("shapes.pdf")
End Sub
End ModuleVectSharp需要建立Colour.FromRgb()管理顏色。 IronPDF使用熟悉的CSS屬性:background-color, border-radius: 50%用於圓形,以及標準的邊距。
從VectSharp API到IronPDF的映射參考
此映射透過展示直接的 API 等價加速遷移:
| VectSharp | IronPDF |
|---|---|
Document | ChromePdfRenderer |
Page | 自動 |
Graphics | HTML/CSS |
graphics.FillRectangle() | CSS background-color on <div> |
graphics.StrokeRectangle() | CSS border on <div> |
graphics.FillText() | HTML文字元素 |
graphics.StrokePath() | SVG或CSS邊框 |
GraphicsPath | SVG <path> 元素 |
Colour.FromRgb() | CSS顏色值 |
Font / FontFamily | CSS font-family |
doc.SaveAsPDF() | pdf.SaveAs() |
| 手動頁面尺寸設置 | RenderingOptions.PaperSize |
遷移策略
策略1:將繪圖程式碼轉換為HTML/CSS
使用HTML元素替換基於座標的繪圖:
// VectSharp
graphics.FillRectangle(100, 50, 300, 80, Colour.FromRgb(0, 102, 204));
graphics.FillText(110, 80, "Header", font, Colours.White);
//IronPDFHTML equivalent
<div style="
position: absolute;
left: 100px;
top: 50px;
width: 300px;
height: 80px;
background: rgb(0, 102, 204);
color: white;
padding: 10px;
">Header</div>// VectSharp
graphics.FillRectangle(100, 50, 300, 80, Colour.FromRgb(0, 102, 204));
graphics.FillText(110, 80, "Header", font, Colours.White);
//IronPDFHTML equivalent
<div style="
position: absolute;
left: 100px;
top: 50px;
width: 300px;
height: 80px;
background: rgb(0, 102, 204);
color: white;
padding: 10px;
">Header</div>' VectSharp
graphics.FillRectangle(100, 50, 300, 80, Colour.FromRgb(0, 102, 204))
graphics.FillText(110, 80, "Header", font, Colours.White)
' IronPDFHTML equivalent
'<div style="
' position: absolute;
' left: 100px;
' top: 50px;
' width: 300px;
' height: 80px;
' background: rgb(0, 102, 204);
' color: white;
' padding: 10px;
'">Header</div>' VectSharp
graphics.FillRectangle(100, 50, 300, 80, Colour.FromRgb(0, 102, 204))
graphics.FillText(110, 80, "Header", font, Colours.White)
' IronPDFHTML equivalent
'<div style="
' position: absolute;
' left: 100px;
' top: 50px;
' width: 300px;
' height: 80px;
' background: rgb(0, 102, 204);
' color: white;
' padding: 10px;
'">Header</div>策略2:使用SVG作為向量圖形
對於複雜的形狀,在您的HTML中使用內嵌SVG:
// VectSharp path
GraphicsPath path = new GraphicsPath();
path.MoveTo(100, 100);
path.LineTo(200, 50);
path.LineTo(300, 100);
path.Close();
graphics.FillPath(path, Colours.Blue);
//IronPDFSVG equivalent
<svg><polygon points="100,100 200,50 300,100" fill="blue"/></svg>// VectSharp path
GraphicsPath path = new GraphicsPath();
path.MoveTo(100, 100);
path.LineTo(200, 50);
path.LineTo(300, 100);
path.Close();
graphics.FillPath(path, Colours.Blue);
//IronPDFSVG equivalent
<svg><polygon points="100,100 200,50 300,100" fill="blue"/></svg>' VectSharp path
Dim path As New GraphicsPath()
path.MoveTo(100, 100)
path.LineTo(200, 50)
path.LineTo(300, 100)
path.Close()
graphics.FillPath(path, Colours.Blue)
'IronPDFSVG equivalent
<svg><polygon points="100,100 200,50 300,100" fill="blue"/></svg>' VectSharp path
Dim path As New GraphicsPath()
path.MoveTo(100, 100)
path.LineTo(200, 50)
path.LineTo(300, 100)
path.Close()
graphics.FillPath(path, Colours.Blue)
'IronPDFSVG equivalent
<svg><polygon points="100,100 200,50 300,100" fill="blue"/></svg>策略3:使用JavaScript圖表函式庫
對於科學可視化 - VectSharp的專長 - IronPDF可以利用強大的JavaScript庫,例如Chart.js、D3.js或Plotly:
var html = @"
<script src='https://cdn.plot.ly/plotly-latest.min.js'></script>
<div id='chart'></div>
<script>
Plotly.newPlot('chart', [{
x: [1, 2, 3, 4],
y: [10, 15, 13, 17],
type: 'scatter'
}]);
</script>";
var renderer = new ChromePdfRenderer();
renderer.RenderingOptions.EnableJavaScript = true;
var pdf = renderer.RenderHtmlAsPdf(html);var html = @"
<script src='https://cdn.plot.ly/plotly-latest.min.js'></script>
<div id='chart'></div>
<script>
Plotly.newPlot('chart', [{
x: [1, 2, 3, 4],
y: [10, 15, 13, 17],
type: 'scatter'
}]);
</script>";
var renderer = new ChromePdfRenderer();
renderer.RenderingOptions.EnableJavaScript = true;
var pdf = renderer.RenderHtmlAsPdf(html);Dim html As String = "
<script src='https://cdn.plot.ly/plotly-latest.min.js'></script>
<div id='chart'></div>
<script>
Plotly.newPlot('chart', [{
x: [1, 2, 3, 4],
y: [10, 15, 13, 17],
type: 'scatter'
}]);
</script>"
Dim renderer As New ChromePdfRenderer()
renderer.RenderingOptions.EnableJavaScript = True
Dim pdf = renderer.RenderHtmlAsPdf(html)Dim html As String = "
<script src='https://cdn.plot.ly/plotly-latest.min.js'></script>
<div id='chart'></div>
<script>
Plotly.newPlot('chart', [{
x: [1, 2, 3, 4],
y: [10, 15, 13, 17],
type: 'scatter'
}]);
</script>"
Dim renderer As New ChromePdfRenderer()
renderer.RenderingOptions.EnableJavaScript = True
Dim pdf = renderer.RenderHtmlAsPdf(html)常見遷移問題及解決方案
問題1:座標系統差異
VectSharp 使用從左上角開始的點,需手動定位。
解決方案: 使用CSS定位:
.element {
position: absolute;
top: 50px;
left: 100px;
}
問題2:字體物件
VectSharp 以程式方式建立FontFamily物件。
解決方案: 使用CSS字體族:
<style>
body { font-family: Arial, sans-serif; font-size: 12pt; }
</style>
問題3:顏色處理
VectSharp 使用Colour.FromRgb()方法調用。
解決方案: 使用CSS顏色:
.header { color: rgb(0, 102, 204); background-color: #f0f0f0; }
問題4:圖形路徑
VectSharp 使用複雜的GraphicsPath API及MoveTo, LineTo, Arc方法。
解決方案: 使用SVG作為向量圖形:
<svg>
<path d="M 100 100 L 200 50 L 300 100 Z" fill="blue"/>
</svg>
VectSharp遷移檢查清單
遷移前任務
審核您的程式碼庫以識別所有VectSharp使用情況:
grep -r "using VectSharp" --include="*.cs" .
grep -r "Graphics\|FillRectangle\|FillText" --include="*.cs" .
記錄頁面尺寸(new Page(595, 842)模式)。 註明使用Colour.FromRgb()的顏色方案。 確定字體設置。 使用GraphicsPath識別複雜的向量圖進行SVG轉換。
程式碼更新任務
- 移除VectSharp NuGet包
- 安裝IronPDFNuGet 套件
- 將using語句從
IronPdf - 將
background-color的CSS框 - 將
FillText調用轉換為帶有CSS樣式的HTML文字元素 - 將
GraphicsPath操作轉換為SVG<path>元素 - 用
RenderingOptions.PaperSize取代手動頁面管理 - 在啟動時新增授權初始化
遷移後測試
遷移後,驗證以下方面:
- 比較VectSharp與IronPDF版本之間的視覺輸出
- 驗證顏色使用
Colour.FromRgb()值的CSS等效項以匹配 - 檢查從基於座標的放置轉換的元素的定位準確性
- 測試多頁文件的分頁
- 驗證通過SVG正確渲染的向量圖形
遷移到IronPDF的關鍵好處
從VectSharp轉到IronPDF為文件生成提供了多個優勢:
基於HTML的內容: 網頁開發者可以利用現有的HTML和CSS技能。 不需要學習基於座標的繪圖API。
自動佈局: 文字換行、分頁和流佈局自動進行。 不需要手算元素位置。
現代CSS支持: 完整的CSS3包括Flexbox和網格佈局。 響應式設計直接轉化為PDF。
JavaScript執行: 使用Chart.js、D3.js或Plotly的互動式圖表可以正確渲染。 動態內容如預期運行。
URL轉PDF: 捕捉任何網頁為PDF - 這是VectSharp無法實現的功能。
PDF操作: 合併、分割、新增水印、密碼保護和數位簽名都是內建功能。
更低的程式碼冗長性: HTML/CSS是陳述性的且易讀。 相同的文件需要比VectSharp的命令式繪圖方法更少的程式碼。
主動開發: IronPDF的定期更新保證與當前和未來.NET版本的相容性。
Curtis Chau擁有Carleton大學的電腦科學學士學位,專精於前端開發,擁有Node.js、TypeScript、JavaScript和React的專業知識。Curtis熱衷於建立直觀且美觀的使用者介面,喜愛使用現代框架並建立結構良好、視覺吸引力的手冊。
相關文章


