18

我一直在使用 ghostscript 来做 pdf 以从 pdf 生成单个页面的图像。现在我需要能够从 pdf 中提取多个页面并生成一个长的垂直图像。

有没有我遗漏的论点允许这样做?

到目前为止,当我调用 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
            };
4

3 回答 3

11

我最终将“%d”添加到“OutputFile”参数中,以便每页生成一个文件。然后我只需阅读所有文件并将它们拼接到我的 c# 代码中,如下所示:

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
}
于 2009-05-19T20:47:57.200 回答
9

如果你可以使用 ImageMagick,你可以使用它的一个很好的命令:

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

在哪里

  • -density 144决定 dpi 的分辨率,如果需要增加它,默认为 72
  • -type Grayscale如果您的 PDF 没有颜色,请使用它,您将在生成的图像中保存一些 KB
于 2014-06-19T12:19:47.897 回答
1

首先检查输出设备选项;但我认为没有选择。

很可能你需要自己做一些拼版,或者让 GhostScript 去做(你必须编写一个 PostScript 程序),或者用 ImageMagick 或类似的东西缝合生成的渲染页面。

如果您想尝试 PostScript 路线(可能是最有效的),请查看 GhostScript 包中包含的 N-up 示例。

于 2009-05-19T19:59:11.040 回答