将文件名放入许多 PDF 查看器可以显示的书签列表中相当容易。
这是通过 PostScript 使用“pdfmark”蒸馏器操作符完成的。例如,使用以下内容
gs -sDEVICE=pdfwrite -o finished.pdf control.ps
其中 control.ps 包含用于打印页面并输出书签 (/OUT) pdfmarks 的 PS 命令:
(examples/tiger.eps) run [ /Page 1 /Title (tiger.eps) /OUT pdfmark
(examples/colorcir.ps) run [ /Page 2 /Title (colorcir.ps) /OUT pdfmark
请注意,您还可以使用 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
注意,未指定 filenameforall 枚举的顺序,因此您可能需要对列表进行排序
使用 Ghostscript 扩展名 .sort ( array lt .sort lt ) 来控制顺序。
经过思考之后,我还意识到,如果输入文件有多于一页,则有一种更好的方法可以使用“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
上面创建了一个字符串数组(将它们复制到唯一的字符串对象,因为 filenameforall
只是覆盖给定的字符串),然后对其进行排序,最后处理字符串数组
使用 forall 运算符。通过使用 PageCount 设备属性来获取已生成的页面数,书签的页码 (PN) 将是正确的。我已将此代码段测试为“control.ps”。