【问题标题】:Ghostscript Multipage PDF to PNGGhostscript 多页 PDF 转 PNG
【发布时间】:2010-10-27 10:36:36
【问题描述】:

我一直在使用 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
            };

【问题讨论】:

    标签: pdf png ghostscript


    【解决方案1】:

    我最终将“%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
    }
    

    【讨论】:

    • 是否有可能在脚本/bash 级别执行而不用高级代码(如上面的 C#)进行连接。
    • @Supra man gs 有一节介绍使用-sOutputFile=%d 分别打印每一页
    【解决方案2】:

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

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

    在哪里

    • -density 144 决定 dpi 的分辨率,如果需要增加它,默认是 72
    • -type Grayscale 如果您的 PDF 没有颜色,请使用它,您将在生成的图像中节省一些 KBs

    【讨论】:

      【解决方案3】:

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

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

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

      【讨论】:

        猜你喜欢
        • 2016-03-11
        • 2013-12-27
        • 2011-06-10
        • 1970-01-01
        • 2010-11-01
        • 2011-09-08
        • 2017-06-02
        • 2013-10-12
        • 1970-01-01
        相关资源
        最近更新 更多