Jak przejść z PDFSharp na IronPDF w języku C#
Przejście z PDFSharp naIronPDFzmienia proces tworzenia plików PDF z ręcznego rysowania opartego na współrzędnych na nowoczesne szablony HTML/CSS. Niniejszy przewodnik przedstawia kompletną, krok po kroku ścieżkę migracji, która zastępuje żmudne pozycjonowanie w stylu GDI+ technologiami internetowymi, co znacznie skraca czas programowania i sprawia, że generowanie plików PDF staje się łatwe do utrzymania przy użyciu standardowych umiejętności HTML/CSS.
Dlaczego warto przejść z PDFSharp na IronPDF
Zrozumienie PDFSharp
PDFSharp jest znany jako biblioteka do tworzenia plików PDF na niskim poziomie, umożliwiająca programistom generowanie dokumentów PDF za pomocą podejścia programistycznego. PDFSharp, udostępniony na licencji MIT, zapewnia społeczności programistów swobodę użytkowania i modyfikacji. PDFSharp służy przede wszystkim jako narzędzie do tworzenia i kompilowania plików PDF od podstaw, co może być zarówno korzystne, jak i ograniczające, w zależności od charakteru projektu.
PDFSharp jest czasami błędnie uznawany za konwerter HTML na PDF, czym w rzeczywistości nie jest. Jego przeznaczeniem jest wyłącznie programowe tworzenie dokumentów PDF. Chociaż istnieje dodatek HtmlRenderer.PdfSharp, którego celem jest zapewnienie możliwości renderowania HTML, obsługuje on jedynie CSS 2.1, bez wsparcia dla nowoczesnych funkcji CSS, takich jak flexbox i grid.
Problem obliczeń współrzędnych
Podejście PDFSharp oparte na GDI+ oznacza, że musisz:
- Oblicz dokładne pozycje X, Y dla każdego elementu
- Ręczne śledzenie wysokości treści w przypadku przepełnienia strony
- Samodzielnie zadbaj o zawijanie linii i wymiarowanie tekstu
- Rysuj tabele komórka po komórce z obliczeniami obramowań
- Zarządzaj dokumentami wielostronicowymi z ręcznymi podziałami stron
Architektura PDFSharp wymaga dogłębnego zrozumienia pozycjonowania za pomocą współrzędnych, co często stanowi wyzwanie przy tworzeniu złożonych układów.
Porównanie PDFSharp i IronPDF
| Funkcja | PDFSharp | IronPDF |
|---|---|---|
| Licencja | MIT (bezpłatne) | Komercjalne |
| Obsługa konwersji HTML do PDF | Nie | Tak (obsługa HTML5/CSS3) |
| Obsługa nowoczesnego CSS | Nie (tylko CSS 2.1) | Tak (pełna obsługa CSS3) |
| Tworzenie dokumentów | Rysowanie oparte na współrzędnych | Szablony HTML/CSS |
| System układu | Ręczne pozycjonowanie X, Y | Przepływ CSS/Flexbox/Grid |
| Podziały stron | Obliczenia ręczne | Automatyczne + sterowanie CSS |
| Tabele | Rysuj komórki pojedynczo | HTML <table> |
| Stylizacja | Czcionki/kolory oparte na kodzie | Arkusze stylów CSS |
| Dokumentacja API | Niski poziom (wymaga współrzędnych) | Poziom wysokiego poziomu (uproszczone API) |
| Aktualizacje | Rzadko | Regularny |
IronPDF sprawdza się doskonale w sytuacjach, gdy dokumenty HTML wymagają konwersji do formatu PDF z zachowaniem pełnej wierności. Ta biblioteka .NET obsługuje HTML5 i CSS3, zapewniając zgodność z nowoczesnymi standardami internetowymi. Dzięki wbudowanym funkcjom konwersji HTML do PDF programiści mogą wykorzystywać istniejące treści internetowe lub szablony zaprojektowane przy użyciu nowoczesnych narzędzi internetowych.
Dla zespołów planujących wdrożenie .NET 10 i C# 14 w latach 2025 i 2026,IronPDFoferuje nowoczesne podejście, które eliminuje konieczność obliczania współrzędnych, jednocześnie wykorzystując umiejętności w zakresie tworzenia stron internetowych.
Zanim zaczniesz
Wymagania wstępne
- Środowisko .NET: .NET Framework 4.6.2+ lub .NET Core 3.1+ / .NET 5/6/7/8/9+
- Dostęp do NuGet: Możliwość instalowania pakietów NuGet
- Licencja IronPDF: Uzyskaj klucz licencyjny na stronie ironpdf.com
Zmiany w pakiecie NuGet
# Remove PDFSharp
dotnet remove package PdfSharp
dotnet remove package PdfSharp-wpf
dotnet remove package PdfSharp.Charting
# Add IronPDF
dotnet add package IronPdf
# Remove PDFSharp
dotnet remove package PdfSharp
dotnet remove package PdfSharp-wpf
dotnet remove package PdfSharp.Charting
# Add IronPDF
dotnet add package IronPdf
Konfiguracja licencji
// Add at application startup (Program.cs or Startup.cs)
IronPdf.License.LicenseKey = "YOUR-LICENSE-KEY";
// 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"
Zidentyfikuj zastosowanie PDFSharp
# Find all PDFSharp usages in your codebase
grep -r "PdfSharp\|XGraphics\|XFont\|XBrush\|XPen" --include="*.cs" .
# Find all PDFSharp usages in your codebase
grep -r "PdfSharp\|XGraphics\|XFont\|XBrush\|XPen" --include="*.cs" .
Kompletna dokumentacija API
Zmiany w przestrzeni nazw
// 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.Editing
Mapowania podstawowych interfejsów API
| PDFSharp API | IronPDF API |
|---|---|
new PdfDocument() |
ChromePdfRenderer.RenderHtmlAsPdf() |
document.AddPage() |
Automatyczne |
XGraphics.FromPdfPage() |
Nie jest potrzebne |
XGraphics.DrawString() |
HTML <p>, <h1> itp. |
XGraphics.DrawImage() |
Tag HTML <img> |
XFont |
CSS font-family, font-size |
XBrush, XPen |
Kolory/obramowania CSS |
document.Save() |
pdf.SaveAs() |
PdfReader.Open() |
PdfDocument.FromFile() |
Przykłady migracji kodu
Przykład 1: Konwersja HTML do PDF
Przed (PDFSharp):
// NuGet: Install-Package PdfSharp
using PdfSharp.Pdf;
using PdfSharp.Drawing;
using System;
class Program
{
static void Main()
{
// PDFSharp does not have built-in HTML do 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);
// Podręcznik 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 PdfSharp
using PdfSharp.Pdf;
using PdfSharp.Drawing;
using System;
class Program
{
static void Main()
{
// PDFSharp does not have built-in HTML do 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);
// Podręcznik 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
Module Program
Sub Main()
' PDFSharp does not have built-in HTML to 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)
' Podręcznik 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 Module
Po (IronPDF):
// NuGet: Install-Package IronPdf
using IronPdf;
using System;
class Program
{
static void Main()
{
//IronPDFhas native HTML do PDF rendering
var renderer = new ChromePdfRenderer();
string html = "<h1>Hello from IronPDF</h1><p>Easy HTML do 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 native HTML do PDF rendering
var renderer = new ChromePdfRenderer();
string html = "<h1>Hello from IronPDF</h1><p>Easy HTML do PDF conversion</p>";
var pdf = renderer.RenderHtmlAsPdf(html);
pdf.SaveAs("output.pdf");
}
}
Imports IronPdf
Imports System
Class Program
Shared Sub Main()
' IronPDF has native HTML to PDF rendering
Dim renderer As New ChromePdfRenderer()
Dim html As String = "<h1>Hello from IronPDF</h1><p>Easy HTML to PDF conversion</p>"
Dim pdf = renderer.RenderHtmlAsPdf(html)
pdf.SaveAs("output.pdf")
End Sub
End Class
Ten przykład podkreśla najważniejszą różnicę między tymi dwiema bibliotekami. PDFSharp wyraźnie stwierdza, że "nie posiada wbudowanej funkcji konwersji HTML do PDF" — należy ręcznie utworzyć PdfDocument, dodać PdfPage, pobrać obiekt XGraphics, utworzyć XFont oraz użyć DrawString() z współrzędnymi XRect.
IronPDF zapewnia natywne renderowanie HTML do PDF poprzez ChromePdfRenderer. Metoda RenderHtmlAsPdf() przyjmuje ciągi znaków HTML i konwertuje je wewnętrznie przy użyciu silnika Chromium.IronPDFz łatwością konwertuje pliki HTML do formatu PDF, zachowując wszystkie style zdefiniowane w HTML5 i CSS3, co eliminuje konieczność wykonywania obliczeń współrzędnych. Kompleksowe przykłady można znaleźć w dokumentacji dotyczącej konwersji HTML do PDF.
Przykład 2: Dodawanie tekstu/znaku wodnego do istniejącego pliku PDF
Przed (PDFSharp):
// NuGet: Install-Package PdfSharp
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);
XFont font = new XFont("Arial", 20, XFontStyle.Bold);
// Draw text at specific position
gfx.DrawString("Watermark Text", font, XBrushes.Red,
new XPoint(200, 400));
document.Save("modified.pdf");
}
}
// NuGet: Install-Package PdfSharp
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);
XFont font = new XFont("Arial", 20, XFontStyle.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)
Dim font As New XFont("Arial", 20, XFontStyle.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
Po (IronPDF):
// NuGet: Install-Package IronPdf
using IronPdf;
using IronPdf.Editing;
using System;
class Program
{
static void Main()
{
// Open existing PDF
var pdf = PdfDocument.FromFile("existing.pdf");
// Add text stamp/watermark
var textStamper = new TextStamper()
{
Text = "Watermark Text",
FontSize = 20,
Color = 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()
{
// Open existing PDF
var pdf = PdfDocument.FromFile("existing.pdf");
// Add text stamp/watermark
var textStamper = new TextStamper()
{
Text = "Watermark Text",
FontSize = 20,
Color = IronSoftware.Drawing.Color.Red,
VerticalAlignment = VerticalAlignment.Middle,
HorizontalAlignment = HorizontalAlignment.Center
};
pdf.ApplyStamp(textStamper);
pdf.SaveAs("modified.pdf");
}
}
Imports IronPdf
Imports IronPdf.Editing
Imports System
Module Program
Sub Main()
' Open existing PDF
Dim pdf = PdfDocument.FromFile("existing.pdf")
' Add text stamp/watermark
Dim textStamper = New TextStamper() With {
.Text = "Watermark Text",
.FontSize = 20,
.Color = IronSoftware.Drawing.Color.Red,
.VerticalAlignment = VerticalAlignment.Middle,
.HorizontalAlignment = HorizontalAlignment.Center
}
pdf.ApplyStamp(textStamper)
pdf.SaveAs("modified.pdf")
End Sub
End Module
PDFSharp wymaga otwarcia pliku PDF za pomocą PdfReader.Open() z określeniem PdfDocumentOpenMode.Modify, uzyskania dostępu do strony, utworzenia obiektu XGraphics, utworzenie XFont ze stylem oraz użycie DrawString() z XPoint określającym dokładne współrzędne X,Y (200, 400).
IronPDF upraszcza to dzięki PdfDocument.FromFile(), obiektowi TextStamper z właściwościami deklaratywnymi (Text, FontSize, Color, VerticalAlignment, HorizontalAlignment) oraz ApplyStamp(). Nie są potrzebne żadne obliczenia współrzędnych — wystarczy określić wyrównanie, aIronPDFzajmie się pozycjonowaniem. Należy pamiętać, że przestrzeń nazw IronPdf.Editing jest wymagana do działania funkcji stemplowania.
Przykład 3: Tworzenie pliku PDF z obrazami
Przed (PDFSharp):
// NuGet: Install-Package PdfSharp
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 PdfSharp
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");
}
}
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
Po (IronPDF):
// NuGet: Install-Package IronPdf
using IronPdf;
using System;
class Program
{
static void Main()
{
// 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()
{
// 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);
}
}
Imports IronPdf
Imports System
Class Program
Shared Sub Main()
' 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 Class
PDFSharp wymaga utworzenia nowego PdfDocument, dodania PdfPage, pobrania XGraphics, załadowania XImage z pliku, obliczenie szerokości i wysokości, użycie DrawImage() z dokładnymi współrzędnymi (50, 50, 200, 200), a następnie osobne dodanie tekstu za pomocą DrawString().
IronPDF wykorzystuje standardowy kod HTML z tagiem <img> oraz stylizację CSS (style='width:200px; height:200px;'). Nie są potrzebne żadne obliczenia współrzędnych — układ strony obsługuje CSS.IronPDFudostępnia również ImageStamper do dodawania obrazów do istniejących plików PDF z deklaratywnymi właściwościami wyrównania. Dowiedz się więcej z naszych samouczków.
Ważne uwagi dotyczące migracji
Zmiana paradygmatu: współrzędne do HTML/CSS
Najważniejszą zmianą jest przejście z rysowania opartego na współrzędnych na HTML/CSS:
// PDFSharp: Podręcznik positioning nightmare
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: Podręcznik positioning nightmare
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: Podręcznik positioning nightmare
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)
Migracja czcionek
// PDFSharp: XFont objects
var titleFont = new XFont("Arial", 24, XFontStyle.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
var titleFont = new XFont("Arial", 24, XFontStyle.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>";
Dim titleFont As New XFont("Arial", 24, XFontStyle.Bold)
Dim bodyFont As New XFont("Times New Roman", 12)
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>"
Zmiana w ładowaniu dokumentów
// 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
Imports IronPDF
' PDFSharp: PdfReader.Open()
Dim document As PdfDocument = PdfReader.Open("existing.pdf", PdfDocumentOpenMode.Modify)
' IronPDF: PdfDocument.FromFile()
Dim pdf = PdfDocument.FromFile("existing.pdf")
Zmiana metody zapisywania
// 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");
Zmiana dostępu do strony
// PDFSharp: document.Pages[0]
PdfPage page = document.Pages[0];
// IronPDF: Automatyczne page handling or pdf.Pages[0]
// Pages are created automatically from HTML content
// PDFSharp: document.Pages[0]
PdfPage page = document.Pages[0];
// IronPDF: Automatyczne 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: Automatyczne page handling or pdf.Pages(0)
' Pages are created automatically from HTML content
Nowe możliwości po migracji
Po migracji doIronPDFzyskujesz możliwości, których PDFSharp nie jest w stanie zapewnić:
Natywny HTML do 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>")
URL do pliku PDF
var pdf = renderer.RenderUrlAsPdf("https://example.com");
var pdf = renderer.RenderUrlAsPdf("https://example.com");
Dim pdf = renderer.RenderUrlAsPdf("https://example.com")
Łączenie plików 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)
Znaki wodne w 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>")
Podsumowanie porównania funkcji
| Funkcja | PDFSharp | IronPDF |
|---|---|---|
| Rysowanie oparte na współrzędnych | ✓ | ✗(użyj HTML) |
| HTML do PDF | ✗ | ✓ |
| Obsługa CSS3 | ✗ | ✓ |
| Układ Flexbox/Grid | ✗ | ✓ |
| Stemplowanie tekstu | Podręcznik XGraphics | TextStamper |
| Oznaczanie obrazów | Podręcznik XImage | ImageStamper |
| Łączenie plików PDF | Podręcznik | ✓ |
| URL do pliku PDF | ✗ | ✓ |
| Nowoczesne renderowanie stron internetowych | ✗ | Silnik Chromium |
| Automatyczne podziały stron | ✗ | ✓ |
Lista kontrolna migracji
Przed migracją
- Sporządź spis wszystkich miejsc użycia biblioteki PDFSharp w kodzie źródłowym
- Określ generowane typy dokumentów (raporty, faktury, certyfikaty)
- Zwróć uwagę na wszelkie niestandardowe elementy graficzne lub operacje rysowania
- Zaplanuj przechowywanie klucza licencyjnegoIronPDF(zalecane są zmienne środowiskowe)
- Najpierw przetestuj z Licencją Trial IronPDF
Zmiany w pakiecie
- Usuń pakiet NuGet
PdfSharp - Usuń pakiet NuGet
PdfSharp-wpf, jeśli jest używany - Usuń pakiet NuGet
PdfSharp.Charting, jeśli jest używany - Zainstaluj pakiet NuGet
IronPdf:dotnet add package IronPdf
Zmiany w kodzie
- Zaktualizuj importy przestrzeni nazw (
using PdfSharp.Pdf;→using IronPdf;) - Dodaj
using IronPdf.Editing;w celu zapewnienia funkcji stemplowania - Konwersja układów opartych na współrzędnych do HTML/CSS
- Zastąp
XFontwłaściwościami czcionek CSS - Zastąp
XPenkolorami/obramowaniami CSS - Zastąp
XGraphics.DrawString()elementami tekstowymi HTML - Zastąp
XGraphics.DrawImage()tagami HTML<img> - Zastąp
PdfReader.Open()przezPdfDocument.FromFile() - Zastąp
document.Save()przezpdf.SaveAs() - Przekształć kod rysowania tabel w tabele HTML
Po migracji
- Wizualne porównanie wygenerowanych plików PDF
- Testowanie dokumentów wielostronicowych
- Sprawdź renderowanie czcionek
- W razie potrzeby dodaj nowe funkcje (konwersja HTML do PDF, scalanie, znaki wodne)

