将PDF文件与ghostscript结合使用,如何包含原始文件名? [英] combining pdf files with ghostscript, how to include original file names?

查看:92
本文介绍了将PDF文件与ghostscript结合使用,如何包含原始文件名?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have about 250 single-page pdf files that have names like:

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文件:

I am using the following command to combine them to a single pdf file:

gs -dBATCH -dNOPAUSE -q -sDEVICE=pdfwrite -sOutputFile=finished.pdf file*pdf

它可以完美地工作,并以正确的顺序将它们组合在一起.但是,当我查看finish.pdf时,我想要一个参考,告诉我每一页的原始文件名.

It works perfectly, combining them in the correct order. However, when I am looking at finished.pdf, I want to have a reference that tells me the orignal filename for each page.

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

Does anyone have any suggestions? Can I add page names referencing the files or something?

推荐答案

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

It is fairly easy to put the file names into a list of Bookmarks which many PDF viewers can display.

这是通过PostScript使用"pdfmark" distiller运算符完成的.例如,使用以下

This is done with PostScript using the 'pdfmark' distiller operator. For example, use the following

gs -sDEVICE=pdfwrite -o finished.pdf control.ps

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

where control.ps contains PS commands to print the pages and output the bookmark (/OUT) pdfmarks:

(examples/tiger.eps) run [ /Page 1 /Title (tiger.eps) /OUT pdfmark
(examples/colorcir.ps) run [ /Page 2 /Title (colorcir.ps) /OUT pdfmark

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

Note that you can also perform the enumeration using PS to automate the entire process:

/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)来控制顺序.

NB that the order of filenameforall enumeration is not specified, so you may want to sort the list to control the order, using the Ghostscript extension .sort ( array lt .sort lt ).

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

Also after thinking about this, I also realized that if an imput file has more than one page, there is a better way to set the bookmark to the correct page number using the 'PageCount' device property.

[
  (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".

The above creates an array of strings (copying them to unique string objects since filenameforall just overwrites the string it is given), then sorts it, and finally processes the array of strings using the forall operator. By using the PageCount device property to get the count of pages already produced, the page number (PN) for the bookmark will be correct. I have tested this snippet as 'control.ps'.

这篇关于将PDF文件与ghostscript结合使用,如何包含原始文件名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