
C#でHTML ZIPファイルをPDFにレンダリングする方法|IronPDF
VectSharp(Giorgio Bianchini氏、arklumpus/VectSharp)は、科学的な視覚化や技術的なイラストレーションによく使用される.NET Standard 2.0用の軽量クロスプラットフォームベクターグラフィックライブラリです。 開発チームがビジネス文書、レポート、請求書、またはHTMLベースのコンテンツを生成する必要があるとき、VectSharpのグラフィック優先パラダイムは摩擦を生じます。 それは図やプロット用に設計されており、ドキュメント生成用ではありません。
このガイドでは、VectSharpからIronPDFへの完全な移行経路を、移行を検討しているプロ for .NET開発者のために、ステップバイステップの手順、コード比較、実用的な例とともに提供します。
なぜVectSharpから移行するのか
VectSharpは、VectSharp.Rasterを含むプラグイン可能な出力レイヤーを備えた、図、チャート、技術イラストレーションの作成用に設計されたベクターグラフィックスライブラリです。 これはドキュメント生成を目的としたものではありません。Documentに追加する描画ライブラリです。 開発チームが移行を検討する主な理由は次のとおりです:
科学的な焦点のみ:ベクトシャープは、請求書、レポート、証明書などのビジネス文書ではなく、データの視覚化とプロット用に設計されています。
HTML サポートなし:ベクトシャープは HTML または CSS を PDF に変換できません。 すべての要素は、ベクトルグラフィックコマンドを使用して手動で描画する必要があります。
座標ベースの API:すべての要素は正確な X、Y 座標で配置する必要があります。 自動レイアウト、フロー、テキストの折り返しはありません。
CSS スタイル設定なし:すべてのスタイル設定はメソッド呼び出しを通じてプログラムで行われます。 ウェブ開発者は、既存のCSSの知識を活用することはできません。
JavaScriptなし:ベクトシャープは、動的な Web コンテンツ、インタラクティブなグラフ、または 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(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の比較:機能比較
アーキテクチャの違いを理解することは、技術的な意思決定者が移行への投資を評価するのに役立ちます:
| フィーチャー | ベクトシャープ | IronPDF |
|---|---|---|
| 主な用途 | ベクターグラフィックス | ドキュメントの作成 |
| PDF出力 | はい | はい |
| HTMLのサポート。 | なし | はい |
| ライセンスについて | LGPL-3.0-only | 商用 |
| オープンソース。 | はい | 一部(商用機能) |
| 最適な対象 | 科学的視覚化 | 一般的なPDF文書 |
| カスタマイズ。 | グラフィック限定 | 広範なドキュメント関連 |
| HTMLからPDFへ | なし | 完全なChromium |
| URLからPDFへ。 | なし | はい |
| CSSサポート | なし | 完全なCSS3 |
| JavaScript(JavaScript | なし | フルES2024 |
| 自動レイアウト。 | なし | はい |
| 自動改ページ | なし | はい |
| テキストラッピング。 | マニュアル | 自動翻訳 |
| PDFをマージする。 | なし | はい |
| PDFを分割する。 | なし | はい |
| パスワード保護 | なし | はい |
| デジタル署名。 | なし | はい |
| ラーニングカーブ | 高(座標) | 低レベル(HTML/CSS) |
| コードの正確さ | 非常に高い | 低レベル |
クイックスタートVectSharpからIronPDFへの移行
これらの基本的なステップを踏めば、すぐにでも移行を開始できます。
ステップ 1: NuGet パッケージを置き換える
すべてのVectSharpパッケージを削除してください:
# Removeベクトシャープpackages (PDF first, then core)
dotnet remove package VectSharp.PDF
dotnet remove package VectSharp
IronPDFをインストールします:
# Install IronPDF
dotnet add package IronPdf
ステップ 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;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()
{
//ベクトシャープ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()
{
//ベクトシャープ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
Imports VectSharp
Imports VectSharp.PDF
Imports VectSharp.SVG
Imports System.IO
Class Program
Shared Sub Main()
'ベクトシャープ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 Class' NuGet: Install-Package VectSharp.PDF
Imports VectSharp
Imports VectSharp.PDF
Imports VectSharp.SVG
Imports System.IO
Class Program
Shared Sub Main()
'ベクトシャープ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では、Graphicsオブジェクトを作成し、正確な座標とフォントオブジェクトで手動でテキストを配置する必要があります。 IronPDFはCSSスタイリングを完全にサポートし、HTMLを直接レンダリングします。
高度なHTMLからPDFへのシナリオについては、HTMLからPDFへの変換ガイドをご覧ください。
複数ページのドキュメントを作成する
複数ページのドキュメントでは、これら for .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は、CSS page-break-after: alwaysを使用して、単一のHTML文字列でマルチページドキュメントを自動的に作成します。
図形とテキストを描く
グラフィック機能は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 - 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プロパティを使用します:border-radius: 50%、標準のマージン。
##ベクトシャープAPIからIronPDFへのマッピングリファレンス
このマッピングは、APIと同等のものを直接示すことで、移行を加速します:
| ベクトシャープ | 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を使用してください:
//ベクトシャープ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>//ベクトシャープ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>Imports System.Drawing.Drawing2D
'ベクトシャープpath
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
'ベクトシャープpath
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チャートライブラリを使用する
IronPDFは、Chart.js、D3.js、Plotlyのような強力なJavaScriptライブラリを活用することができます:
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 font-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を使用します。
ソリューション: ベクターグラフィックスには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()を使用した配色。 フォント設定を特定する。 SVG変換用にGraphicsPathを使用して複雑なベクターグラフィックスをマッピングします。
コード更新タスク
1.VectSharp NuGetパッケージの削除
2.IronPDFNuGet パッケージをインストールします
3. using ステートメントをIronPdfに更新します
4. 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 サポート: Flexbox およびグリッド レイアウトを含む完全な CSS3。 レスポンシブデザインはPDFに直接翻訳します。
JavaScript実行: Chart.js、D3.js、または Plotly を使用したインタラクティブ チャートが正しくレンダリングされます。 動的コンテンツは期待通りに機能します。
URL から PDF:任意の Web ページを PDF としてキャプチャします。これはベクトシャープでは不可能な機能です。
PDF 操作:結合、分割、透かしの追加、パスワード保護、デジタル署名などの機能が組み込まれています。
**コードの詳細度が低い:**HTML/CSSは宣言型で読みやすいです。 同じ文書であれば、VectSharpの命令型描画アプローチよりも大幅に少ないコードで済みます。
アクティブな開発: IronPDFの定期的な更新は、現在および将来 for .NETバージョンとの互換性を確保します。
Curtis Chauは、カールトン大学でコンピュータサイエンスの学士号を取得し、Node.js、TypeScript、JavaScript、およびReactに精通したフロントエンド開発を専門としています。直感的で美しいユーザーインターフェースを作成することに情熱を持ち、Curtisは現代のフレームワークを用いた開発や、構造の良い視覚的に魅力的なマニュアルの作成を楽しんでいます。
関連する記事


