【问题标题】:How do I get output file name containing page X of Y如何获取包含 Y 页 X 的输出文件名
【发布时间】:2014-10-21 10:05:26
【问题描述】:

我正在使用 ghostscript 将多页 pdf 转换为 png。我需要将 png 文件的输出文件名设为 pageXofY.png,其中 X 是页码,Y 是 pdf 中的总页数。

我知道我可以使用 page%d.png 来获取当前页码。

有人可以帮忙吗?我有很多多页 pdf,因此不能手动重命名 png 文件。

谢谢。

【问题讨论】:

    标签: ghostscript


    【解决方案1】:

    您无法在标准 Ghostscript 中执行此操作。主要原因是设备无法知道输入的页数。虽然这可以针对单个输入 PDF 文件确定,但如果您有 2 个输入文件,您如何知道(在处理文件 1 时)所有文件的总页数是多少?

    一旦确定了页数,您或许可以使用 PDF 解释器重置设备的 OutputFile。

    在/ghostpdl/gs/Resource/Init/pdf_main.ps 中查找程序/runpdfpagerange,程序终止:

           QUIET not { 
             (Processing pages ) print 1 index =only ( through ) print dup =only
             (.) = flush
           } if
         } ifelse
       } ifelse
    } bind def
    

    在 "}bind def" 前面,您可以尝试添加如下内容:

    10 LastPage cvs              %% (LastPage)
    dup length 15 add string     %% (LastPage) string
    dup (Page %d of ) exch copy  %% (LastPage) (Page %d of ) (Page %d of)
    dup 12 3 index               %% (LastPage) (Page %d of ) (Page %d of) 12 (LastPage)
    putinterval                  %% (LastPage) (Page %d of LastPage)
    dup 3 1 roll                 %% (Page %d of LastPage) (Page %d of LastPage) (LastPage)
    length 12 add                %% (Page %d of LastPage) (Page %d of LastPage) n
    (.png) putinterval           %% (Page %d of LastPage.png)
    << /OutputFile 3 1 roll>> setpagedevice
    

    哪个可能有效,我还没试过。

    或者,使用 Ghostscript 的 pdf_info.ps 确定输入 PDF 文件中的页数,然后根据 pdf_info.ps 返回的信息再次调用 Ghostscript 创建 OutputFile(例如 -sOutputFile="Page %d of 100 .png")

    更新

    pdf_info.ps 作为 Ghostscript 源代码的一部分分发,它不在预打包的 Windows 安装程序中,因此您需要获取源代码分发的副本(www.ghostscript.com,只需下载源代码而不是 Windows安装程序)。

    该文件可以在 ghostpdl/gs/toolbin 中找到,如果您使用文本编辑器打开它,您会在评论的顶部附近看到用法。这是一个输出示例:

    D:\tests>\ghostpdl\gs\debugbin\gswin32c -dNODISPLAY -sFile=01_001.pdf /ghostpdl/
    gs/toolbin/pdf_info.ps
    GPL Ghostscript GIT PRERELEASE 9.16 (2014-09-22)
    Copyright (C) 2014 Artifex Software, Inc.  All rights reserved.
    This software comes with NO WARRANTY: see the file PUBLIC for details.
    
            01_001.pdf has 9 pages
    
    Title: 571l_p
    Author: mwang
    Subject: 571l_p
    Keywords:
    Creator: Adobe PageMaker 6.52
    Producer: Acrobat Distiller 4.05 for Windows
    CreationDate: D:20001113140954
    ModDate: D:20010305115330-08'00'
    
    Page 1 MediaBox: [0 0 612 792] CropBox: [0 0 612 792]    Rotate = 0
    Page 2 MediaBox: [0 0 612 792] CropBox: [0 0 612 792]    Rotate = 0
    Page 3 MediaBox: [0 0 612 792] CropBox: [0 0 612 792]    Rotate = 0
    Page 4 MediaBox: [0 0 612 792] CropBox: [0 0 612 792]    Rotate = 0
    Page 5 MediaBox: [0 0 612 792] CropBox: [0 0 612 792]    Rotate = 0
    Page 6 MediaBox: [0 0 612 792] CropBox: [0 0 612 792]    Rotate = 0
    Page 7 MediaBox: [0 0 612 792] CropBox: [0 0 612 792]    Rotate = 0
    Page 8 MediaBox: [0 0 612 792] CropBox: [0 0 612 792]    Rotate = 0
    Page 9 MediaBox: [0 0 612 792] CropBox: [0 0 612 792]    Rotate = 0
    
    Fonts Needed that are not embedded (system fonts required):
        Arial
        Arial,Bold
    
    D:\tests>
    

    因此,您需要将其输出到文件并对其进行扫描,或者通过 grep 或类似的方式通过管道输出以查找页数。

    【讨论】:

    • 感谢您的意见..我必须承认,就 ghostscript 而言,我是一个新手,我不知道 pdfInterpreter。您能否详细说明ghostscript的 pdf_info.ps 解决方案?是否可以使用 pdf_info.ps 将页数传递给一个变量,然后再传递给 -sOutputFile?如果是这样,任何关于如何实现这一目标的指针?我必须在此处添加在 Windows 7 上使用 ghostscript(不确定该信息是否有帮助)。
    • 编辑了我的答案,因为它太复杂了,无法发表评论。
    猜你喜欢
    • 1970-01-01
    • 2016-10-17
    • 2014-05-16
    • 1970-01-01
    • 1970-01-01
    • 2013-02-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多