DimonSmart.PdfCropper 1.0.10

Prefix Reserved
There is a newer version of this package available.
See the version list below for details.
dotnet add package DimonSmart.PdfCropper --version 1.0.10
                    
NuGet\Install-Package DimonSmart.PdfCropper -Version 1.0.10
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="DimonSmart.PdfCropper" Version="1.0.10" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="DimonSmart.PdfCropper" Version="1.0.10" />
                    
Directory.Packages.props
<PackageReference Include="DimonSmart.PdfCropper" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add DimonSmart.PdfCropper --version 1.0.10
                    
#r "nuget: DimonSmart.PdfCropper, 1.0.10"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package DimonSmart.PdfCropper@1.0.10
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=DimonSmart.PdfCropper&version=1.0.10
                    
Install as a Cake Addin
#tool nuget:?package=DimonSmart.PdfCropper&version=1.0.10
                    
Install as a Cake Tool

DimonSmart.PdfCropper

Build and Test NuGet Version NuGet Downloads License

DimonSmart.PdfCropper is a cross-platform .NET library that intelligently trims PDF pages to actual content using two different methods. The library exposes a simple API through PdfSmartCropper.CropAsync, which accepts a PDF document in memory and returns a new PDF with recalculated CropBox and TrimBox on every non-empty page.

Example

Here's a visual example of what PdfCropper does - it removes unnecessary margins and whitespace:

Before Cropping After Cropping
Before Crop After Crop

Example pages from "Pragmatic Type-Level Design" by Alexander Granin

The CLI utility is particularly useful for reading e-books with minimal margins, making them more comfortable to read on tablets and e-readers by removing excessive whitespace around the content.

Platform Support

Windows - Full support
Linux - Full support
macOS - Full support
Other Unix systems - Compatible with .NET runtime

Target Frameworks: .NET 8.0, .NET 9.0

Installation

Install the package via NuGet:

dotnet add package DimonSmart.PdfCropper

Or via Package Manager Console:

Install-Package DimonSmart.PdfCropper

Features

  • Two cropping methods:
    • ContentBased (default): Analyzes PDF content (text, vectors, images) directly - fast and preserves quality
    • BitmapBased: Renders pages to images and analyzes pixels - more accurate for complex layouts
  • Preserves the existing content streams, metadata, fonts and resources
  • Leaves empty pages untouched and keeps MediaBox, BleedBox and ArtBox intact
  • Handles rotated pages and maintains deterministic output
  • Extensible logging through IPdfCropLogger interface
  • Built on top of iText 9.3 and PDFium (for bitmap rendering)

Usage

Basic Usage (ContentBased method - default)

using DimonSmart.PdfCropper;

byte[] cropped = await PdfSmartCropper.CropAsync(inputBytes, cancellationToken);

Using BitmapBased Method

using DimonSmart.PdfCropper;

byte[] cropped = await PdfSmartCropper.CropAsync(
    inputBytes, 
    CropMethod.BitmapBased, 
    logger: null,
    cancellationToken);

With Custom Logger

using DimonSmart.PdfCropper;

public class MyLogger : IPdfCropLogger
{
    public void LogInfo(string message) => Console.WriteLine($"[INFO] {message}");
    public void LogWarning(string message) => Console.WriteLine($"[WARN] {message}");
    public void LogError(string message) => Console.Error.WriteLine($"[ERROR] {message}");
}

var logger = new MyLogger();
byte[] cropped = await PdfSmartCropper.CropAsync(
    inputBytes, 
    CropMethod.ContentBased, 
    logger,
    cancellationToken);

The method throws PdfCropException with a specific PdfCropErrorCode when the input is invalid, encrypted or cannot be processed.

Command Line Utility

The repository includes a console application that wraps the library. This CLI tool is especially useful for preparing e-books and documents for comfortable reading on tablets and e-readers by removing excessive margins and whitespace.

Perfect for e-book readers: Transform PDF books with large margins into reader-friendly versions that utilize screen space more efficiently.

# Basic usage (ContentBased method)
dotnet run --project src/PdfCropper.Cli/PdfCropper.Cli.csproj -- input.pdf output.pdf

# With BitmapBased method
dotnet run --project src/PdfCropper.Cli/PdfCropper.Cli.csproj -- input.pdf output.pdf -m 1

# With verbose logging
dotnet run --project src/PdfCropper.Cli/PdfCropper.Cli.csproj -- input.pdf output.pdf -v

# All options combined
dotnet run --project src/PdfCropper.Cli/PdfCropper.Cli.csproj -- input.pdf output.pdf -m 1 -v
CLI Options
  • -m, --method <0|1> - Cropping method:
    • 0 = ContentBased (default, analyzes PDF content)
    • 1 = BitmapBased (renders to image, slower but more accurate)
  • -v, --verbose - Enable verbose logging

Cropping Methods Comparison

Feature ContentBased BitmapBased
Speed ⚡ Fast 🐌 Slower
Quality ✅ Preserves vector quality ✅ Preserves vector quality
Accuracy Good for standard documents Better for complex layouts
Use case Most PDFs PDFs with complex graphics

Development

  • Library target frameworks: .NET 8.0, .NET 9.0
  • Cross-platform support: Windows, Linux, macOS, and other Unix systems
  • Dependencies:
    • iText 9.3.0 (PDF manipulation)
    • PDFtoImage 5.1.1 (PDF to bitmap rendering) - includes native libraries for all platforms
    • SkiaSharp (image processing) - cross-platform 2D graphics
  • Tests are located in tests/PdfCropper.Tests and use xUnit
  • Build with dotnet build PdfCropper.sln
  • Run tests with dotnet test PdfCropper.sln
  • CI/CD: Automated builds and tests via GitHub Actions - see GitHub Actions Setup

Platform-Specific Notes

  • Linux: Requires libfontconfig1 and libgdiplus for optimal PDF rendering
  • macOS: No additional dependencies required
  • Windows: No additional dependencies required

API Reference

IPdfCropLogger Interface

public interface IPdfCropLogger
{
    void LogInfo(string message);
    void LogWarning(string message);
    void LogError(string message);
}

CropMethod Enum

public enum CropMethod
{
    ContentBased = 0,  // Analyzes PDF content
    BitmapBased = 1    // Renders to bitmap
}
Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 is compatible.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.6.0 195 10/23/2025
1.5.0 160 10/22/2025
1.4.0 171 10/22/2025
1.3.0 173 10/22/2025
1.2.1 156 10/21/2025
1.2.0 157 10/21/2025
1.1.2 193 10/20/2025
1.1.1 160 10/20/2025
1.1.0 159 10/20/2025
1.0.14 169 10/20/2025
1.0.13 106 10/18/2025
1.0.12 97 10/18/2025
1.0.11 83 10/17/2025
1.0.10 110 10/17/2025
1.0.9 120 10/17/2025
1.0.7 136 10/17/2025
1.0.6 159 10/16/2025
1.0.5 159 10/16/2025