How to Convert PDF to PostScript in .NET with C#

Convert PDF documents to PostScript with .NET with C# and configure page, scaling, annotation, and output settings.
Share this page

This .NET walkthrough uses C# and HttpClient to submit the request. It converts a PDF into PostScript through /postscript with visible output controls for a production workflow. The complete sample is included below so .NET with C# developers can see the file handling, request construction, and response path together.

Why PDF to PostScript in .NET with C#?

.NET applications can generate PostScript for established production systems without making users export through a local print driver. The request accepts a PDF and returns a separate managed PostScript output.

A C# batch process may submit a report PDF with Level 3 output, selected pages, shrink-to-fit behavior, and annotation handling chosen for a print pipeline. Those decisions are captured in the request rather than left to the machine running the job.

The source PDF is not overwritten. That separation lets the application retain an approved original and track the generated PostScript file as a downstream deliverable.

C# Code Example for PDF to PostScript

/*
 * What this sample does:
 * - Converts PDF to PostScript through the /postscript endpoint.
 * - Pair with /pdf for PDF refrying: PDF -> PostScript -> PDF. Some print and prepress
 *   workflows use this lossy roundtrip to rebuild, flatten, or normalize page content.
 * - Requests Level 3, text-safe output, all pages at original scale, shrink-to-fit
 *   without rotation, and printable annotations.
 *
 * Setup (environment):
 * - Set PDFREST_API_KEY=your_api_key_here
 * - Optional: set PDFREST_URL to override the API region.
 *
 * Usage:
 *   dotnet run -- postscript-multipart 
 */
using System.Net.Http.Headers;

namespace Samples.EndpointExamples.MultipartPayload;

public static class Postscript
{
    public static async Task Execute(string[] args)
    {
        var inputPath = args.Length > 0 ? args[0] : "/path/to/sample.pdf";
        var apiKey = Environment.GetEnvironmentVariable("PDFREST_API_KEY");
        if (string.IsNullOrWhiteSpace(apiKey)) throw new InvalidOperationException("Missing PDFREST_API_KEY");
        var baseUrl = Environment.GetEnvironmentVariable("PDFREST_URL") ?? "https://api.pdfrest.com";
        using var client = new HttpClient { BaseAddress = new Uri(baseUrl) };
        client.DefaultRequestHeaders.TryAddWithoutValidation("Api-Key", apiKey);
        client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
        using var form = new MultipartFormDataContent();
        var inputContent = new ByteArrayContent(await File.ReadAllBytesAsync(inputPath));
        inputContent.Headers.ContentType = new MediaTypeHeaderValue("application/pdf");
        form.Add(inputContent, "file", Path.GetFileName(inputPath));
        form.Add(new StringContent("3"), "ps_level");
        form.Add(new StringContent("all"), "page_range");
        form.Add(new StringContent("false"), "binary_output");
        form.Add(new StringContent("1"), "scale");
        form.Add(new StringContent("false"), "rotate");
        form.Add(new StringContent("true"), "shrink_to_fit");
        form.Add(new StringContent("true"), "print_annotations");
        form.Add(new StringContent("postscript_from_pdf"), "output");
        var response = await client.PostAsync("postscript", form);
        var result = await response.Content.ReadAsStringAsync();
        Console.WriteLine(result);
        if (!response.IsSuccessStatusCode) Environment.ExitCode = 1;
    }
}

Source: View the sample on GitHub

Breaking Down the Code

Configure the service and input

var inputPath = args.Length > 0 ? args[0] : "/path/to/sample.pdf";
        var apiKey = Environment.GetEnvironmentVariable("PDFREST_API_KEY");
        if (string.IsNullOrWhiteSpace(apiKey)) throw new InvalidOperationException("Missing PDFREST_API_KEY");
        var baseUrl = Environment.GetEnvironmentVariable("PDFREST_URL") ?? "https://api.pdfrest.com";
        using var client = new HttpClient { BaseAddress = new Uri(baseUrl) };
        client.DefaultRequestHeaders.TryAddWithoutValidation("Api-Key", apiKey);
        client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

For this PDF to PostScript example, .NET with C# sends the multipart request to /postscript. The C# samples use framework HttpClient APIs and disposable content objects. API credentials come from PDFREST_API_KEY, keeping the secret out of the source file and request body. The sample’s placeholder path and API key make the moving pieces visible; replace them with a real local path and protected runtime credential before using the request.

Select PostScript Output Settings

var inputContent = new ByteArrayContent(await File.ReadAllBytesAsync(inputPath));
        inputContent.Headers.ContentType = new MediaTypeHeaderValue("application/pdf");
        form.Add(inputContent, "file", Path.GetFileName(inputPath));
        form.Add(new StringContent("3"), "ps_level");
        form.Add(new StringContent("all"), "page_range");
        form.Add(new StringContent("false"), "binary_output");
        form.Add(new StringContent("1"), "scale");
        form.Add(new StringContent("false"), "rotate");
        form.Add(new StringContent("true"), "shrink_to_fit");
        form.Add(new StringContent("true"), "print_annotations");
        form.Add(new StringContent("postscript_from_pdf"), "output");

The .NET with C# sample explicitly requests ps_level=3, all pages, binary output, no rotation, unit scale, shrink-to-fit, and printable annotations. Those choices describe the new PostScript result; they do not edit or replace the uploaded PDF.

Build the multipart request

client.DefaultRequestHeaders.TryAddWithoutValidation("Api-Key", apiKey);
        client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
        using var form = new MultipartFormDataContent();
        var inputContent = new ByteArrayContent(await File.ReadAllBytesAsync(inputPath));
        inputContent.Headers.ContentType = new MediaTypeHeaderValue("application/pdf");
        form.Add(inputContent, "file", Path.GetFileName(inputPath));
        form.Add(new StringContent("3"), "ps_level");
        form.Add(new StringContent("all"), "page_range");
        form.Add(new StringContent("false"), "binary_output");
        form.Add(new StringContent("1"), "scale");
        form.Add(new StringContent("false"), "rotate");

For the PDF to PostScript form, MultipartFormDataContent owns the uploaded streams and text values. The .NET runtime calculates the multipart boundary and content type when HttpClient sends the container.

Read the output resource

form.Add(new StringContent("true"), "shrink_to_fit");
        form.Add(new StringContent("true"), "print_annotations");
        form.Add(new StringContent("postscript_from_pdf"), "output");
        var response = await client.PostAsync("postscript", form);
        var result = await response.Content.ReadAsStringAsync();
        Console.WriteLine(result);
        if (!response.IsSuccessStatusCode) Environment.ExitCode = 1;
    }
}

After the PDF to PostScript call, the client reads the response text and reports non-success status through the process exit code. A successful JSON body identifies the managed output resource, while an unsuccessful body should be surfaced to the calling application.

Beyond the Tutorial

This .NET with C# example submitted a PDF to /postscript and selected the controls that shape the resulting PostScript file. Retain the returned resource ID whenever the output needs to be downloaded, monitored, or used by a following workflow step.

Try representative pdf to postscript inputs in API Lab. For the authoritative request fields, accepted values, and response contract, consult the pdfRest API Reference Guide. The same request pattern can then be adapted to your .NET with C# application’s error handling, retention policy, and delivery workflow.

For .NET with C# applications that already hold the source as a managed resource, the repository includes a JSON-payload PDF-to-PostScript example. It uploads the PDF separately and sends the returned ID with the PostScript options. View the JSON-payload sample on GitHub.

Generate a self-service API Key now!
Create your FREE API Key to start processing PDFs in seconds, only possible with pdfRest.