Migrate from PDFSharp to IronPDF

IronPDF in C#Photo from Unsplash

Originally Posted On: https://ironpdf.com/blog/migration-guides/migrate-from-pdfsharp-to-ironpdf/

How to Migrate from PDFSharp to IronPDF in C#

Migrating from PDFSharp to IronPDF transforms your PDF generation workflow from manual coordinate-based drawing to modern HTML/CSS templating. This guide provides a complete, step-by-step migration path that replaces tedious GDI+ style positioning with web technologies, dramatically reducing development time and making PDF generation maintainable through standard HTML/CSS skills.

Why Migrate from PDFSharp to IronPDF

Understanding PDFSharp

PDFSharp is renowned as a low-level PDF creation library, allowing developers to generate PDF documents through a programmatic approach. Released under the MIT license, PDFSharp grants the developer community freedom in usage and modification. PDFSharp primarily functions as a tool for drawing and compiling PDFs from scratch, which can both be beneficial and restrictive depending on the project’s nature.

PDFSharp is sometimes mistakenly assumed to be an HTML-to-PDF converter, which it is not. Its purpose is dedicated to programmatic PDF document creation only. While there is an add-on, HtmlRenderer.PdfSharp, intended to provide HTML rendering capabilities, it only supports CSS 2.1, with no support for modern CSS features like flexbox and grid.

The Coordinate Calculation Problem

PDFSharp’s GDI+ approach means you must:

  • Calculate exact X,Y positions for every element
  • Manually track content height for page overflow
  • Handle line wrapping and text measurement yourself
  • Draw tables cell by cell with border calculations
  • Manage multi-page documents with manual page breaks

PDFSharp’s architecture requires a deep understanding of positioning using coordinates, often posing challenges in creating complex layouts.

PDFSharp vs IronPDF Comparison

Feature PDFSharp IronPDF
License MIT (Free) Commercial
HTML to PDF Support No Yes (HTML5/CSS3 Support)
Modern CSS Support No (CSS 2.1 Only) Yes (Full CSS3)
Document Creation Coordinate-based drawing HTML/CSS templates
Layout System Manual X,Y positioning CSS Flow/Flexbox/Grid
Page Breaks Manual calculation Automatic + CSS control
Tables Draw cells individually HTML <table>
Styling Code-based fonts/colors CSS stylesheets
Document API Low-Level (Requires Coordinates) High-Level (Simplified API)
Updates Infrequent Regular

IronPDF shines in scenarios where HTML documents need conversion to PDFs with full fidelity. This .NET library supports HTML5 and CSS3, ensuring modern web standards are met. Its native HTML-to-PDF capabilities mean developers can take advantage of existing web content or templates designed with contemporary web tools.

For teams planning .NET 10 and C# 14 adoption through 2025 and 2026, IronPDF provides a modern approach that eliminates coordinate calculations while leveraging web development skills.

Before You Start

Prerequisites

  1. .NET Environment: .NET Framework 4.6.2+ or .NET Core 3.1+ / .NET 5/6/7/8/9+
  2. NuGet Access: Ability to install NuGet packages
  3. IronPDF License: Obtain your license key from ironpdf.com

NuGet Package Changes

  1. # Remove PDFSharp
  2. dotnet remove package PdfSharp
  3. dotnet remove package PdfSharp-wpf
  4. dotnet remove package PdfSharp.Charting
  5. # Add IronPDF
  6. dotnet add package IronPdf
SHELL

License Configuration

  1. // Add at application startup (Program.cs or Startup.cs)
  2. IronPdf.License.LicenseKey = "YOUR-LICENSE-KEY";

Identify PDFSharp Usage

  1. # Find all PDFSharp usages in your codebase
  2. grep -r "PdfSharp|XGraphics|XFont|XBrush|XPen" --include="*.cs" .
SHELL

Complete API Reference

Namespace Changes

  1. // Before: PDFSharp
  2. using PdfSharp.Pdf;
  3. using PdfSharp.Drawing;
  4. using PdfSharp.Pdf.IO;
  5. // After: IronPDF
  6. using IronPdf;
  7. using IronPdf.Editing;

