All formats

Ghostscript.NET

LanguageC# (.NET Standard 2.0: .NET 6+, .NET Framework 4.6.1+)
LicenseAGPL-3.0 or commercial license from Artifex (Ghostscript itself is AGPL-3.0)
Repositorygithub.com/ArtifexSoftware/Ghostscript.NET
DocumentationREADME, Ghostscript command-line options
Depends onGhostscript native library (gsdll64.dll / libgs.so), SkiaSharp
Read / writeyes / yes (anything Ghostscript does: render, PS to PDF, PDF to PS, text)

Ghostscript.NET loads the Ghostscript native library and calls it through P/Invoke, in your process, without starting gs.exe. It has three main classes:

It works on Windows and Linux (x86 and x64). The Ghostscript library is AGPL-3.0, and so is Ghostscript.NET; closed-source use needs a commercial license from Artifex.

Installation#

Shell
dotnet add package Ghostscript.NET
# optional: ship Ghostscript with the app instead of requiring an install
dotnet add package Ghostscript.NativeAssets

Without Ghostscript.NativeAssets, install Ghostscript: the Windows installer (same bitness as your process) or sudo apt install ghostscript on Linux.

Basic usage: render PostScript pages to PNG#

C#
using Ghostscript.NET;
using Ghostscript.NET.Rasterizer;
using SkiaSharp;

var version = GhostscriptVersionInfo.GetPreferredVersion();

using var rasterizer = new GhostscriptRasterizer();
rasterizer.TextAlphaBits = 4;      // anti-aliasing
rasterizer.GraphicsAlphaBits = 4;
rasterizer.Open("in.ps", version, false);

Console.WriteLine($"pages: {rasterizer.PageCount}");
for (int page = 1; page <= rasterizer.PageCount; page++)  // 1-based
{
    using SKBitmap bitmap = rasterizer.GetPage(dpi: 150, pageNumber: page);
    using var stream = File.OpenWrite($"page-{page:D3}.png");
    bitmap.Encode(stream, SKEncodedImageFormat.Png, 100);
}

How to convert PostScript to PDF in C#

C#
using Ghostscript.NET.Processor;

using var processor = new GhostscriptProcessor();
processor.Process(new[]
{
    "-dBATCH", "-dNOPAUSE", "-dSAFER", "-dQUIET",
    "-sDEVICE=pdfwrite",
    "-sOutputFile=out.pdf",
    "in.ps",
});

For PDF to PostScript use -sDEVICE=ps2write.

How to extract text from a PostScript file#

The txtwrite device writes text; -sOutputFile=- sends it to stdout, which you capture with a GhostscriptStdIO subclass:

C#
using System.Text;
using Ghostscript.NET;
using Ghostscript.NET.Processor;

class Capture : GhostscriptStdIO
{
    public readonly StringBuilder Out = new();
    public override void StdIn(out string input, int count) => input = string.Empty;
    public override void StdOut(string output) => Out.Append(output);
    public override void StdError(string error) => Console.Error.Write(error);
}

var capture = new Capture();
using var processor = new GhostscriptProcessor();
processor.Process(new[]
{
    "-dBATCH", "-dNOPAUSE", "-dSAFER", "-dQUIET",
    "-sDEVICE=txtwrite", "-sOutputFile=-", "in.ps",
}, capture);
Console.WriteLine(capture.Out.ToString());

How to render PostScript from a stream#

C#
using var input = File.OpenRead("in.ps");
using var rasterizer = new GhostscriptRasterizer();
rasterizer.Open(input, GhostscriptVersionInfo.GetPreferredVersion(), false);
using SKBitmap first = rasterizer.GetPage(dpi: 96, pageNumber: 1);

How to use a specific Ghostscript DLL#

C#
var version = new GhostscriptVersionInfo(@"C:\MyApp\gs\gsdll64.dll");
using var rasterizer = new GhostscriptRasterizer();
rasterizer.Open("in.ps", version, false);

Tips#