Multiple page PDF to one image with GhostScriptSharp

1.1k views Asked by At

I'm using GhostScriptSharp to generate images from PDF documents. It works for single pages, but what I'm after is getting it to generate one image from the whole document.

Here's my code:

GhostscriptWrapper.GenerateOutput(sourcePdfFilePath, destinationPngFilePath,
    new GhostscriptSettings
    {
        Device = GhostscriptDevices.pngalpha,
        Page = new GhostscriptPages
        {
            AllPages = true
        },
        Resolution = new Size
        {
            Height = 72,
            Width = 72
        },
        Size = new GhostscriptPageSize
        {
            Native = GhostscriptPageSizes.a4
        }
    }
);
2

There are 2 answers

1
KenS On

You need to do 'imposition' or 'n-up'. There's no provision for this as standard, you can do it, but you need to do some PostScript programming, and since the input is PDF that's going to make it trickier. You might be better advised to use a tool intended to do PDF imposition.

BTW how do you plan to get one image from a (for example) 1000 page document ? If that seems extreme, I've seen PDF files with more than 64k pages in them....

0
K J On

There have been updates since the above answer

If anyone wants just the simplest means to xyZ-Up (thats x pages wide then by y pages high, thus across and down) This is now easy using Ghostscript since version 9.54.

A typical command for pdf to pdf could be

gs -dNEWPDF -sDEVICE=pdfwrite -o 4perPage.pdf -sNupControl=2x2 -sDEFAULTPAPERSIZE=a4 -dFitPage -f in.pdf

So for this application it would be an image writer output

gs -sDEVICE=png16m -o 3perPage.png -sNupControl=3x1 -g900x800  -r120 -f in.pdf

And you will need to adjust scale sizes etc. for your own use https://www.ghostscript.com/doc/current/Use.htm

enter image description here