DEV Community

Cover image for NetPdfKit: Make, Read, and Preview PDFs in .NET Without the Headache
Ssewannonda Keith Edwin
Ssewannonda Keith Edwin

Posted on

NetPdfKit: Make, Read, and Preview PDFs in .NET Without the Headache

Working with PDFs in .NET used to mean juggling a few different libraries. One to create files. One to read text. Another to turn pages into images. Each had its own style and its own quirks.

I got tired of that. So I built NetPdfKit. It puts all of that behind one simple, fluent API.

What is NetPdfKit?

NetPdfKit is a .NET library for creating, reading, and managing PDF files. Under the hood, it uses well-known open-source libraries: PDFsharp, PdfPig, PDFtoImage, ZXing.Net, and Svg.Skia. You don't need to learn each one. You learn one API, and it does the rest.

  • Works on Windows, macOS, and Linux
  • Supports .NET 8, 9, 10, and 11
  • MIT licensed

There are three packages. Let's go through them.

1. NetPdfKit (the main library)

This is the core. Install it like this:

dotnet add package NetPdfKit
Enter fullscreen mode Exit fullscreen mode

Create a PDF with a simple layout

You don't have to place every piece of text by hand. Describe the page, and NetPdfKit handles the flow and page breaks.

using NetPdf.Fluent;

Document.Create(doc => doc
    .Page(page => page
        .Size(PageSizes.A4)
        .Margin(50)
        .Header(h => h.Text("ACME Quarterly Report"))
        .Content(c => c.Text("Body text flows across pages on its own."))
        .Footer(f => f.AlignCenter().PageNumber("Page {number} of {total}"))))
    .Save("report.pdf");
Enter fullscreen mode Exit fullscreen mode

You also get tables, lists, rich text, QR codes, barcodes, and SVG images.

Read a PDF

using var doc = PdfFile.Open("report.pdf");
string text = doc.ExtractText();
Enter fullscreen mode Exit fullscreen mode

Merge, split, rotate, and lock PDFs

PdfFile.Merge(["a.pdf", "b.pdf"], "merged.pdf");

using var doc = PdfFile.Open("merged.pdf");
using var locked = doc.Protect(userPassword: "secret");
Enter fullscreen mode Exit fullscreen mode

Your original file is never changed. Every action gives you a new document back.

Turn a page into an image

byte[] png = doc.RenderPage(0, dpi: 150);
Enter fullscreen mode Exit fullscreen mode

A few extras that matter in real projects

  • Watermarks and letterheads
  • Digital signatures
  • Accessible (tagged) PDFs and PDF/A
  • Right-to-left text, including Arabic
  • Your own fonts, with a safe built-in fallback so PDFs still work in containers with no fonts installed

2. NetPdfKit.Previewer (see changes instantly)

Designing a PDF in code has one annoying part. You change a line, rebuild, open the file, and look. Then you repeat.

NetPdfKit.Previewer fixes that. It adds one method to your project: ShowInPreviewer().

dotnet add package NetPdfKit.Previewer
Enter fullscreen mode Exit fullscreen mode
Document.Create(doc => doc
    .Page(page => page
        .Content(c => c.Text("Hello, previewer!"))))
    .ShowInPreviewer(); // use this instead of .Save() while designing
Enter fullscreen mode Exit fullscreen mode

Run your app with dotnet watch run. Each time you save, the new PDF shows up in a window. If your code throws an error, the error shows in the window too. Your loop doesn't crash.

3. NetPdfKit.Previewer.App (the preview window)

This is the desktop window that shows your PDF. You install it once as a global tool:

dotnet tool install -g NetPdfKit.Previewer.App
Enter fullscreen mode Exit fullscreen mode

You need .NET 8 or newer. Make sure ~/.dotnet/tools is on your PATH. The .NET SDK usually sets this up for you.

You don't have to start it yourself. ShowInPreviewer() launches it if it isn't running. The window has scrolling pages, a thumbnail sidebar, zoom, fit-to-width, and a Save PDF button.

How the three fit together

Package What it does Do you need it?
NetPdfKit Create, read, and manage PDFs Yes, this is the main one
NetPdfKit.Previewer Adds ShowInPreviewer() to your code Optional, for live previews
NetPdfKit.Previewer.App The window that shows the preview Optional, install once as a tool

Try it

I made a small site that explains everything with examples: netpdfkitsite.netlify.app

You can also find the packages here:

If you try it, I'd love to hear what you think. Found a bug or want a feature? Open an issue on GitHub.

Top comments (0)