18

I've been using ghostscript to do pdf to image generation of a single page from the pdf. Now I need to be able to pull multiple pages from the pdf and produce a long vertical image.

Is there an argument that I'm missing that would allow this?

So far I'm using the following arguments when I call out to ghostscript:

string[] args ={
                "-q",                     
                "-dQUIET",                   
                "-dPARANOIDSAFER", // Run this command in safe mode
                "-dBATCH", // Keep gs from going into interactive mode
                "-dNOPAUSE", // Do not prompt and pause for each page
                "-dNOPROMPT", // Disable prompts for user interaction                           
                "-dFirstPage="+start,
                "-dLastPage="+stop,   
                "-sDEVICE=png16m",
                "-dTextAlphaBits=4",
                "-dGraphicsAlphaBits=4",
                "-r300x300",                

                // Set the input and output files
                String.Format("-sOutputFile={0}", tempFile),
                originalPdfFile
            };
Josh Bush
  • 2,708
  • 4
  • 24
  • 25

3 Answers3

11

I ended up adding "%d" to the "OutputFile" parameter so that it would generate one file per page. Then I just read up all of the files and stitched them together in my c# code like so:

var images =pdf.GetPreview(1,8); //All of the individual images read in one per file

using (Bitmap b = new Bitmap(images[0].Width, images.Sum(img=>img.Height))) {
    using (var g = Graphics.FromImage(b)) {
        for (int i = 0; i < images.Count; i++) {
            g.DrawImageUnscaled(images[i], 0, images.Take(i).Sum(img=>img.Height));
        }
    }
    //Do Stuff
}
Josh Bush
  • 2,708
  • 4
  • 24
  • 25
  • Is it somehow possible to do at script/bash level without wiring it up with high level code , like C# above. – Supra Dec 07 '15 at 09:57
  • @Supra `man gs` has a section on printing each page separately using `-sOutputFile=` with `%d` – dat Feb 20 '18 at 00:47
9

If you can use ImageMagick, you could use one of its good commands:

montage -mode Concatenate -tile 1x -density 144 -type Grayscale input.pdf output.png

where

  • -density 144 determins resolution in dpi, increase it if needed, default is 72
  • -type Grayscale use it if your PDF has no colors, you'll save some KBs in the resulting image
bluish
  • 26,356
  • 27
  • 122
  • 180
1

First check the output device options; but I don't think there's an option for that.

Most probably you'll need to do some imposition yourself, either make GhostScript do it (you'll have to write a PostScript program), or stitch the resulting rendered pages with ImageMagick or something similar.

If you want to try the PostScript route (likely to be the most efficient), check the N-up examples included in the GhostScript package.

Javier
  • 60,510
  • 8
  • 78
  • 126