我大约有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
我正在使用以下命令将它们组合为单个pdf文件:
gs -dBATCH -dNOPAUSE -q -sDEVICE=pdfwrite -sOutputFile=finished.pdf file*pdf
它可以完美地工作,并以正确的顺序组合它们。但是,当我查看finish.pdf时,我想要一个引用,告诉我每一页的原始文件名。
有没有人有什么建议?我可以添加引用文件或其他内容的页面名称吗?
最佳答案
将文件名放入许多PDF查看器可以显示的书签列表中是很容易的。
这是通过PostScript使用“pdfmark”蒸馏器运算符完成的。例如,使用以下
gs -sDEVICE=pdfwrite -o finished.pdf control.ps
其中control.ps包含PS命令以打印页面并输出书签(/OUT)pdfmark:
(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(数组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”。
关于pdf - 将PDF文件与ghostscript结合使用,如何包含原始文件名?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7102090/