What Is PDFsharp and How to Use It in C#

authorPic

By Jenefey Aaron

2025-05-27 / Knowledge

I need to generate a PDF report in my C# application. Is PDFsharp a good choice for this?

If you're working with C# and need to create or modify PDF documents, you might have come across PDFsharp. This free, open-source .NET library is widely used for handling PDFs in C# applications. In this PDFsharp guide, we'll explore what PDFsharp is, how to install it, and how to use it to create and manipulate PDF files.

Part 1: What Is PDFsharp

PDFsharp is a free, open-source .NET library written in C# for creating and processing PDF documents. It allows developers to generate PDFs from scratch, modify existing ones, and draw graphics, text, and images onto PDF pages. Whether you're building a report generator, an invoice system, or any application that requires PDF output, PDFsharp provides the necessary tools.

Core Features of PDFsharp

  • Creating PDF Documents: Generate PDFs from scratch, adding text, images, and graphics.
  • Modifying Existing PDFs: Merge, split, and edit existing PDF files.
  • Drawing Capabilities: Draw shapes, lines, and text directly onto PDF pages.
  • Standards Support: Supports PDF/A and PDF/UA standards, ensuring compliance with archival and accessibility requirements.
  • Cross-Platform Compatibility: Works on Windows, Linux, and macOS, making it versatile for various development environments.

Licensing

PDFsharp is licensed under the MIT License, which means you can use it freely in both personal and commercial projects without any licensing fees. This permissive license encourages wide adoption and contribution from the developer community.

Part 2: How to Install PDFsharp in Your C# Project

Integrating PDFsharp into your C# project is straightforward, thanks to its availability via NuGet, the package manager for .NET.

Using NuGet Package Manager

  • Via Package Manager Console: Open the Package Manager Console in Visual Studio and run the following command:

    Install-Package PDFsharp -Version 6.1.1

  • Via .NET CLI: If you're using the .NET CLI, run:

    dotnet add package PDFsharp --version 6.1.1

Visual Studio Integration

After installation, you can start using PDFsharp by adding the necessary namespaces to your C# files:

using PDFsharp.PDF;

using PDFsharp.Drawing;

Target Frameworks

PDFsharp is compatible with various .NET versions, including:

  • .NET 6.0
  • .NET 8.0
  • .NET Framework 4.6.2

This ensures that you can use PDFsharp in both modern .NET Core applications and older .NET Framework projects.

Alternative Builds

Depending on your application's requirements, you might choose between different builds of PDFsharp:

  • PDFsharp-GDI: Suitable for Windows Forms applications.
  • PDFsharp-WPF: Designed for Windows Presentation Foundation (WPF) applications.

These builds provide tailored functionality to match the specific needs of different application types.

Part 3: How to Create a PDF Using PDFsharp in C#

To create a PDF file in C#, we will use the PDFsharp library. Follow these steps to add a reference to the PDFsharp library in your project:

  • Right-click on your project in the Solution Explorer, then select "Manage NuGet Packages..." from the context menu.
  • In the NuGet Package Manager, go to the Browse tab and search for PDFsharp.
  • Locate the official PDFsharp package and install the latest stable version by clicking Install.
  • Once installed, the necessary references to the PDFsharp library will be added to your project automatically.
create pdf using pdfsharp

Part 4: How to Convert HTML to PDF Using PDFsharp

I need some help resolving an issue when converting an HTML invoice to PDF using PDFsharp and HtmlRenderer. The output PDF renders about 75% of the content correctly, but does not include the item lines (i.e., Description, Quantity, Unit Price, Extended), which are present in the original HTML invoice.

Please see the attached source HTML and output PDF for a visual comparison PDFsharp HTML to PDF.

Environment & Libraries Used:

  • PDFsharp.dll version 1.32.3057.0
  • PDFsharp.Charting.dll version 1.32.3057.0
  • HtmlRenderer.dll version 1.5.0.6
  • HtmlRenderer.PDFsharp.dll version 1.5.0.6

