
ASP.NET MVCビューでHTMLをPDFに変換する方法
PDFsharp からIronPDFへの移行は、手動の座標ベースの描画から、現代の HTML/CSS テンプレート化に変換します。 このガイドは、GDI+ スタイルのポジショニングを Web 技術に置き換え、開発時間を短縮し、標準的な HTML/CSS スキルを通じて PDF 生成を保守可能にするステップバイステップの移行パスを提供します。
PDFシャープ からIronPDFへの移行理由
PDFシャープ を理解する
PDFsharp は、開発者がプログラミングで PDF ドキュメントを生成できる低レベルの PDF 作成ライブラリです。 MIT ライセンスの下で公開され、PDFsharp-Team (もとは empira Software GmbH) によって維持されている PDFシャープ 6.x は、.NET 8/9/10 と .NET Standard 2.0 をターゲットにし、Core ビルドを介して Windows、Linux、macOS でクロスプラットフォームで動作します。 PDFシャープ は主に GDI+ スタイルの API を使用してゼロから PDF を描画およびコンパイルするツールとして機能しますが、プロジェクトの性質によっては、利点と制約があることがあります。
PDFsharp は時折、HTML から PDF への変換ツールと誤解されますが、実際にはそうではありません。 その目的は、プログラムによるPDF文書作成に特化しています。 HtmlRenderer.PdfSharp (ArthurHub による) が提供する予定である HTML レンダリング機能を提供するコミュニティのアドオンがありますが、それは HTML 4.01 / CSS レベル 2 のみをカバーし、flexbox や grid、JavaScript のような現代的な CSS 機能は含まれていません。
座標計算問題
PDFsharp の GDI+ アプローチでは、以下が必要です:
- 各要素の正確なX,Y位置を計算する。
- ページのオーバーフローのためにコンテンツの高さを手動で追跡する
- 行の折り返しやテキスト測定を自分で処理する
- ボーダー計算でセルごとに表を描く
- 手動改ページで複数ページのドキュメントを管理
PDFsharp のアーキテクチャは、座標を使ったポジショニングの深い理解を必要とし、複雑なレイアウトの作成にはしばしば課題をもたらします。
PDFシャープ とIronPDFの比較
| フィーチャー | PDFシャープ | IronPDF |
|---|---|---|
| ライセンス | MIT (無料) | 商用 |
| HTMLからPDFへのサポート。 | なし | はい(HTML5/CSS3サポート) |
| モダンCSSサポート | いいえ (HTML 4.01 / CSS 2 は HtmlRenderer アドオン経由) | はい(フルCSS3) |
| ドキュメント作成 | 座標ベースの図面 | HTML/CSSテンプレート |
| レイアウトシステム | マニュアルX,Yポジショニング | CSS フロー/フレックスボックス/グリッド |
| 改ページ | 手計算 | 自動 + CSS コントロール |
| テーブル | セルを個別に描画 | HTML <table> |
| スタイリング。 | コードベースのフォント/カラー | CSSスタイルシート |
| ドキュメント API | 低レベル(座標が必要です) | ハイレベル(簡易API) |
| 更新情報 | アクティブ (6.x ライン) | レギュラー |
IronPDFはHTMLドキュメントを完全な忠実度でPDFに変換する必要があるシナリオで輝きを放ちます。 こ for .NETライブラリは、HTML5とCSS3をサポートしており、最新のWeb標準に対応しています。 HTMLからPDFへのネイティブ機能により、開発者は既存のWebコンテンツや最新のWebツールでデザインされたテンプレートを活用することができます。
現代の .NET を使っているチーム向けに、IronPDF は座標計算を排除し、Web 開発スキルを活用するアプローチを提供します。
始める前に
前提条件
- .NET環境: .NET Framework 4.6.2+ または.NET Core 3.1+ / .NET 5/6/7/8/9+
- NuGetアクセス: NuGetパッケージをインストールする機能
- IronPDFライセンス: IronPDFからライセンスキーを取得します。
NuGetパッケージの変更
# Remove PDFシャープ (official PDFsharp-Team package IDs; case-sensitive on case-sensitive feeds)
dotnet remove package PDFsharp
# dotnet remove package PDFsharp-WPF # if you used the WPF build
# dotnet remove package PDFsharp-GDI # if you used the GDI build
# dotnet remove package PDFsharp-MigraDoc # if you used MigraDoc on top
# dotnet remove package PdfSharpCore # community .NET Standard port
# Add IronPDF
dotnet add package IronPdf
ライセンス構成
// Add at application startup (Program.cs or Startup.cs)
IronPdf.License.LicenseKey = "YOUR-LICENSE-KEY";' Add at application startup (Program.vb or Startup.vb)
IronPdf.License.LicenseKey = "YOUR-LICENSE-KEY"' Add at application startup (Program.vb or Startup.vb)
IronPdf.License.LicenseKey = "YOUR-LICENSE-KEY"PDFシャープ の使用を特定する
# Find all PDFシャープ usages in your codebase
grep -r "PdfSharp\|XGraphics\|XFont\|XBrush\|XPen" --include="*.cs" .
完全な API リファレンス
名前空間の変更
// Before: PDFsharp
using PdfSharp.Pdf;
using PdfSharp.Drawing;
using PdfSharp.Pdf.IO;
// After: IronPDF
using IronPdf;
using IronPdf.Editing;// Before: PDFsharp
using PdfSharp.Pdf;
using PdfSharp.Drawing;
using PdfSharp.Pdf.IO;
// After: IronPDF
using IronPdf;
using IronPdf.Editing;Imports IronPdf
Imports IronPdf.EditingImports IronPdf
Imports IronPdf.Editingコア API マッピング
| PDFsharp API | IronPDF API |
|---|---|
new PdfDocument() | ChromePdfRenderer.RenderHtmlAsPdf() |
document.AddPage() | 自動翻訳 |
XGraphics.FromPdfPage() | 不要 |
XGraphics.DrawString() | HTML <p>, <h1>, など |
XGraphics.DrawImage() | HTML <img> タグ |
XFont | CSS font-family, font-size |
XBrush, XPen | CSSカラー/ボーダー |
document.Save() | pdf.SaveAs() |
PdfReader.Open() | PdfDocument.FromFile() |
コード移行の例
例1: HTMLからPDFへの変換
以前 (PDFsharp の場合):
// NuGet: Install-Package PDFシャープ (official PDFsharp-Team package, MIT)
// PDFシャープ does NOT support HTML-to-PDF natively. The community add-on
// HtmlRenderer.PdfSharp covers HTML 4.01 / CSS level 2 only (no flexbox, grid, JS).
using PdfSharp.Pdf;
using PdfSharp.Drawing;
using System;
class Program
{
static void Main()
{
// PDFシャープ does not have built-inHTMLからPDFへconversion
// You need to manually parse HTML and render content
PdfDocument document = new PdfDocument();
PdfPage page = document.AddPage();
XGraphics gfx = XGraphics.FromPdfPage(page);
XFont font = new XFont("Arial", 12);
// マニュアル text rendering (no HTML support)
gfx.DrawString("Hello from PDFsharp", font, XBrushes.Black,
new XRect(0, 0, page.Width, page.Height),
XStringFormats.TopLeft);
document.Save("output.pdf");
}
}// NuGet: Install-Package PDFシャープ (official PDFsharp-Team package, MIT)
// PDFシャープ does NOT support HTML-to-PDF natively. The community add-on
// HtmlRenderer.PdfSharp covers HTML 4.01 / CSS level 2 only (no flexbox, grid, JS).
using PdfSharp.Pdf;
using PdfSharp.Drawing;
using System;
class Program
{
static void Main()
{
// PDFシャープ does not have built-inHTMLからPDFへconversion
// You need to manually parse HTML and render content
PdfDocument document = new PdfDocument();
PdfPage page = document.AddPage();
XGraphics gfx = XGraphics.FromPdfPage(page);
XFont font = new XFont("Arial", 12);
// マニュアル text rendering (no HTML support)
gfx.DrawString("Hello from PDFsharp", font, XBrushes.Black,
new XRect(0, 0, page.Width, page.Height),
XStringFormats.TopLeft);
document.Save("output.pdf");
}
}Imports PdfSharp.Pdf
Imports PdfSharp.Drawing
Imports System
Class Program
Shared Sub Main()
' PDFシャープ does not have built-inHTMLからPDFへconversion
' You need to manually parse HTML and render content
Dim document As New PdfDocument()
Dim page As PdfPage = document.AddPage()
Dim gfx As XGraphics = XGraphics.FromPdfPage(page)
Dim font As New XFont("Arial", 12)
' マニュアル text rendering (no HTML support)
gfx.DrawString("Hello from PDFsharp", font, XBrushes.Black, New XRect(0, 0, page.Width, page.Height), XStringFormats.TopLeft)
document.Save("output.pdf")
End Sub
End ClassImports PdfSharp.Pdf
Imports PdfSharp.Drawing
Imports System
Class Program
Shared Sub Main()
' PDFシャープ does not have built-inHTMLからPDFへconversion
' You need to manually parse HTML and render content
Dim document As New PdfDocument()
Dim page As PdfPage = document.AddPage()
Dim gfx As XGraphics = XGraphics.FromPdfPage(page)
Dim font As New XFont("Arial", 12)
' マニュアル text rendering (no HTML support)
gfx.DrawString("Hello from PDFsharp", font, XBrushes.Black, New XRect(0, 0, page.Width, page.Height), XStringFormats.TopLeft)
document.Save("output.pdf")
End Sub
End Class翻訳後(IronPDF):。
// NuGet: Install-Package IronPdf
using IronPdf;
using System;
class Program
{
static void Main()
{
//IronPDFhas nativeHTMLからPDFへrendering
var renderer = new ChromePdfRenderer();
string html = "<h1>Hello from IronPDF</h1><p>EasyHTMLからPDFへconversion</p>";
var pdf = renderer.RenderHtmlAsPdf(html);
pdf.SaveAs("output.pdf");
}
}// NuGet: Install-Package IronPdf
using IronPdf;
using System;
class Program
{
static void Main()
{
//IronPDFhas nativeHTMLからPDFへrendering
var renderer = new ChromePdfRenderer();
string html = "<h1>Hello from IronPDF</h1><p>EasyHTMLからPDFへconversion</p>";
var pdf = renderer.RenderHtmlAsPdf(html);
pdf.SaveAs("output.pdf");
}
}' NuGet: Install-Package IronPdf
Imports IronPdf
Imports System
Class Program
Shared Sub Main()
' IronPDF has native HTML から PDF へ rendering
Dim renderer = New ChromePdfRenderer()
Dim html As String = "<h1>Hello from IronPDF</h1><p>Easy HTML から PDF へ conversion</p>"
Dim pdf = renderer.RenderHtmlAsPdf(html)
pdf.SaveAs("output.pdf")
End Sub
End Class' NuGet: Install-Package IronPdf
Imports IronPdf
Imports System
Class Program
Shared Sub Main()
' IronPDF has native HTML から PDF へ rendering
Dim renderer = New ChromePdfRenderer()
Dim html As String = "<h1>Hello from IronPDF</h1><p>Easy HTML から PDF へ conversion</p>"
Dim pdf = renderer.RenderHtmlAsPdf(html)
pdf.SaveAs("output.pdf")
End Sub
End Classこの例では、2つのライブラリの最も大きな違いを強調しています。 PDFsharpは組み込みのHTMLからPDFへの変換を提供していません。手動でXRect座標を使用する必要があります。
IronPDFはChromePdfRendererを通じてネイティブなHTMLからPDFへのレンダリングを提供します。 RenderHtmlAsPdf()メソッドはHTML文字列を受け取り、内部でChromiumエンジンを使用して変換します。 IronPDFは簡単にHTMLファイルをPDFに変換し、HTML5とCSS3で定義されたすべてのスタイルを保持し、座標計算の必要性を排除します。 包括的な例については、HTML to PDF documentationを参照してください。
例2: 既存のPDFにテキスト/透かしを追加する
以前 (PDFsharp の場合):
// NuGet: Install-Package PDFシャープ (official PDFsharp-Team package, MIT)
using PdfSharp.Pdf;
using PdfSharp.Pdf.IO;
using PdfSharp.Drawing;
using System;
class Program
{
static void Main()
{
// Open existing PDF
PdfDocument document = PdfReader.Open("existing.pdf", PdfDocumentOpenMode.Modify);
PdfPage page = document.Pages[0];
// Get graphics object
XGraphics gfx = XGraphics.FromPdfPage(page);
// Note: in PDFシャープ 6.x, XFontStyle was renamed to XFontStyleEx
XFont font = new XFont("Arial", 20, XFontStyleEx.Bold);
// Draw text at specific position
gfx.DrawString("Watermark Text", font, XBrushes.Red,
new XPoint(200, 400));
document.Save("modified.pdf");
}
}// NuGet: Install-Package PDFシャープ (official PDFsharp-Team package, MIT)
using PdfSharp.Pdf;
using PdfSharp.Pdf.IO;
using PdfSharp.Drawing;
using System;
class Program
{
static void Main()
{
// Open existing PDF
PdfDocument document = PdfReader.Open("existing.pdf", PdfDocumentOpenMode.Modify);
PdfPage page = document.Pages[0];
// Get graphics object
XGraphics gfx = XGraphics.FromPdfPage(page);
// Note: in PDFシャープ 6.x, XFontStyle was renamed to XFontStyleEx
XFont font = new XFont("Arial", 20, XFontStyleEx.Bold);
// Draw text at specific position
gfx.DrawString("Watermark Text", font, XBrushes.Red,
new XPoint(200, 400));
document.Save("modified.pdf");
}
}Imports PdfSharp.Pdf
Imports PdfSharp.Pdf.IO
Imports PdfSharp.Drawing
Imports System
Module Program
Sub Main()
' Open existing PDF
Dim document As PdfDocument = PdfReader.Open("existing.pdf", PdfDocumentOpenMode.Modify)
Dim page As PdfPage = document.Pages(0)
' Get graphics object
Dim gfx As XGraphics = XGraphics.FromPdfPage(page)
' Note: in PDFシャープ 6.x, XFontStyle was renamed to XFontStyleEx
Dim font As XFont = New XFont("Arial", 20, XFontStyleEx.Bold)
' Draw text at specific position
gfx.DrawString("Watermark Text", font, XBrushes.Red, New XPoint(200, 400))
document.Save("modified.pdf")
End Sub
End ModuleImports PdfSharp.Pdf
Imports PdfSharp.Pdf.IO
Imports PdfSharp.Drawing
Imports System
Module Program
Sub Main()
' Open existing PDF
Dim document As PdfDocument = PdfReader.Open("existing.pdf", PdfDocumentOpenMode.Modify)
Dim page As PdfPage = document.Pages(0)
' Get graphics object
Dim gfx As XGraphics = XGraphics.FromPdfPage(page)
' Note: in PDFシャープ 6.x, XFontStyle was renamed to XFontStyleEx
Dim font As XFont = New XFont("Arial", 20, XFontStyleEx.Bold)
' Draw text at specific position
gfx.DrawString("Watermark Text", font, XBrushes.Red, New XPoint(200, 400))
document.Save("modified.pdf")
End Sub
End Module翻訳後(IronPDF):。
// NuGet: Install-Package IronPdf
using IronPdf;
using IronPdf.Editing;
using System;
class Program
{
static void Main()
{
IronPdf.License.LicenseKey = "YOUR-LICENSE-KEY";
// Open existing PDF
var pdf = PdfDocument.FromFile("existing.pdf");
// Add text stamp/watermark
var textStamper = new TextStamper()
{
Text = "Watermark Text",
FontSize = 20,
FontFamily = "Arial",
IsBold = true,
FontColor = IronSoftware.Drawing.Color.Red,
VerticalAlignment = VerticalAlignment.Middle,
HorizontalAlignment = HorizontalAlignment.Center
};
pdf.ApplyStamp(textStamper);
pdf.SaveAs("modified.pdf");
}
}// NuGet: Install-Package IronPdf
using IronPdf;
using IronPdf.Editing;
using System;
class Program
{
static void Main()
{
IronPdf.License.LicenseKey = "YOUR-LICENSE-KEY";
// Open existing PDF
var pdf = PdfDocument.FromFile("existing.pdf");
// Add text stamp/watermark
var textStamper = new TextStamper()
{
Text = "Watermark Text",
FontSize = 20,
FontFamily = "Arial",
IsBold = true,
FontColor = IronSoftware.Drawing.Color.Red,
VerticalAlignment = VerticalAlignment.Middle,
HorizontalAlignment = HorizontalAlignment.Center
};
pdf.ApplyStamp(textStamper);
pdf.SaveAs("modified.pdf");
}
}' NuGet: Install-Package IronPdf
Imports IronPdf
Imports IronPdf.Editing
Imports System
Class Program
Shared Sub Main()
IronPdf.License.LicenseKey = "YOUR-LICENSE-KEY"
' Open existing PDF
Dim pdf = PdfDocument.FromFile("existing.pdf")
' Add text stamp/watermark
Dim textStamper = New TextStamper() With {
.Text = "Watermark Text",
.FontSize = 20,
.FontFamily = "Arial",
.IsBold = True,
.FontColor = IronSoftware.Drawing.Color.Red,
.VerticalAlignment = VerticalAlignment.Middle,
.HorizontalAlignment = HorizontalAlignment.Center
}
pdf.ApplyStamp(textStamper)
pdf.SaveAs("modified.pdf")
End Sub
End Class' NuGet: Install-Package IronPdf
Imports IronPdf
Imports IronPdf.Editing
Imports System
Class Program
Shared Sub Main()
IronPdf.License.LicenseKey = "YOUR-LICENSE-KEY"
' Open existing PDF
Dim pdf = PdfDocument.FromFile("existing.pdf")
' Add text stamp/watermark
Dim textStamper = New TextStamper() With {
.Text = "Watermark Text",
.FontSize = 20,
.FontFamily = "Arial",
.IsBold = True,
.FontColor = IronSoftware.Drawing.Color.Red,
.VerticalAlignment = VerticalAlignment.Middle,
.HorizontalAlignment = HorizontalAlignment.Center
}
pdf.ApplyStamp(textStamper)
pdf.SaveAs("modified.pdf")
End Sub
End ClassPDFsharpでは、XPointで正確なX,Y座標(200, 400)を指定します。
IronPDFではText, FontSize, FontFamily, IsBold, FontColor, VerticalAlignment, ApplyStamp()によってこれを簡素化しています。 座標計算の必要はありません。アライメントを指定するだけで、IronPDFが位置決めを行います。 スタンプ機能のためにIronPdf.Editing名前空間が必要です。
例3: 画像でPDFを作成する
以前 (PDFsharp の場合):
// NuGet: Install-Package PDFシャープ (official PDFsharp-Team package, MIT)
using PdfSharp.Pdf;
using PdfSharp.Drawing;
using System;
class Program
{
static void Main()
{
// Create new PDF document
PdfDocument document = new PdfDocument();
PdfPage page = document.AddPage();
XGraphics gfx = XGraphics.FromPdfPage(page);
// Load and draw image
XImage image = XImage.FromFile("image.jpg");
// Calculate size to fit page
double width = 200;
double height = 200;
gfx.DrawImage(image, 50, 50, width, height);
// Add text
XFont font = new XFont("Arial", 16);
gfx.DrawString("Image in PDF", font, XBrushes.Black,
new XPoint(50, 270));
document.Save("output.pdf");
}
}// NuGet: Install-Package PDFシャープ (official PDFsharp-Team package, MIT)
using PdfSharp.Pdf;
using PdfSharp.Drawing;
using System;
class Program
{
static void Main()
{
// Create new PDF document
PdfDocument document = new PdfDocument();
PdfPage page = document.AddPage();
XGraphics gfx = XGraphics.FromPdfPage(page);
// Load and draw image
XImage image = XImage.FromFile("image.jpg");
// Calculate size to fit page
double width = 200;
double height = 200;
gfx.DrawImage(image, 50, 50, width, height);
// Add text
XFont font = new XFont("Arial", 16);
gfx.DrawString("Image in PDF", font, XBrushes.Black,
new XPoint(50, 270));
document.Save("output.pdf");
}
}' NuGet: Install-Package PDFシャープ (official PDFsharp-Team package, MIT)
Imports PdfSharp.Pdf
Imports PdfSharp.Drawing
Imports System
Module Program
Sub Main()
' Create new PDF document
Dim document As New PdfDocument()
Dim page As PdfPage = document.AddPage()
Dim gfx As XGraphics = XGraphics.FromPdfPage(page)
' Load and draw image
Dim image As XImage = XImage.FromFile("image.jpg")
' Calculate size to fit page
Dim width As Double = 200
Dim height As Double = 200
gfx.DrawImage(image, 50, 50, width, height)
' Add text
Dim font As New XFont("Arial", 16)
gfx.DrawString("Image in PDF", font, XBrushes.Black, New XPoint(50, 270))
document.Save("output.pdf")
End Sub
End Module' NuGet: Install-Package PDFシャープ (official PDFsharp-Team package, MIT)
Imports PdfSharp.Pdf
Imports PdfSharp.Drawing
Imports System
Module Program
Sub Main()
' Create new PDF document
Dim document As New PdfDocument()
Dim page As PdfPage = document.AddPage()
Dim gfx As XGraphics = XGraphics.FromPdfPage(page)
' Load and draw image
Dim image As XImage = XImage.FromFile("image.jpg")
' Calculate size to fit page
Dim width As Double = 200
Dim height As Double = 200
gfx.DrawImage(image, 50, 50, width, height)
' Add text
Dim font As New XFont("Arial", 16)
gfx.DrawString("Image in PDF", font, XBrushes.Black, New XPoint(50, 270))
document.Save("output.pdf")
End Sub
End Module翻訳後(IronPDF):。
// NuGet: Install-Package IronPdf
using IronPdf;
using System;
class Program
{
static void Main()
{
IronPdf.License.LicenseKey = "YOUR-LICENSE-KEY";
// Create PDF from HTML with image
var renderer = new ChromePdfRenderer();
string html = @"
<h1>Image in PDF</h1>
<img src='image.jpg' style='width:200px; height:200px;' />
<p>Easy image embedding with HTML</p>";
var pdf = renderer.RenderHtmlAsPdf(html);
pdf.SaveAs("output.pdf");
// Alternative: Add image to existing PDF
var existingPdf = new ChromePdfRenderer().RenderHtmlAsPdf("<h1>Document</h1>");
var imageStamper = new IronPdf.Editing.ImageStamper(new Uri("image.jpg"))
{
VerticalAlignment = IronPdf.Editing.VerticalAlignment.Top
};
existingPdf.ApplyStamp(imageStamper);
}
}// NuGet: Install-Package IronPdf
using IronPdf;
using System;
class Program
{
static void Main()
{
IronPdf.License.LicenseKey = "YOUR-LICENSE-KEY";
// Create PDF from HTML with image
var renderer = new ChromePdfRenderer();
string html = @"
<h1>Image in PDF</h1>
<img src='image.jpg' style='width:200px; height:200px;' />
<p>Easy image embedding with HTML</p>";
var pdf = renderer.RenderHtmlAsPdf(html);
pdf.SaveAs("output.pdf");
// Alternative: Add image to existing PDF
var existingPdf = new ChromePdfRenderer().RenderHtmlAsPdf("<h1>Document</h1>");
var imageStamper = new IronPdf.Editing.ImageStamper(new Uri("image.jpg"))
{
VerticalAlignment = IronPdf.Editing.VerticalAlignment.Top
};
existingPdf.ApplyStamp(imageStamper);
}
}' NuGet: Install-Package IronPdf
Imports IronPdf
Imports System
Module Program
Sub Main()
IronPdf.License.LicenseKey = "YOUR-LICENSE-KEY"
' Create PDF from HTML with image
Dim renderer = New ChromePdfRenderer()
Dim html As String = "
<h1>Image in PDF</h1>
<img src='image.jpg' style='width:200px; height:200px;' />
<p>Easy image embedding with HTML</p>"
Dim pdf = renderer.RenderHtmlAsPdf(html)
pdf.SaveAs("output.pdf")
' Alternative: Add image to existing PDF
Dim existingPdf = New ChromePdfRenderer().RenderHtmlAsPdf("<h1>Document</h1>")
Dim imageStamper = New IronPdf.Editing.ImageStamper(New Uri("image.jpg")) With {
.VerticalAlignment = IronPdf.Editing.VerticalAlignment.Top
}
existingPdf.ApplyStamp(imageStamper)
End Sub
End Module' NuGet: Install-Package IronPdf
Imports IronPdf
Imports System
Module Program
Sub Main()
IronPdf.License.LicenseKey = "YOUR-LICENSE-KEY"
' Create PDF from HTML with image
Dim renderer = New ChromePdfRenderer()
Dim html As String = "
<h1>Image in PDF</h1>
<img src='image.jpg' style='width:200px; height:200px;' />
<p>Easy image embedding with HTML</p>"
Dim pdf = renderer.RenderHtmlAsPdf(html)
pdf.SaveAs("output.pdf")
' Alternative: Add image to existing PDF
Dim existingPdf = New ChromePdfRenderer().RenderHtmlAsPdf("<h1>Document</h1>")
Dim imageStamper = New IronPdf.Editing.ImageStamper(New Uri("image.jpg")) With {
.VerticalAlignment = IronPdf.Editing.VerticalAlignment.Top
}
existingPdf.ApplyStamp(imageStamper)
End Sub
End ModulePDFsharpが必要とするのは、新しいDrawImage()を使って正確な座標(50, 50, 200, 200)で行い、それから別にDrawString()でテキストを追加します。
IronPDFは標準HTMLと<img>タグおよびCSSスタイリング(style='width:200px;')を使用します。 高さ:200px;')。 座標計算は不要で、CSSがレイアウトを処理します。 IronPDFはまた、宣言型の整列プロパティを持つ既存のPDFに画像を追加するためのImageStamperを提供します。 詳しくは、チュートリアルをご覧ください。
重要な移行に関する注意事項
パラダイムシフト:座標からHTML/CSSへ
最も大きな変更点は、座標ベースの描画からHTML/CSSへの移行です:
// PDFsharp: manual positioning
gfx.DrawString("Invoice", titleFont, XBrushes.Black, new XPoint(50, 50));
gfx.DrawString("Customer: John", bodyFont, XBrushes.Black, new XPoint(50, 80));
// IronPDF: let CSS handle layout
var html = @"
<div style='padding: 50px;'>
<h1>Invoice</h1>
<p>Customer: John</p>
</div>";
var pdf = renderer.RenderHtmlAsPdf(html);// PDFsharp: manual positioning
gfx.DrawString("Invoice", titleFont, XBrushes.Black, new XPoint(50, 50));
gfx.DrawString("Customer: John", bodyFont, XBrushes.Black, new XPoint(50, 80));
// IronPDF: let CSS handle layout
var html = @"
<div style='padding: 50px;'>
<h1>Invoice</h1>
<p>Customer: John</p>
</div>";
var pdf = renderer.RenderHtmlAsPdf(html);Imports PdfSharp.Drawing
Imports IronPdf
' PDFsharp: manual positioning
gfx.DrawString("Invoice", titleFont, XBrushes.Black, New XPoint(50, 50))
gfx.DrawString("Customer: John", bodyFont, XBrushes.Black, New XPoint(50, 80))
' IronPDF: let CSS handle layout
Dim html As String = "
<div style='padding: 50px;'>
<h1>Invoice</h1>
<p>Customer: John</p>
</div>"
Dim pdf = renderer.RenderHtmlAsPdf(html)Imports PdfSharp.Drawing
Imports IronPdf
' PDFsharp: manual positioning
gfx.DrawString("Invoice", titleFont, XBrushes.Black, New XPoint(50, 50))
gfx.DrawString("Customer: John", bodyFont, XBrushes.Black, New XPoint(50, 80))
' IronPDF: let CSS handle layout
Dim html As String = "
<div style='padding: 50px;'>
<h1>Invoice</h1>
<p>Customer: John</p>
</div>"
Dim pdf = renderer.RenderHtmlAsPdf(html)フォントの移行
// PDFsharp: XFont objects (PDFsharp 6.x renamed XFontStyle to XFontStyleEx)
var titleFont = new XFont("Arial", 24, XFontStyleEx.Bold);
var bodyFont = new XFont("Times New Roman", 12);
// IronPDF: CSS font properties
var html = @"
<style>
h1 { font-family: Arial, sans-serif; font-size: 24px; font-weight: bold; }
p { font-family: 'Times New Roman', serif; font-size: 12px; }
</style>";// PDFsharp: XFont objects (PDFsharp 6.x renamed XFontStyle to XFontStyleEx)
var titleFont = new XFont("Arial", 24, XFontStyleEx.Bold);
var bodyFont = new XFont("Times New Roman", 12);
// IronPDF: CSS font properties
var html = @"
<style>
h1 { font-family: Arial, sans-serif; font-size: 24px; font-weight: bold; }
p { font-family: 'Times New Roman', serif; font-size: 12px; }
</style>";' PDFsharp: XFont objects (PDFsharp 6.x renamed XFontStyle to XFontStyleEx)
Dim titleFont As New XFont("Arial", 24, XFontStyleEx.Bold)
Dim bodyFont As New XFont("Times New Roman", 12)
' IronPDF: CSS font properties
Dim html As String = "
<style>
h1 { font-family: Arial, sans-serif; font-size: 24px; font-weight: bold; }
p { font-family: 'Times New Roman', serif; font-size: 12px; }
</style>"' PDFsharp: XFont objects (PDFsharp 6.x renamed XFontStyle to XFontStyleEx)
Dim titleFont As New XFont("Arial", 24, XFontStyleEx.Bold)
Dim bodyFont As New XFont("Times New Roman", 12)
' IronPDF: CSS font properties
Dim html As String = "
<style>
h1 { font-family: Arial, sans-serif; font-size: 24px; font-weight: bold; }
p { font-family: 'Times New Roman', serif; font-size: 12px; }
</style>"ドキュメントの読み込みの変更
// PDFsharp: PdfReader.Open()
PdfDocument document = PdfReader.Open("existing.pdf", PdfDocumentOpenMode.Modify);
// IronPDF: PdfDocument.FromFile()
var pdf = PdfDocument.FromFile("existing.pdf");// PDFsharp: PdfReader.Open()
PdfDocument document = PdfReader.Open("existing.pdf", PdfDocumentOpenMode.Modify);
// IronPDF: PdfDocument.FromFile()
var pdf = PdfDocument.FromFile("existing.pdf");Imports PdfSharp.Pdf
Imports PdfSharp.Pdf.IO
Imports IronPdf
' PDFsharp: PdfReader.Open()
Dim document As PdfDocument = PdfReader.Open("existing.pdf", PdfDocumentOpenMode.Modify)
' IronPDF: PdfDocument.FromFile()
Dim pdf As PdfDocument = PdfDocument.FromFile("existing.pdf")Imports PdfSharp.Pdf
Imports PdfSharp.Pdf.IO
Imports IronPdf
' PDFsharp: PdfReader.Open()
Dim document As PdfDocument = PdfReader.Open("existing.pdf", PdfDocumentOpenMode.Modify)
' IronPDF: PdfDocument.FromFile()
Dim pdf As PdfDocument = PdfDocument.FromFile("existing.pdf")保存メソッドの変更
// PDFsharp: document.Save()
document.Save("output.pdf");
// IronPDF: pdf.SaveAs()
pdf.SaveAs("output.pdf");// PDFsharp: document.Save()
document.Save("output.pdf");
// IronPDF: pdf.SaveAs()
pdf.SaveAs("output.pdf");' PDFsharp: document.Save()
document.Save("output.pdf")
' IronPDF: pdf.SaveAs()
pdf.SaveAs("output.pdf")' PDFsharp: document.Save()
document.Save("output.pdf")
' IronPDF: pdf.SaveAs()
pdf.SaveAs("output.pdf")ページのアクセス変更
// PDFsharp: document.Pages[0]
PdfPage page = document.Pages[0];
// IronPDF:自動翻訳page handling or pdf.Pages[0]
// Pages are created automatically from HTML content// PDFsharp: document.Pages[0]
PdfPage page = document.Pages[0];
// IronPDF:自動翻訳page handling or pdf.Pages[0]
// Pages are created automatically from HTML content' PDFsharp: document.Pages(0)
Dim page As PdfPage = document.Pages(0)
' IronPDF:自動翻訳page handling or pdf.Pages(0)
' Pages are created automatically from HTML content' PDFsharp: document.Pages(0)
Dim page As PdfPage = document.Pages(0)
' IronPDF:自動翻訳page handling or pdf.Pages(0)
' Pages are created automatically from HTML content移行後の新機能
IronPDF に移行した後、PDFsharp が提供できない機能を得ることができます。
ネイティブ HTML から PDF
var renderer = new ChromePdfRenderer();
var pdf = renderer.RenderHtmlAsPdf("<h1>Modern Web Content</h1>");var renderer = new ChromePdfRenderer();
var pdf = renderer.RenderHtmlAsPdf("<h1>Modern Web Content</h1>");Dim renderer As New ChromePdfRenderer()
Dim pdf = renderer.RenderHtmlAsPdf("<h1>Modern Web Content</h1>")Dim renderer As New ChromePdfRenderer()
Dim pdf = renderer.RenderHtmlAsPdf("<h1>Modern Web Content</h1>")URLをPDFに
var pdf = renderer.RenderUrlAsPdf("https://example.com");var pdf = renderer.RenderUrlAsPdf("https://example.com");Dim pdf = renderer.RenderUrlAsPdf("https://example.com")Dim pdf = renderer.RenderUrlAsPdf("https://example.com")PDFマージ
var pdf1 = PdfDocument.FromFile("document1.pdf");
var pdf2 = PdfDocument.FromFile("document2.pdf");
var merged = PdfDocument.Merge(pdf1, pdf2);var pdf1 = PdfDocument.FromFile("document1.pdf");
var pdf2 = PdfDocument.FromFile("document2.pdf");
var merged = PdfDocument.Merge(pdf1, pdf2);Dim pdf1 = PdfDocument.FromFile("document1.pdf")
Dim pdf2 = PdfDocument.FromFile("document2.pdf")
Dim merged = PdfDocument.Merge(pdf1, pdf2)Dim pdf1 = PdfDocument.FromFile("document1.pdf")
Dim pdf2 = PdfDocument.FromFile("document2.pdf")
Dim merged = PdfDocument.Merge(pdf1, pdf2)HTMLによるウォーターマーク
pdf.ApplyWatermark("<h2 style='color:red;'>CONFIDENTIAL</h2>");pdf.ApplyWatermark("<h2 style='color:red;'>CONFIDENTIAL</h2>");pdf.ApplyWatermark("<h2 style='color:red;'>CONFIDENTIAL</h2>")pdf.ApplyWatermark("<h2 style='color:red;'>CONFIDENTIAL</h2>")機能比較の概要
| フィーチャー | PDFシャープ | IronPDF |
|---|---|---|
| 座標ベースの図面 | ✓ | ✗ (HTMLを使用) |
| HTMLからPDFへ | ✗ | ✓ |
| CSS3 サポート | ✗ | ✓ |
| フレックスボックス/グリッドレイアウト | ✗ | ✓ |
| テキストスタンプ | マニュアル XGraphics | テキストスタンパー |
| 画像スタンプ | マニュアル XImage | イメージスタンパー |
| PDFのマージ | マニュアル | ✓ |
| URLからPDFへ | ✗ | ✓ |
| 最新のウェブレンダリング | ✗ | Chromium エンジン |
| 自動改ページ | ✗ | ✓ |
移行チェックリスト
移行前
- コードベースにおけるすべての PDFシャープ の使用をインベントリする
- 生成される文書の種類(レポート、請求書、証明書)を識別する
- カスタムグラフィックや描画操作をメモします
- IronPDFライセンス キーの保存を計画する (環境変数を推奨)
- まずはIronPDFの試用ライセンスでテストしてください
パッケージの変更
PDFsharpNuGetパッケージ (公式PDFsharp-Teamパッケージ)を削除- 使用されている場合は
PDFsharp-WPF,PDFsharp-GDI, またはPDFsharp-MigraDocを削除 - コミュニティ.NET Standardポートを使用していた場合は
PdfSharpCoreを削除 IronPdfNuGetパッケージをインストール:dotnet add package IronPdf
コードの変更
- 名前空間インポートを更新 (
using PdfSharp.Pdf;→using IronPdf;) - スタンプ機能のために
using IronPdf.Editing;を追加 - 座標ベースのレイアウトをHTML/CSSに変換する
XFontをCSSフォントプロパティに置き換えXPenをCSSの色/境界に置き換えXGraphics.DrawString()をHTMLテキスト要素に置き換えXGraphics.DrawImage()をHTML<img>タグに置き換えPdfDocument.FromFile()に置き換えpdf.SaveAs()に置き換え- テーブル描画コードをHTMLテーブルに変換する
移行後
- 生成されたPDFの視覚的な比較
- 複数ページのドキュメントをテストする
- フォントレンダリングを確認する
- 必要に応じて新しい機能(HTMLからPDF、結合、透かし)を追加します
Curtis Chauは、カールトン大学でコンピュータサイエンスの学士号を取得し、Node.js、TypeScript、JavaScript、およびReactに精通したフロントエンド開発を専門としています。直感的で美しいユーザーインターフェースを作成することに情熱を持ち、Curtisは現代のフレームワークを用いた開発や、構造の良い視覚的に魅力的なマニュアルの作成を楽しんでいます。
関連する記事


