Ghostscript多页PDF转PNG

18

我一直在使用Ghostscript将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
            };
3个回答

11

最终我在“OutputFile”参数中添加了“%d”,这样它就会为每个页面生成一个文件。然后我只是读取所有文件并在我的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
}

有没有可能在脚本/ bash 级别上完成,而不需要将其与高级代码(如 C#)连接起来? - Supra
@Supra man gs 中有一节关于使用 -sOutputFile=%d 来分别打印每个页面的内容。 - dat

9

如果您能够使用ImageMagick,您可以使用其中一个好的命令:

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

这段内容是关于PDF转图片的命令行参数说明:

  • -density 144 指定dpi(每英寸点数),如果需要可以增加该值,72为默认值。
  • -type Grayscale 如果PDF没有颜色,使用该参数可以减小生成图片的文件大小。

1

首先请检查输出设备选项;但是我不认为有这样的选项。

很可能你需要自己进行一些排版操作,要么使用GhostScript来实现(你需要编写一个PostScript程序),要么使用ImageMagick或类似工具将渲染出来的页面拼接起来。

如果你想尝试使用PostScript方法(可能是最有效的方法),请检查GhostScript软件包中包含的N-up示例。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接