将pdf文件与ghostscript相结合,如何包含原始文件名?

Ste*_*hen 9 pdf ghostscript

我有大约250个单页pdf文件,其名称如下:

file_1_100.pdf,
file_1_200.pdf, 
file_1_300.pdf, 
file_2_100.pdf, 
file_2_200.pdf, 
file_2_300.pdf, 
file_3_100.pdf, 
file_3_200.pdf, 
file_3_300.pdf
...etc
Run Code Online (Sandbox Code Playgroud)

我使用以下命令将它们组合到单个pdf文件中:

gs -dBATCH -dNOPAUSE -q -sDEVICE=pdfwrite -sOutputFile=finished.pdf file*pdf
Run Code Online (Sandbox Code Playgroud)

它完美地工作,以正确的顺序组合它们.但是,当我查看finished.pdf时,我希望有一个引用,告诉我每个页面的orignal文件名.

有没有人有什么建议?我可以添加引用文件或其他内容的页面名称吗?

小智 7

将文件名放入许多PDF查看器可以显示的书签列表中相当容易.

这是使用PostScript使用'pdfmark'蒸馏器运算符完成的.例如,使用以下内容

gs -sDEVICE=pdfwrite -o finished.pdf control.ps
Run Code Online (Sandbox Code Playgroud)

其中control.ps包含PS命令以打印页面并输出书签(/ OUT)pdfmarks:

(examples/tiger.eps) run [ /Page 1 /Title (tiger.eps) /OUT pdfmark
(examples/colorcir.ps) run [ /Page 2 /Title (colorcir.ps) /OUT pdfmark
Run Code Online (Sandbox Code Playgroud)

请注意,您还可以使用PS执行枚举以自动执行整个过程:

/PN 1 def
(file*.pdf) {
  /FN exch def
  FN run
  [ /Page PN /Title FN /OUT pdfmark % do the file and bookmark it by filename
  /PN PN 1 add def % bump the page number
} 1000 string filenameforall
Run Code Online (Sandbox Code Playgroud)

请注意,未指定filenameforall枚举的顺序,因此您可能希望使用Ghostscript扩展名.sort(array lt .sort lt)对列表进行排序以控制顺序.

在考虑了这一点后,我也意识到如果一个imput文件有多个页面,有一种更好的方法可以使用'PageCount'设备属性将书签设置为正确的页码.

[
  (file*.pdf) { dup length string copy } 1000 string filenameforall
] % create array of filenames
{ lt } .sort % sort in increasing alphabetic order
/PN 1 def
{ /FN exch def
  /PN currentpagedevice /PageCount get 1 add def % get current page count done (next is one greater)
  FN run [ /Page PN /Title FN /OUT pdfmark % do the file and bookmark it by filename
} forall
Run Code Online (Sandbox Code Playgroud)

上面创建了一个字符串数组(将它们复制到唯一的字符串对象,因为filenameforall只是覆盖它给出的字符串),然后对它进行排序,最后使用forall运算符处理字符串数组.通过使用PageCount设备属性来获取已生成的页面数,书签的页码(PN)将是正确的.我已将此代码段测试为"control.ps".

  • 我非常抱歉,但措辞极差.我们是否有机会澄清一下`tiger.eps`或`colorcir.ps`是什么或'1000`是什么? (2认同)