跳至頁尾內容
移民指南

如何在 C# 中將 VectSharp 遷移到 IronPDF

VectSharp 已成為 .NET 生態系統中強大的向量圖庫,尤其在科學視覺化和技術插圖方面備受青睞。 然而,當開發團隊需要產生業務文件、報告、發票或任何基於 HTML 的內容時,VectSharp 的圖形優先範式會造成很大的摩擦。 該庫是為創建圖表的科學家設計的,而不是為生成文件的開發人員設計的。

本指南提供了從 VectSharp 到 IronPDF 的完整遷移路徑,包括逐步說明、程式碼比較和實用範例,供正在評估此過渡的專業 .NET 開發人員參考。

為什麼要從 VectSharp 遷移?

VectSharp 是一個科學視覺化和向量圖形庫,旨在創建圖表、圖形和技術插圖。 它並非設計用於生成文件——它是一個繪圖庫,恰好可以輸出 PDF 文件。 開發團隊考慮遷移的主要原因包括:

僅限科學用途: VectSharp 專為資料視覺化和繪圖而設計,不適用於發票、報告或證書等商業文件。

不支援HTML: VectSharp無法將HTML或CSS轉換為PDF。 每個元素都必須使用向量圖形命令手動繪製。

基於座標的 API:每個元素都必須使用精確的 X、Y 座標進行定位。 沒有自動版面配置、排版或文字換行。

不使用 CSS 樣式:所有樣式均透過方法呼叫以程式設計方式實現。 Web開發人員無法利用他們現有的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(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
$vbLabelText   $csharpLabel

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);
$vbLabelText   $csharpLabel

IronPDF 與 VectSharp:功能對比

了解架構差異有助於技術決策者評估遷移投資:

特徵VectSharpIronPDF
主要用途向量圖形文件創建
PDF 輸出是的是的
HTML 支援是的
授權LGPL商業的
開源是的部分(商業功能)
最適合科學視覺化一般PDF文檔
客製化限圖形內容廣泛,與文件相關
HTML 轉 PDF全鉻
PDF檔案的URL是的
CSS 支援完整的 CSS3
JavaScript完整版 ES2024
自動佈局是的
自動分頁符是的
文字換行手動的自動的
合併PDF是的
拆分PDF是的
密碼保護是的
數位簽名是的
學習曲線高(座標)低(HTML/CSS)
程式碼冗長性非常高低的

快速入門:VectSharp 到 IronPDF 的遷移

透過這些基礎步驟,遷移工作可以立即開始。

步驟 1:替換 NuGet 套件

移除所有 VectSharp 套件:

# Remove VectSharp packages
dotnet remove package VectSharp
dotnet remove package VectSharp.PDF
# Remove VectSharp packages
dotnet remove package VectSharp
dotnet remove package VectSharp.PDF
SHELL

安裝 IronPDF:

# Install IronPDF
dotnet add package IronPdf
# Install IronPDF
dotnet add package IronPdf
SHELL

步驟 2:更新命名空間

將 VectSharp 命名空間替換為 IronPdf 命名空間:

// Before (VectSharp)
using VectSharp;
using VectSharp.PDF;

// After (IronPDF)
using IronPdf;
// Before (VectSharp)
using VectSharp;
using VectSharp.PDF;

// After (IronPDF)
using IronPdf;
$vbLabelText   $csharpLabel

步驟 3:初始化許可證

在應用程式啟動時新增許可證初始化:

IronPdf.License.LicenseKey = "YOUR-LICENSE-KEY";
IronPdf.License.LicenseKey = "YOUR-LICENSE-KEY";
$vbLabelText   $csharpLabel

程式碼遷移範例

將 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));

        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));

        doc.Pages.Add(page);
        doc.SaveAsPDF("output.pdf");
    }
}
$vbLabelText   $csharpLabel

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");
    }
}
$vbLabelText   $csharpLabel

VectSharp 需要建立DocumentPageGraphics對象,然後手動使用精確座標和字體對象定位文字。 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();

        // 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");
    }
}
$vbLabelText   $csharpLabel

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");
    }
}
$vbLabelText   $csharpLabel

VectSharp 需要為每個頁面建立單獨的Page物件、單獨的Graphics上下文,並手動為每個頁面定位每個文字元素及其座標和字體物件。 IronPDF 使用單一 HTML 字串,並結合 CSS page-break-after: always來自動建立多頁文件。

繪製形狀和文字