Core API Mappings

PDFSharp API IronPDF API Notes
new PdfDocument() ChromePdfRenderer.RenderHtmlAsPdf() Create from HTML
document.AddPage() Automatic Pages created from HTML content
XGraphics.FromPdfPage() Not needed Use HTML elements
XGraphics.DrawString() HTML <p>, <h1>, etc. Position with CSS
XGraphics.DrawImage() HTML <img> tag Position with CSS
XFont CSS font-family, font-size Standard CSS
XBrush, XPen CSS colors/borders color, background-color
document.Save() pdf.SaveAs() Similar functionality
PdfReader.Open() PdfDocument.FromFile() Open existing PDF

Code Migration Examples

Example 1: HTML to PDF Conversion

Before (PDFSharp):

  1. // NuGet: Install-Package PdfSharp
  2. using PdfSharp.Pdf;
  3. using PdfSharp.Drawing;
  4. using System;
  5. class Program
  6. {
  7. static void Main()
  8. {
  9. // PDFSharp does not have built-in HTML to PDF conversion
  10. // You need to manually parse HTML and render content
  11. PdfDocument document = new PdfDocument();
  12. PdfPage page = document.AddPage();
  13. XGraphics gfx = XGraphics.FromPdfPage(page);
  14. XFont font = new XFont("Arial", 12);
  15. // Manual text rendering (no HTML support)
  16. gfx.DrawString("Hello from PDFSharp", font, XBrushes.Black,
  17. new XRect(0, 0, page.Width, page.Height),
  18. XStringFormats.TopLeft);
  19. document.Save("output.pdf");
  20. }
  21. }

After (IronPDF):

  1. // NuGet: Install-Package IronPdf
  2. using IronPdf;
  3. using System;
  4. class Program
  5. {
  6. static void Main()
  7. {
  8. // IronPDF has native HTML to PDF rendering
  9. var renderer = new ChromePdfRenderer();
  10. string html = "<h1>Hello from IronPDF</h1><p>Easy HTML to PDF conversion</p>";
  11. var pdf = renderer.RenderHtmlAsPdf(html);
  12. pdf.SaveAs("output.pdf");
  13. }
  14. }

This example highlights the most significant difference between the two libraries. PDFSharp explicitly states it “does not have built-in HTML to PDF conversion”—you must manually create a PdfDocument, add a PdfPage, get an XGraphics object, create an XFont, and use DrawString() with XRect coordinates.

IronPDF provides native HTML to PDF rendering through ChromePdfRenderer. The RenderHtmlAsPdf() method accepts HTML strings and converts them using a Chromium engine internally. IronPDF easily converts HTML files to PDF, preserving all styles defined in HTML5 and CSS3, eliminating the need for coordinate calculations. See the HTML to PDF documentation for comprehensive examples.

Example 2: Adding Text/Watermark to Existing PDF

Before (PDFSharp):

  1. // NuGet: Install-Package PdfSharp
  2. using PdfSharp.Pdf;
  3. using PdfSharp.Pdf.IO;
  4. using PdfSharp.Drawing;
  5. using System;
  6. class Program
  7. {
  8. static void Main()
  9. {
  10. // Open existing PDF
  11. PdfDocument document = PdfReader.Open("existing.pdf", PdfDocumentOpenMode.Modify);
  12. PdfPage page = document.Pages[0];
  13. // Get graphics object
  14. XGraphics gfx = XGraphics.FromPdfPage(page);
  15. XFont font = new XFont("Arial", 20, XFontStyle.Bold);
  16. // Draw text at specific position
  17. gfx.DrawString("Watermark Text", font, XBrushes.Red,
  18. new XPoint(200, 400));
  19. document.Save("modified.pdf");
  20. }
  21. }

