
如何用 C# | IronPDF 将 HTML ZIP 文件渲染为 PDF
VectSharp(由Giorgio Bianchini创建,arklumpus/VectSharp)是一个轻量级跨平台向量图形库,为 .NET Standard 2.0 设计,常用于科学可视化和技术插图。 当开发团队需要生成业务文档、报告、发票或基于HTML的内容时,VectSharp的图形优先范式会引起摩擦。 它是为图形和绘图设计的,而不是用于生成文档。
本指南提供了从VectSharp到IronPDF的完整迁移路径,为评估这一过渡的专业 .NET 开发人员提供了分步说明、代码比较和实用示例。
为什么要从VectSharp迁移
VectSharp 是一个矢量图形库,专为创建图表、图形和技术插图而设计,具有可插拔的输出层,包括 VectSharp.SVG 和 VectSharp.Raster。 它不是为文档生成而设计的——它是一个绘图库,其 VectSharp.PDF 包增加了 SaveAsPDF 扩展方法于 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) |
| 代码准确性 | 极高 | low |
快速入门:VectSharp 到IronPDF的迁移
迁移工作可以通过以下基本步骤立即开始。
步骤 1:替换 NuGet 软件包
删除所有VectSharp软件包:
# RemoveVectSharppackages (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()
{
//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),
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()
{
//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),
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()
'VectSharpdoesn'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()
'VectSharpdoesn'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 需要创建一个 Page 和 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 需要创建单独的 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 - 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 需要创建 GraphicsPath 对象,调用具有精确数学参数的 Arc(),并通过 Colour.FromRgb() 管理颜色。IronPDF使用熟悉的 CSS 属性: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> element |
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:
//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>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)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 字体-family:
<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,其中包含 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 转换。
代码更新任务
1.删除VectSharpNuGet 软件包
2. 安装IronPDF NuGet包
3. 更新使用声明从 VectSharp 到 IronPdf
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的命令式绘图方法相比,同一文档所需的代码量要少得多。
活跃开发:IronPDF的定期更新确保与当前和未来.NET版本的兼容性。
Curtis Chau 拥有卡尔顿大学的计算机科学学士学位,专注于前端开发,精通 Node.js、TypeScript、JavaScript 和 React。他热衷于打造直观且美观的用户界面,喜欢使用现代框架并创建结构良好、视觉吸引力强的手册。
相关文章