圖形功能展現了 VectSharp 的優勢所在,但也暴露了 Web 標準在提供同等功能的同時,程式碼量卻更少。

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");
    }
}
$vbLabelText   $csharpLabel

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");
    }
}
$vbLabelText   $csharpLabel

VectSharp 需要建立GraphicsPath對象,使用精確的數學參數呼叫Arc() ,並透過Colour.FromRgb()管理顏色。 IronPDF 使用常見的 CSS 屬性: background-colorborder-radius: 50% (用於圓形)和標準邊距。

VectSharp API 到 IronPDF 映射參考

此映射透過顯示直接的 API 等效項來加速遷移:

VectSharpIronPDF筆記
DocumentChromePdfRenderer建立渲染器
Page自動的由 HTML 建立的頁面
GraphicsHTML/CSS聲明式標記
graphics.FillRectangle()CSS background-color on<div>HTML 框
graphics.StrokeRectangle()CSS border``<div>邊界
graphics.FillText()HTML 文字元素<p><h1><span>
graphics.StrokePath()SVG 或 CSS 邊框向量路徑
GraphicsPathSVG<path>元素複雜形狀
Colour.FromRgb()CSS顏色值rgb()#hex ,命名
Font / FontFamilyCSS font-family支援的網頁字體
doc.SaveAsPDF()pdf.SaveAs()儲存到文件
手動調整頁面尺寸RenderingOptions.PaperSize或者 CSS @page

移民策略

策略一:將繪圖程式碼轉換為 HTML/CSS

用HTML元素取代基於座標的繪圖:

// VectSharp
graphics.FillRectangle(100, 50, 300, 80, Colour.FromRgb(0, 102, 204));
graphics.FillText(110, 80, "Header", font, Colours.White);

// IronPDF HTML 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);

// IronPDF HTML equivalent
<div style="
    position: absolute;
    left: 100px;
    top: 50px;
    width: 300px;
    height: 80px;
    background: rgb(0, 102, 204);
    color: white;
    padding: 10px;
">Header</div>
$vbLabelText   $csharpLabel

策略二:使用 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);

// IronPDF SVG 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);

// IronPDF SVG equivalent
<svg><polygon points="100,100 200,50 300,100" fill="blue"/></svg>
$vbLabelText   $csharpLabel

策略三:使用 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);
$vbLabelText   $csharpLabel

常見遷移問題及解決方案

問題一:座標系差異

VectSharp使用從左上角原點出發的點,並進行手動定位。

解決方案:使用 CSS 定位:

.element {
    position: absolute;
    top: 50px;
    left: 100px;
}

第二期:字體對象

VectSharp以程式設計方式建立FontFontFamily物件。

解決方法:使用 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>
HTML

第三期:色彩處理

VectSharp使用Colour.FromRgb()方法呼叫。

解決方法:使用 CSS 顏色:

.header { color: rgb(0, 102, 204); background-color: #f0f0f0; }

問題 4:圖形路徑

VectSharp使用複雜的GraphicsPath API,其中包含MoveToLineToArc方法。

解決方案:使用 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>
HTML

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" .
SHELL

文件頁面尺寸( new Page(595, 842)模式)。 注意使用Colour.FromRgb()設定的顏色方案。 識別字體配置。 使用GraphicsPath映射複雜的向量圖形以進行 SVG 轉換。

程式碼更新任務

  1. 刪除 VectSharp NuGet 套件
  2. 安裝 IronPdf NuGet 套件
  3. 使用VectSharpIronPdf語句進行更新
  4. FillRectangle呼叫轉換為帶有background-color的 CSS 方塊
  5. FillText呼叫轉換為帶有 CSS 樣式的 HTML 文字元素
  6. GraphicsPath操作轉換為 SVG<path>元素
  7. 使用RenderingOptions.PaperSize替換手動頁面管理
  8. 新增啟動時許可證初始化功能

遷移後測試

遷移完成後,請確認以下幾個面向:

  • 比較 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 版本相容。

柯蒂斯·週
技術撰稿人

Curtis Chau擁有卡爾頓大學電腦科學學士學位,專長於前端開發,精通Node.js、TypeScript、JavaScript和React。他熱衷於打造直覺美觀的使用者介面,喜歡使用現代框架,並擅長撰寫結構清晰、視覺效果出色的使用者手冊。

除了開發工作之外,柯蒂斯對物聯網 (IoT) 也抱有濃厚的興趣,致力於探索硬體和軟體整合的創新方法。閒暇時,他喜歡玩遊戲和製作 Discord 機器人,將他對科技的熱愛與創造力結合。