Skip to content
 
 

Repository files navigation

PdfSharpCore

NuGet Version CI codecov.io

PdfSharpCore is a partial port of PdfSharp.Xamarin for .NET Standard. Additionally MigraDoc has been ported as well (from version 1.32). The core PdfSharpCore package carries no imaging or font dependency of its own. Pick a backend package and register it once at startup.

Backends

Package Backend License Notes
PdfSharpCore.Skia SkiaSharp MIT Default. Native library — see below.
PdfSharpCore.ImageSharp SixLabors.ImageSharp / Fonts Apache-2.0 Pinned to the Apache-2.0 licensed 2.1.x / 1.0.x lines — see below.

Register the backend before creating any font or loading any image:

using MigraDocCore.DocumentObjectModel.MigraDoc.DocumentObjectModel.Shapes;
using PdfSharpCore.Fonts;
using PdfSharpCore.Utils;

GlobalFontSettings.FontResolver = new SkiaFontResolver();
ImageSource.ImageSourceImpl = new SkiaImageSource();

Both throw a descriptive InvalidOperationException if you use them without registering a backend first.

Fonts

The resolver shipped with each backend discovers .ttf, .otf, .ttc and .otc in the platform's font directories, and every face of a collection separately. Fonts are always embedded — there is no setting for it and never has been, on any platform.

Two limits are worth knowing before you pick a font:

  • OpenType fonts with PostScript (CFF) outlines embed whole. TrueType fonts are subsetted down to the glyphs a document actually draws; CFF outlines cannot be, so an .otf goes in at its full size. For a CJK face that is megabytes per document.
  • A weight or slant a family ships no file for is drawn on, by stroking the glyphs and skewing them. It looks like what a word processor does in the same situation, which is to say noticeably worse than a real bold or italic. Ship the real faces where output quality matters.

SkiaSharp native assets

SkiaSharp is a native library, so an application using PdfSharpCore.Skia must also reference the native asset package for each platform it runs on. PdfSharpCore deliberately does not pull these in, so that you can choose the right Linux variant:

<PackageReference Include="SkiaSharp.NativeAssets.Win32" Version="4.150.1" />
<PackageReference Include="SkiaSharp.NativeAssets.Linux.NoDependencies" Version="4.150.1" />
<PackageReference Include="SkiaSharp.NativeAssets.macOS" Version="4.150.1" />

Use SkiaSharp.NativeAssets.Linux instead of ...NoDependencies if libfontconfig1 is available on your Linux image. The PdfSharpCore.ImageSharp backend is fully managed and needs none of this.

ImageSharp 3.x and later are not supported

PdfSharpCore.ImageSharp requires SixLabors.ImageSharp 2.1.x. SixLabors relicensed from Apache-2.0 to the Six Labors Split License in ImageSharp 3.0 and Fonts 2.0, so this package stays on the last Apache-2.0 versions rather than pushing that licence onto everyone who installs PdfSharpCore.

ImageSharp 3.0 is also not binary compatible with 2.1.x — it removed the Image.Load(..., out IImageFormat) overloads and made every encoder property init-only, which changes the setter signature. A build compiled against 2.1.x therefore fails at runtime with MissingMethodException if 3.x is loaded instead (#348).

The dependency is declared as the range [2.1.13,3.0.0), so a mismatch surfaces at restore rather than at runtime:

  • If ImageSharp 3.x arrives transitively, restore fails with NU1107 (version conflict).

  • If your project references ImageSharp 3.x directly, a direct reference wins and you get only a NU1608 warning. Treat it as an error, because the build will still crash at runtime:

    <WarningsAsErrors>$(WarningsAsErrors);NU1608</WarningsAsErrors>

Should it get through anyway, the backend reports the mismatch as a descriptive InvalidOperationException naming the loaded version instead of a bare MissingMethodException.

Only ImageSharp carries an upper bound. SixLabors.Fonts is referenced at 1.0.1 for the same licensing reason, but ImageSharpFontResolver uses the small part of its API that 2.x keeps unchanged, so resolving Fonts to 2.x is a licence decision for you to make rather than something that breaks at runtime.

If your application needs ImageSharp 3.x or later, use the PdfSharpCore.Skia backend instead — it has no ImageSharp dependency, so both can coexist in one project.

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 8 console app with top level statements.

using PdfSharpCore.Drawing;
using PdfSharpCore.Fonts;
using PdfSharpCore.Pdf;
using PdfSharpCore.Utils;

GlobalFontSettings.FontResolver = new SkiaFontResolver();

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");

Running the tests

dotnet test needs no setup. Ghostscript, used to rasterize PDFs for the visual comparison tests, comes from the Ghostscript.NativeAssets package, so there is nothing to install on Windows. On Linux and macOS ImageMagick invokes the system gs delegate instead, so install Ghostscript through your package manager (apt-get install ghostscript, brew install ghostscript) if you want those tests to run.

The visual comparison tests in XTextFormatterTest compare against reference images rendered on Linux. Text rasterizes differently elsewhere because a different set of system fonts is installed, so on other platforms they report as skipped with the reason rather than failing. CI runs on Linux and remains the authority on rendering; if you change text layout, check its result.

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

About

Port of the PdfSharp library to .NET Core - largely removed GDI+ (only missing GetFontData - which can be replaced with freetype2)

Resources

Stars

Watchers

Forks

Releases

Packages

Contributors

Languages