PdfSharpCore
Port of the PdfSharp library to .NET Core - largely removed GDI+ (only missing GetFontData - which can be replaced with freetype2)
Top Related Projects
iText for .NET is the .NET version of the iText library, formerly known as iTextSharp, which it replaces. iText represents the next level of SDKs for developers that want to take advantage of the benefits PDF can bring. Equipped with a better document engine, high and low-level programming capabilities and the ability to create, edit and enha
:camera: A fluent wrapper around System.Drawing for the processing of image files.
:camera: A modern, cross-platform, 2D Graphics library for .NET
Generate and edit PDF documents in your .NET applications using the open-source QuestPDF library and its C# Fluent API. Build invoices, reports and data exports with ease.
Quick Overview
PdfSharpCore is a .NET Core port of the popular PdfSharp library. It allows developers to create, manipulate, and render PDF documents programmatically in .NET Core applications. This project aims to provide cross-platform PDF functionality for modern .NET environments.
Pros
- Cross-platform compatibility with .NET Core
- Lightweight and easy to use
- Supports both creation and modification of PDF documents
- Active community and ongoing development
Cons
- Limited documentation compared to the original PdfSharp
- Some features from the original library may not be fully implemented
- Potential compatibility issues with complex PDF structures
- Performance may vary compared to native PDF libraries
Code Examples
Creating a simple PDF document:
using PdfSharpCore.Pdf;
using PdfSharpCore.Drawing;
using (var document = new PdfDocument())
{
var page = document.AddPage();
var gfx = XGraphics.FromPdfPage(page);
var font = new XFont("Arial", 20, XFontStyle.Bold);
gfx.DrawString("Hello, PdfSharpCore!", font, XBrushes.Black,
new XRect(0, 0, page.Width, page.Height), XStringFormats.Center);
document.Save("output.pdf");
}
Adding an image to a PDF:
using PdfSharpCore.Pdf;
using PdfSharpCore.Drawing;
using (var document = new PdfDocument())
{
var page = document.AddPage();
var gfx = XGraphics.FromPdfPage(page);
var image = XImage.FromFile("image.jpg");
gfx.DrawImage(image, 50, 50, 300, 200);
document.Save("output_with_image.pdf");
}
Creating a table in PDF:
using PdfSharpCore.Pdf;
using PdfSharpCore.Drawing;
using (var document = new PdfDocument())
{
var page = document.AddPage();
var gfx = XGraphics.FromPdfPage(page);
var font = new XFont("Arial", 12);
double y = 50;
for (int i = 0; i < 5; i++)
{
gfx.DrawRectangle(XPens.Black, 50, y, 100, 30);
gfx.DrawString($"Row {i + 1}", font, XBrushes.Black, new XRect(50, y, 100, 30), XStringFormats.Center);
y += 30;
}
document.Save("table_example.pdf");
}
Getting Started
-
Install the NuGet package:
dotnet add package PdfSharpCore
-
Add the necessary using statements to your C# file:
using PdfSharpCore.Pdf; using PdfSharpCore.Drawing;
-
Create a new PDF document and start adding content:
using (var document = new PdfDocument()) { var page = document.AddPage(); var gfx = XGraphics.FromPdfPage(page); // Add your content here document.Save("your_pdf_file.pdf"); }
Competitor Comparisons
iText for .NET is the .NET version of the iText library, formerly known as iTextSharp, which it replaces. iText represents the next level of SDKs for developers that want to take advantage of the benefits PDF can bring. Equipped with a better document engine, high and low-level programming capabilities and the ability to create, edit and enha
Pros of itext-dotnet
- More comprehensive PDF manipulation capabilities, including advanced features like digital signatures and PDF/A compliance
- Better documentation and commercial support options
- Actively maintained with regular updates and bug fixes
Cons of itext-dotnet
- Commercial licensing required for many use cases, which can be costly
- Steeper learning curve due to its extensive feature set
- Larger library size, potentially increasing application footprint
Code Comparison
itext-dotnet:
using iText.Kernel.Pdf;
using iText.Layout;
using iText.Layout.Element;
using var writer = new PdfWriter("output.pdf");
using var pdf = new PdfDocument(writer);
using var document = new Document(pdf);
document.Add(new Paragraph("Hello, World!"));
PdfSharpCore:
using PdfSharpCore.Pdf;
using PdfSharpCore.Drawing;
using var document = new PdfDocument();
var page = document.AddPage();
using var gfx = XGraphics.FromPdfPage(page);
gfx.DrawString("Hello, World!", new XFont("Arial", 12), XBrushes.Black, 20, 20);
document.Save("output.pdf");
Both libraries offer PDF creation capabilities, but itext-dotnet provides a more structured approach with separate Document and Element classes, while PdfSharpCore uses a more direct drawing approach. itext-dotnet is generally more suitable for complex PDF manipulation tasks, while PdfSharpCore is lighter and easier to use for simpler PDF generation scenarios.
:camera: A fluent wrapper around System.Drawing for the processing of image files.
Pros of ImageProcessor
- Focuses specifically on image processing tasks, offering a wide range of image manipulation features
- Provides a fluent API for easy chaining of image processing operations
- Supports various image formats and color spaces
Cons of ImageProcessor
- Limited to image processing, lacking PDF-related functionality
- May have a steeper learning curve for users primarily interested in PDF manipulation
- Less actively maintained compared to PdfSharpCore
Code Comparison
ImageProcessor:
using (var image = new ImageFactory())
{
image.Load(sourceImage)
.Resize(new Size(150, 0))
.Format(new JpegFormat { Quality = 70 })
.Save(destImage);
}
PdfSharpCore:
using (var document = new PdfDocument())
{
var page = document.AddPage();
var gfx = XGraphics.FromPdfPage(page);
var font = new XFont("Arial", 20, XFontStyle.Bold);
gfx.DrawString("Hello, World!", font, XBrushes.Black, 20, 20);
document.Save("document.pdf");
}
The code examples demonstrate the different focus areas of each library. ImageProcessor provides a straightforward way to manipulate images, while PdfSharpCore offers tools for creating and modifying PDF documents.
:camera: A modern, cross-platform, 2D Graphics library for .NET
Pros of ImageSharp
- Broader image processing capabilities, including resizing, filtering, and drawing
- Active development with frequent updates and improvements
- Extensive documentation and community support
Cons of ImageSharp
- Focused solely on image processing, lacking PDF-specific functionality
- May require additional libraries for PDF-related tasks
- Potentially higher resource usage for simple image operations
Code Comparison
ImageSharp:
using SixLabors.ImageSharp;
using SixLabors.ImageSharp.Processing;
using (Image image = Image.Load("input.jpg"))
{
image.Mutate(x => x.Resize(256, 256));
image.Save("output.jpg");
}
PdfSharpCore:
using PdfSharpCore.Drawing;
using PdfSharpCore.Pdf;
using (var document = new PdfDocument())
{
var page = document.AddPage();
var gfx = XGraphics.FromPdfPage(page);
gfx.DrawImage(XImage.FromFile("input.jpg"), 0, 0);
document.Save("output.pdf");
}
Summary
ImageSharp excels in general image processing tasks, offering a wide range of features and active development. PdfSharpCore, on the other hand, specializes in PDF manipulation with built-in support for creating and modifying PDF documents. While ImageSharp provides more comprehensive image editing capabilities, PdfSharpCore is better suited for PDF-specific tasks. The choice between the two depends on the primary focus of your project: image processing or PDF manipulation.
Generate and edit PDF documents in your .NET applications using the open-source QuestPDF library and its C# Fluent API. Build invoices, reports and data exports with ease.
Pros of QuestPDF
- Fluent API design for easier and more intuitive PDF creation
- Built-in support for various document elements like tables, charts, and images
- Active development with frequent updates and community support
Cons of QuestPDF
- Relatively newer project with potentially fewer resources and examples
- May have a steeper learning curve for developers familiar with older PDF libraries
Code Comparison
QuestPDF:
Document.Create(container =>
{
container.Page(page =>
{
page.Content().PaddingVertical(50).Column(column =>
{
column.Item().Text("Hello World!").FontSize(36);
});
});
});
PdfSharpCore:
PdfDocument document = new PdfDocument();
PdfPage page = document.AddPage();
XGraphics gfx = XGraphics.FromPdfPage(page);
XFont font = new XFont("Arial", 36);
gfx.DrawString("Hello World!", font, XBrushes.Black, 20, 50);
Both libraries offer PDF creation capabilities, but QuestPDF provides a more modern and fluent approach, while PdfSharpCore follows a more traditional drawing-based method. QuestPDF's syntax is generally more concise and readable, but PdfSharpCore may offer more fine-grained control over document elements.
Convert
designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual CopilotREADME
PdfSharpCore
PdfSharpCore is a partial port of PdfSharp.Xamarin for .NET Standard. Additionally MigraDoc has been ported as well (from version 1.32). Image support has been implemented with SixLabors.ImageSharp and Fonts support with SixLabors.Fonts.
Table of Contents
Example
The following code snippet creates a simple PDF-file with the text 'Hello World!'. The code is written for a .NET 6 console app with top level statements.
using PdfSharpCore.Drawing;
using PdfSharpCore.Fonts;
using PdfSharpCore.Pdf;
using PdfSharpCore.Utils;
GlobalFontSettings.FontResolver = new FontResolver();
var document = new PdfDocument();
var page = document.AddPage();
var gfx = XGraphics.FromPdfPage(page);
var font = new XFont("Arial", 20, XFontStyle.Bold);
var textColor = XBrushes.Black;
var layout = new XRect(20, 20, page.Width, page.Height);
var format = XStringFormats.Center;
gfx.DrawString("Hello World!", font, textColor, layout, format);
document.Save("helloworld.pdf");
Contributing
We appreciate feedback and contribution to this repo!
License
This software is released under the MIT License. See the LICENSE file for more info.
PdfSharpCore relies on the following projects, that are not under the MIT license:
- SixLabors.ImageSharp and SixLabors.Fonts
- SixLabors.ImageSharp and SixLabors.Fonts, libraries which PdfSharpCore relies upon, are licensed under Apache 2.0 when distributed as part of PdfSharpCore. The SixLabors.ImageSharp license covers all other usage, see https://github.com/SixLabors/ImageSharp/blob/master/LICENSE
Top Related Projects
iText for .NET is the .NET version of the iText library, formerly known as iTextSharp, which it replaces. iText represents the next level of SDKs for developers that want to take advantage of the benefits PDF can bring. Equipped with a better document engine, high and low-level programming capabilities and the ability to create, edit and enha
:camera: A fluent wrapper around System.Drawing for the processing of image files.
:camera: A modern, cross-platform, 2D Graphics library for .NET
Generate and edit PDF documents in your .NET applications using the open-source QuestPDF library and its C# Fluent API. Build invoices, reports and data exports with ease.
Convert
designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual Copilot