如何在 C# 中從 VectSharp 轉換到 IronPDF
VectSharp 已經在 .NET 生態系統中建立了自己作為一個有能力的向量圖形庫的地位,特別是在科學視覺化和技術插圖方面。 然而,當開發團隊需要產生商業文件、報告、發票或任何以 HTML 為基礎的內容時,VectSharp 圖形先行的範例就會產生顯著的摩擦。 該資料庫是專為製作圖表和圖形的科學家所設計,而非為製作文件的開發人員所設計。
本指南提供了從VectSharp到IronPDF的完整轉換路徑,並提供逐步說明、程式碼比較以及實用範例,可供評估此轉換的 .NET 專業開發人員使用。
為何要從VectSharp轉移過來?
VectSharp 是一個科學視覺化和向量圖形函式庫,設計用於建立圖表、圖表和技術插圖。 它並非設計用來產生文件-它只是一個繪圖庫,碰巧可以輸出 PDF。 開發團隊考慮遷移的主要原因包括
僅限科學用途:VectSharp專為資料視覺化和繪圖而設計,不適用於發票、報告或證書等商業文件。
不支援HTML: VectSharp無法將HTML或CSS轉換為PDF。 每個元素都必須使用向量圖形指令手繪。
基於座標的 API:每個元素都必須使用精確的 X、Y 座標進行定位。 沒有自動排版、流程或文字包裝。
不使用 CSS 樣式:所有樣式均透過方法呼叫以程式設計方式實現。 網頁開發人員不能利用他們現有的 CSS 知識。
不支援 JavaScript:VectSharp無法渲染動態網頁內容、互動式圖表或基於 JavaScript 的視覺化效果。
無文字版面:不支援自動文字換行、分頁和串流版面配置。 開發人員必須手動計算文字位置和分頁符號。
圖形優先範式:該庫是為圖表而設計的,而不是為報告或發票設計的。 文件生成需要大量的手動工作。
核心問題:圖形庫 vs 文件產生器
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(new FontFamily("Arial"), 20), Colours.White);
// ... continue drawing every single element manually
// 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(new FontFamily("Arial"), 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(New FontFamily("Arial"), 20), Colours.White)
' ... continue drawing every single element manually
IronPDF 使用 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)
IronPDFvs VectSharp:功能比較
了解架構上的差異有助於技術決策者評估遷移投資:
| 特點 | VectSharp | IronPDF |
|---|---|---|
| 主要用途 | 向量圖形 | 文件製作 |
| PDF輸出 | 是 | 是 |
| HTML 支援 | 無 | 是 |
| 授權 | LGPL | 商業的 |
| 開放原始碼 | 是 | 部分(商業功能) |
| 最適合 | 科學視覺化 | 一般 PDF 文件 |
| 客製化 | 限於圖形 | 廣泛的文件相關 |
| HTML至PDF | 無 | 完整的 Chromium |
| URL轉PDF | 無 | 是 |
| CSS 支援 | 無 | 完整的 CSS3 |
| JavaScript。 | 無 | 完整的 ES2024 |
| 自動排版 | 無 | 是 |
| 自動分頁 | 無 | 是 |
| 文字包裝 | 手動的 | 自動化 |
| 合併 PDF 文件 | 無 | 是 |
| 分割 PDFs | 無 | 是 |
| 密碼保護 | 無 | 是 |
| 數位簽名 | 無 | 是 |
| 學習曲線 | 高(坐標) | 低 (HTML/CSS) |
| 程式語言 | 非常高 | 低 |
快速入門:VectSharp 到IronPDF的轉換。
只要完成這些基本步驟,就可以立即開始遷移。
步驟 1:取代 NuGet 套件
移除所有VectSharp套件:
# RemoveVectSharppackages
dotnet remove package VectSharp
dotnet remove package VectSharp.PDF
# RemoveVectSharppackages
dotnet remove package VectSharp
dotnet remove package VectSharp.PDF
安裝 IronPDF:
# Install IronPDF
dotnet add package 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 IronPdf
步驟 3:初始化授權
在應用程式啟動時加入授權初始化:
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()
{
//VectSharpdoesn'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));
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()
{
//VectSharpdoesn'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));
doc.Pages.Add(page);
doc.SaveAsPDF("output.pdf");
}
}
Imports VectSharp
Imports VectSharp.PDF
Imports VectSharp.SVG
Imports System.IO
Module Program
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))
doc.Pages.Add(page)
doc.SaveAsPDF("output.pdf")
End Sub
End Module
IronPDF 方法:
// 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 Class
VectSharp 需要建立 Document、Page 和 Graphics 對象,然後手動使用精確座標和字體對象定位文字。IronPDF可直接渲染 HTML,並支援完整的 CSS 定義。
如需進階的 HTML 至IronPDF情境,請參閱 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();
// Page 1
Page page1 = new Page(595, 842);
Graphics g1 = page1.Graphics;
g1.FillText(50, 50, "Page 1",
new Font(FontFamily.ResolveFontFamily(FontFamily.StandardFontFamilies.Helvetica), 24));
g1.FillText(50, 100, "First page content",
new Font(FontFamily.ResolveFontFamily(FontFamily.StandardFontFamilies.Helvetica), 14));
doc.Pages.Add(page1);
// Page 2
Page page2 = new Page(595, 842);
Graphics g2 = page2.Graphics;
g2.FillText(50, 50, "Page 2",
new Font(FontFamily.ResolveFontFamily(FontFamily.StandardFontFamilies.Helvetica), 24));
g2.FillText(50, 100, "Second page content",
new Font(FontFamily.ResolveFontFamily(FontFamily.StandardFontFamilies.Helvetica), 14));
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();
// Page 1
Page page1 = new Page(595, 842);
Graphics g1 = page1.Graphics;
g1.FillText(50, 50, "Page 1",
new Font(FontFamily.ResolveFontFamily(FontFamily.StandardFontFamilies.Helvetica), 24));
g1.FillText(50, 100, "First page content",
new Font(FontFamily.ResolveFontFamily(FontFamily.StandardFontFamilies.Helvetica), 14));
doc.Pages.Add(page1);
// Page 2
Page page2 = new Page(595, 842);
Graphics g2 = page2.Graphics;
g2.FillText(50, 50, "Page 2",
new Font(FontFamily.ResolveFontFamily(FontFamily.StandardFontFamilies.Helvetica), 24));
g2.FillText(50, 100, "Second page content",
new Font(FontFamily.ResolveFontFamily(FontFamily.StandardFontFamilies.Helvetica), 14));
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()
' Page 1
Dim page1 As New Page(595, 842)
Dim g1 As Graphics = page1.Graphics
g1.FillText(50, 50, "Page 1", New Font(FontFamily.ResolveFontFamily(FontFamily.StandardFontFamilies.Helvetica), 24))
g1.FillText(50, 100, "First page content", New Font(FontFamily.ResolveFontFamily(FontFamily.StandardFontFamilies.Helvetica), 14))
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", New Font(FontFamily.ResolveFontFamily(FontFamily.StandardFontFamilies.Helvetica), 24))
g2.FillText(50, 100, "Second page content", New Font(FontFamily.ResolveFontFamily(FontFamily.StandardFontFamilies.Helvetica), 14))
doc.Pages.Add(page2)
doc.SaveAsPDF("multipage.pdf")
End Sub
End Class
IronPDF 方法:
// 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");
}
}
Imports IronPdf
Class Program
Shared 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 Class
VectSharp 需要建立單獨的 Page 物件、單獨的 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
graphics.FillText(50, 200, "VectSharp Graphics",
new Font(FontFamily.ResolveFontFamily(FontFamily.StandardFontFamilies.Helvetica), 20));
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
graphics.FillText(50, 200, "VectSharp Graphics",
new Font(FontFamily.ResolveFontFamily(FontFamily.StandardFontFamilies.Helvetica), 20));
doc.Pages.Add(page);
doc.SaveAsPDF("shapes.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
graphics.FillText(50, 200, "VectSharp Graphics",
New Font(FontFamily.ResolveFontFamily(FontFamily.StandardFontFamilies.Helvetica), 20))
doc.Pages.Add(page)
doc.SaveAsPDF("shapes.pdf")
End Sub
End Module
IronPDF 方法:
// 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");
}
}
Imports IronPdf
Imports IronPdf.Rendering
Class Program
Shared 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 Class
VectSharp 需要建立 GraphicsPath 對象,使用精確的數學參數呼叫 Arc(),並透過 Colour.FromRgb() 管理顏色。IronPDF使用熟悉的 CSS 屬性:background-color、border-radius: 50% 用於圓形,以及標準邊距。
VectSharpAPI 到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>
策略 2:使用 SVG 製作向量圖形
對於複雜的圖形,請在 HTML 中使用內嵌 SVG:
//VectSharppath
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>
//VectSharppath
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>
Imports System.Drawing.Drawing2D
'VectSharppath
Dim path As New GraphicsPath()
path.StartFigure()
path.AddLine(100, 100, 200, 50)
path.AddLine(200, 50, 300, 100)
path.CloseFigure()
graphics.FillPath(Brushes.Blue, path)
'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)
常見的遷移問題與解決方案
問題 1:座標系統差異
VectSharp 使用從左上方原點開始的點來手動定位。
解決方案:使用 CSS 定位:
.element {
position: absolute;
top: 50px;
left: 100px;
}
第 2 期:字型物件
VectSharp以程式設計方式建立 Font 和 FontFamily 物件。
解決方案:使用 CSS font-family:
<style>
body { font-family: Arial, sans-serif; font-size: 12pt; }
</style>
<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>
<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" .
grep -r "using VectSharp" --include="*.cs" .
grep -r "Graphics\|FillRectangle\|FillText" --include="*.cs" .
文件頁尺寸(new Page(595, 842) 模式)。 注意使用 Colour.FromRgb() 的顏色方案。 識別字型配置。 使用 GraphicsPath 進行 SVG 轉換,映射複雜的向量圖形。
程式碼更新任務
1.移除VectSharpNuGet 套件 2.安裝IronPDFNuGet 套件
- 使用語句從
VectSharp更新至IronPdf - 將
FillRectangle呼叫轉換為 CSS 框,使用background-color - 將
FillText呼叫轉換為帶有 CSS 樣式的 HTML 文字元素 - 將操作轉換為 SVG 元素
- 將手動頁面管理替換為
RenderingOptions.PaperSize8.在啟動時加入授權初始化
後遷移測試
轉移後,驗證這些方面:
- 比較VectSharp和IronPDF版本之間的視覺輸出
- 使用 CSS 等效值驗證顏色是否符合
Colour.FromRgb()值 - 檢查從基於座標的位置轉換過來的元素的定位精度
- 測試多頁文件的分頁
- 驗證向量圖形透過 SVG 正確呈現
遷移到IronPDF的主要優點。
從VectSharp轉換到IronPDF為文件生成提供了多項優勢:
基於 HTML 的內容: Web 開發人員可以利用現有的 HTML 和 CSS 技能。 不需要學習基於座標的繪圖 API。
自動版面:文字換行、分頁和串流版面配置自動完成。 不需要手動計算元素位置。
現代 CSS 支援:完全支援 CSS3,包括 Flexbox 和 Grid 佈局。 回應式設計可直接翻譯為 PDF。
JavaScript 執行:使用 Chart.js、D3.js 或 Plotly 的互動式圖表能夠正確渲染。 動態內容如預期般運作。
URL 轉 PDF:將任何網頁捕獲為 PDF——VectSharp 無法實現此功能。
PDF 操作:合併、分割、新增浮水印、密碼保護和數位簽章均為內建功能。
降低程式碼冗餘長度:HTML/CSS是聲明式的,可讀性強。 與VectSharp的命令式繪圖方式相比,同一份文件所需的程式碼顯著減少。
積極開發:隨著 .NET 10 和 C# 14 的普及,IronPDF 將持續更新,確保與目前和未來的 .NET 版本相容。