After (IronPDF):

  1. // NuGet: Install-Package IronPdf
  2. using IronPdf;
  3. using IronPdf.Editing;
  4. using System;
  5. class Program
  6. {
  7. static void Main()
  8. {
  9. // Open existing PDF
  10. var pdf = PdfDocument.FromFile("existing.pdf");
  11. // Add text stamp/watermark
  12. var textStamper = new TextStamper()
  13. {
  14. Text = "Watermark Text",
  15. FontSize = 20,
  16. Color = IronSoftware.Drawing.Color.Red,
  17. VerticalAlignment = VerticalAlignment.Middle,
  18. HorizontalAlignment = HorizontalAlignment.Center
  19. };
  20. pdf.ApplyStamp(textStamper);
  21. pdf.SaveAs("modified.pdf");
  22. }
  23. }

PDFSharp requires opening the PDF with PdfReader.Open() specifying PdfDocumentOpenMode.Modify, accessing a page, creating an XGraphics object, creating an XFont with style, and using DrawString() with an XPoint specifying exact X,Y coordinates (200, 400).

IronPDF simplifies this with PdfDocument.FromFile(), a TextStamper object with declarative properties (Text, FontSize, Color, VerticalAlignment, HorizontalAlignment), and ApplyStamp(). No coordinate calculations needed—just specify alignment and IronPDF handles positioning. Note the IronPdf.Editing namespace is required for stamping functionality.

Example 3: Creating PDF with Images

Before (PDFSharp):

  1. // NuGet: Install-Package PdfSharp
  2. using PdfSharp.Pdf;
  3. using PdfSharp.Drawing;
  4. using System;
  5. class Program
  6. {
  7. static void Main()
  8. {
  9. // Create new PDF document
  10. PdfDocument document = new PdfDocument();
  11. PdfPage page = document.AddPage();
  12. XGraphics gfx = XGraphics.FromPdfPage(page);
  13. // Load and draw image
  14. XImage image = XImage.FromFile("image.jpg");
  15. // Calculate size to fit page
  16. double width = 200;
  17. double height = 200;
  18. gfx.DrawImage(image, 50, 50, width, height);
  19. // Add text
  20. XFont font = new XFont("Arial", 16);
  21. gfx.DrawString("Image in PDF", font, XBrushes.Black,
  22. new XPoint(50, 270));
  23. document.Save("output.pdf");
  24. }
  25. }

After (IronPDF):

  1. // NuGet: Install-Package IronPdf
  2. using IronPdf;
  3. using System;
  4. class Program
  5. {
  6. static void Main()
  7. {
  8. // Create PDF from HTML with image
  9. var renderer = new ChromePdfRenderer();
  10. string html = @"
  11. <h1>Image in PDF</h1>
  12. <img src='image.jpg' style='width:200px; height:200px;' />
  13. <p>Easy image embedding with HTML</p>";
  14. var pdf = renderer.RenderHtmlAsPdf(html);
  15. pdf.SaveAs("output.pdf");
  16. // Alternative: Add image to existing PDF
  17. var existingPdf = new ChromePdfRenderer().RenderHtmlAsPdf("<h1>Document</h1>");
  18. var imageStamper = new IronPdf.Editing.ImageStamper(new Uri("image.jpg"))
  19. {
  20. VerticalAlignment = IronPdf.Editing.VerticalAlignment.Top
  21. };
  22. existingPdf.ApplyStamp(imageStamper);
  23. }
  24. }

PDFSharp requires creating a new PdfDocument, adding a PdfPage, getting XGraphics, loading an XImage from file, calculating width and height, using DrawImage() with exact coordinates (50, 50, 200, 200), then separately adding text with DrawString().

IronPDF uses standard HTML with an <img> tag and CSS styling (style='width:200px; height:200px;'). No coordinate calculations needed—CSS handles layout. IronPDF also provides ImageStamper for adding images to existing PDFs with declarative alignment properties. Learn more in our tutorials.

Critical Migration Notes

Paradigm Shift: Coordinates to HTML/CSS