Steps Taken:

  • Loaded the HTML content containing the invoice layout, including all item lines.
  • Used the following code to generate the PDF:

    PDFDocument PDF = PDFGenerator.GeneratePDF(html, PageSize.A4, 20, null, null, null);

    PDF.Save(HttpContext.Current.Server.MapPath("~/export/" + _PDF_FILENAME_TO_CREATE));

  • Saved the generated PDF to the server.
  • Verified that the item table (containing Description, Quantity, Unit Price, Extended) is missing in the resulting PDF.
pdfsharp html to pdf

Part 5: Best PDF Editor for Any Task - AI Integrated

While PDFsharp is a great tool for developers who want to create or edit PDFs through code, it may not be the best choice for everyday users or tasks that require manual adjustments. If you need to directly edit PDF content—like changing text, adding images, inserting watermarks, or converting files—then you'll benefit from using a dedicated PDF editor.

This is where Tenorshare PDNob becomes very useful. It's designed for all kinds of PDF tasks, offering over 100 editing tools. You can easily convert PDFs to Word, Excel, PowerPoint, and image formats.

Key Features of PDNob PDF Editor

  • Comprehensive Editing: Edit text, images, watermarks, links, and backgrounds in your PDFs.
  • Format Conversion: Convert PDFs to and from various formats, including Word, Excel, PPT, RTF, TXT, PNG, EPUB, JPEG, BMP, TIFF, and PDF/A.
  • Advanced Tools: Merge, split, compress, and protect PDFs with passwords and permissions.
  • AI Integration: Utilize AI-powered features for enhanced editing and document management.

How to Edit PDF with Tenorshare PDNob PDF Editor:

  • For the start, download and install the Tenorshare PDNob on your computer. Open the software to tap on "Open PDF," choosing a file on your PC.
  • open pdnob pdf editor software
  • Now, when you open your PDF, you can click "Edit". You can edit text, images, and various other things in your document.
  • edit pdf mac with pdnob pdf editor
  • Insert Stickers into PDF by clicking on the "Sticker" option. Moreover, add digital signatures to authenticate your documents. Page options for splitting, merging, cropping, or rearranging pages within your document.
  • edit pdf on mac freely with pdnob pdf editor

FAQs of PDFsharp

Is PDFsharp Free for Commercial Use?

Yes, it's free for both personal and commercial use under the MIT License.

Does PDFsharp Support Digital Signatures?

Yes, starting from version 6.2.0. You can add digital PDFsharp signature to new or existing PDFs.

Can PDFsharp Be Used on Web Servers?

Yes, the Core build can be used on any OS supported by .NET, including web servers.

Who Maintains PDFsharp?

PDFsharp is developed by empira Software GmbH. It's open-source and accepts community contributions on GitHub.

What PDF Versions Does PDFsharp Support?

It supports PDF versions 1.2 to 1.7 and 2.0. Most features of PDF 1.4 are fully supported, while some newer features are partially supported.

Conclusion

In this article, we explored what PDFsharp is and how to use it in C#. PDFsharp is a free and open-source library that makes it easy to create and manage PDF documents in .NET projects. You learned how to install PDFsharp, create PDFs from scratch, and even convert HTML to PDF using additional tools.

If you're looking for an easy way to edit and convert PDF documents outside of code, Tenorshare PDNob is a strong choice. It offers a full set of features for converting, editing, and protecting PDFs, all in a user-friendly interface.

The END

About PDNob

I am PDNob.
Swift editing, efficiency first.
Make every second yours: Tackle any PDF task with ease.
As Leonardo da Vinci said, "Simplicity is the ultimate sophistication." That's why we built PDNob.

Speak Your Mind

Registrer/ Login

then write your review

Speak Your Mind

Leave a Comment

Create your review for Tenorshare articles

Related articles

All topics

Tenorshare PDNob

AI-Powered PDNob PDF Editor

Smarter, Faster, Easier PDF Editor Software

AI-Powered PDNob PDF Editor

Edit & enhance PDF with Al