2011-08-18 91 views
7

我有一个名字类似约250单页的PDF文件:结合pdf文件与ghostscript,如何包含原始文件名?

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 

它完美相结合他们按正确的顺序。但是,当我看完finished.pdf时,我想要一个参考,告诉我每个页面的原始文件名。

有没有人有任何建议?我可以添加引用文件的页面名称吗?

+0

的Python脚本采取在这里似乎有希望:http://blog.tremily.us/posts/PDF_bookmarks_with_Ghostscript/ – Geremia

回答

7

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

这是通过使用'pdfmark'蒸馏器操作符的PostScript完成的。例如,使用以下

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

其中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 

请注意,您还可以使用进行枚举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 

NB不指定filenameforall枚举的顺序,所以你可能要对列表进行排序 精读使用Ghostscript扩展名.sort(数组lt 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”。

+1

我真的很抱歉,但是这是非常糟糕的措辞。是否有机会澄清一下'tiger.eps'或'colorcir.ps'是什么或'1000'的用途? – puk

1

要在每个页面上打印文件名,您可以使用ghostscript和pdftk的组合。从https://superuser.com/questions/171790/print-pdf-file-with-file-path-in-footer

gs \ 
-o outdir\footer.pdf \ 
-sDEVICE=pdfwrite \ 
-c "5 5 moveto /Helvetica findfont 9 scalefont setfont (foobar-filename.pdf) show" 

pdftk \ 
foobar-filename.pdf \ 
stamp outdir\footer.pdf \ 
output outdir\merged_foobar-filename.pdf 
相关问题