The most significant change is moving from coordinate-based drawing to HTML/CSS:

  1. // PDFSharp: Manual positioning nightmare
  2. gfx.DrawString("Invoice", titleFont, XBrushes.Black, new XPoint(50, 50));
  3. gfx.DrawString("Customer: John", bodyFont, XBrushes.Black, new XPoint(50, 80));
  4. // IronPDF: Let CSS handle layout
  5. var html = @"
  6. <div style='padding: 50px;'>
  7. <h1>Invoice</h1>
  8. <p>Customer: John</p>
  9. </div>";
  10. var pdf = renderer.RenderHtmlAsPdf(html);

Font Migration

  1. // PDFSharp: XFont objects
  2. var titleFont = new XFont("Arial", 24, XFontStyle.Bold);
  3. var bodyFont = new XFont("Times New Roman", 12);
  4. // IronPDF: CSS font properties
  5. var html = @"
  6. <style>
  7. h1 { font-family: Arial, sans-serif; font-size: 24px; font-weight: bold; }
  8. p { font-family: 'Times New Roman', serif; font-size: 12px; }
  9. </style>";

Document Loading Change

  1. // PDFSharp: PdfReader.Open()
  2. PdfDocument document = PdfReader.Open("existing.pdf", PdfDocumentOpenMode.Modify);
  3. // IronPDF: PdfDocument.FromFile()
  4. var pdf = PdfDocument.FromFile("existing.pdf");

Save Method Change

  1. // PDFSharp: document.Save()
  2. document.Save("output.pdf");
  3. // IronPDF: pdf.SaveAs()
  4. pdf.SaveAs("output.pdf");

Page Access Change

  1. // PDFSharp: document.Pages[0]
  2. PdfPage page = document.Pages[0];
  3. // IronPDF: Automatic page handling or pdf.Pages[0]
  4. // Pages are created automatically from HTML content

New Capabilities After Migration

After migrating to IronPDF, you gain capabilities that PDFSharp cannot provide:

Native HTML to PDF

  1. var renderer = new ChromePdfRenderer();
  2. var pdf = renderer.RenderHtmlAsPdf("<h1>Modern Web Content</h1>");

URL to PDF

  1. var pdf = renderer.RenderUrlAsPdf("https://example.com");

PDF Merging

  1. var pdf1 = PdfDocument.FromFile("document1.pdf");
  2. var pdf2 = PdfDocument.FromFile("document2.pdf");
  3. var merged = PdfDocument.Merge(pdf1, pdf2);

Watermarks with HTML

  1. pdf.ApplyWatermark("<h2 style='color:red;'>CONFIDENTIAL</h2>");

Feature Comparison Summary

Feature PDFSharp IronPDF
Coordinate-based Drawing ✗ (use HTML)
HTML to PDF
CSS3 Support
Flexbox/Grid Layout
Text Stamping Manual XGraphics TextStamper
Image Stamping Manual XImage ImageStamper
Merge PDFs Manual
URL to PDF
Modern Web Rendering Chromium Engine
Automatic Page Breaks

Migration Checklist

Pre-Migration

  • Inventory all PDFSharp usage in codebase
  • Identify document types being generated (reports, invoices, certificates)
  • Note any custom graphics or drawing operations
  • Plan IronPDF license key storage (environment variables recommended)
  • Test with IronPDF trial license first

Package Changes

  • Remove PdfSharp NuGet package
  • Remove PdfSharp-wpf NuGet package if used
  • Remove PdfSharp.Charting NuGet package if used
  • Install IronPdf NuGet package: dotnet add package IronPdf

Code Changes

  • Update namespace imports (using PdfSharp.Pdf;using IronPdf;)
  • Add using IronPdf.Editing; for stamping functionality
  • Convert coordinate-based layouts to HTML/CSS
  • Replace XFont with CSS font properties
  • Replace XBrush/XPen with CSS colors/borders
  • Replace XGraphics.DrawString() with HTML text elements
  • Replace XGraphics.DrawImage() with HTML <img> tags
  • Replace PdfReader.Open() with PdfDocument.FromFile()
  • Replace document.Save() with pdf.SaveAs()
  • Convert table drawing code to HTML tables

Post-Migration

  • Visual comparison of generated PDFs
  • Test multi-page documents
  • Verify font rendering
  • Add new capabilities (HTML to PDF, merging, watermarks) as needed