Skip to content

Get started with PdfNetStack

View as Markdown

Install the package, run one call, and you have a smaller PDF that is proven equivalent to the original.

Add the PdfNetStack package and call the optimizer. That is the whole setup: no native binaries, no Ghostscript, nothing else to install.

Terminal window
dotnet add package PdfNetStack
compress.cs
#:package PdfNetStack@1.2.0
using PdfNetStack;
PdfOptimizationResult result = await PdfOptimizer.OptimizeFileAsync("manual.pdf", "manual.optimized.pdf");
Console.WriteLine($"{result.Disposition}: {result.OriginalBytes:N0} to {result.OutputBytes:N0} bytes, saved {result.SavingPercent:F1}%");
Console.WriteLine(result.Reason);

With the .NET 10 SDK you can also save the code above as compress.cs and run it with dotnet run compress.cs: the #:package line pulls the package in.

TargetRuns onDependencies
net8.0, net10.0.NET 8, 9, 10 and later. Trim and Native AOT compatibleNone
netstandard2.0.NET Framework 4.6.2 to 4.8.1, Mono, Unity, Xamarin, .NET Core 2.0+System.Memory (Microsoft, MIT)

The same managed code runs on Windows, Linux and macOS. CI runs the test suite on .NET Framework 4.8, .NET 8 and .NET 10. One difference: .NET Standard’s zlib has no level 9, so CompressionEffort.Maximum compresses a little less there.

PackageWhat it isOn NuGet
PdfNetStackThe library1.2.0
PdfNetStack.ToolThe pdfnetstack command line tool1.2.0
PdfNetStack.HtmlHTML and CSS to PDF1.2.0
PdfNetStack.HyphenationHyphenation patterns for paragraphs and hyphens: auto1.2.0
PdfNetStack.OcrOCR add-on for scanned PDFs1.2.0
PdfNetStack.Ocr.TesseractOCR through a tesseract you installed1.2.0
PdfNetStack.Ocr.ModelsThe OCR models, versioned on their own1.0.0
PdfNetStack.Ocr.ToolThe pdfnetstack-ocr command line tool, models included1.2.0
Terminal window
dotnet add package PdfNetStack # library
dotnet tool install -g PdfNetStack.Tool # command line: pdfnetstack

Open a document, change its pages, save it. Saving runs the optimizer and its verification by default, so edited files come out compact.

PdfDocument doc = PdfDocument.Open("contract.pdf");
doc.Pages.RemoveAt(0); // drop the cover
doc.Pages.Import(PdfDocument.Open("annex.pdf").Pages); // append another document
doc.Info.Title = "Contract with annex";
doc.Save("contract-final.pdf"); // optimized and verified by default

More in Edit pages.

Version 1.1 adds a drawing canvas. The origin is the top left corner of the page and units are points.

PdfDocument doc = PdfDocument.Create();
PdfPage page = doc.Pages.Add(PdfRectangle.A4);
using (PdfGraphics g = page.CreateGraphics()) // origin top left, y down, points
{
g.DrawRectangle(null, PdfColor.FromHex("#1E3A8A"), 0, 0, page.Width, 120);
g.DrawString("Hello, PDF", PdfStandardFont.HelveticaBold, 32, PdfColor.White, 40, 75);
g.DrawString("Made with PdfNetStack.", PdfStandardFont.Helvetica, 12, PdfColor.Black, 40, 160);
}
doc.Save("hello.pdf");

The page this code draws: a dark blue band with the words Hello, PDF

More in Drawing and Fonts and text.

Every feature on this site is in the 1.2 packages on NuGet. To try what is in the main branch before the next release, build against a clone of the repository. You need the .NET 10 SDK.

Terminal window
git clone https://github.com/jams4code/pdf.netstack
dotnet build pdf.netstack/PdfNetStack.sln -c Release

In a .NET 10 file-based app, reference the project instead of the package, here for a clone in the same folder as the file:

#:project pdf.netstack/src/PdfNetStack/PdfNetStack.csproj

In a project file, use a project reference, here for a clone next to the project folder:

<ItemGroup>
<ProjectReference Include="..\pdf.netstack\src\PdfNetStack\PdfNetStack.csproj" />
<!-- HTML to PDF -->
<ProjectReference Include="..\pdf.netstack\src\PdfNetStack.Html\PdfNetStack.Html.csproj" />
</ItemGroup>

Unreleased API can still change before its release. The changelog lists every change by version.

Every C# sample on this site is a file in the repository’s website/samples folder. CI compiles and runs each of them against the source the site was built from, checks that every PDF it writes opens again, and renders the images you see from those PDFs. The OCR samples are compiled only, because they need model files CI does not download.

Questions

Is PdfNetStack free for commercial use?
Yes. It is MIT licensed, and on .NET 8 and later the core package has no dependencies. On .NET Standard 2.0 it uses Microsoft's System.Memory, also MIT.
Which .NET versions does PdfNetStack support?
.NET 8, 9, 10 and later through the net8.0 and net10.0 targets, and .NET Framework 4.6.2 to 4.8.1, Mono and Unity through netstandard2.0.