合并两个后记时,ghostscript不保留页面级参数

问题描述

我已使用ghostscript将pdf文件转换为Postscript,而转换时,我为双面打印选项传递了页面级参数,如下所示。

gswin32c.exe -q -dSAFER -dnopAUSE -dBATCH -sDEVICE=output.ps \
  -c "<</PSPageOptions [ (<</Duplex false>> setpagedevice) 
  (<</Duplex true>> setpagedevice) (<</Duplex true>> setpagedevice) ] 
  /LockdistillerParams true>> setdistillerparams" -f input.pdf

请参考上述命令的解决方链接https://stackoverflow.com/a/64128881/13696415

现在,我为2个pdf文件添加了双工参数,并转换为2个独立的Postscript,问题是,当我将这些pdf与Ghostscript合并时,它将丢失我在转换为ps时传递的页面级参数。我在下面建议的答案中尝试合并后记。 https://stackoverflow.com/a/3445325/13696415 为什么合并时失去了添加的参数?合并时如何保留页面级参数?有人请帮助。

解决方法

当合并2个脚本文件时,我可以确认setpagedevice的%% BeginPageSetup条目丢失了。甚至/ LockDistillerParams也无法保存设置。仅使用ghostscript ps2write设备再次运行postscript文件会导致输出删除先前的设置。如果/ PSPageOptions缺少重做,我怀疑ghostscript每次都会重写它们。我不知道合并时保存设置的方法。

我尝试了另外两种方法,效果很好。

(1)合并2个Postscript文件,然后使用ps2write设备将所需的设置写入合并的Postscript文件。

gs -dBATCH -dNOPAUSE -sDEVICE=ps2write -sOutputFile=merged.ps -f file1.pdf file2.pdf
gs -dBATCH -dNOPAUSE -sDEVICE=ps2write -sOutputFile=merged-out.ps -c ' << /PSPageOptions [ (<</Duplex false>> setpagedevice) (<</Duplex true>> setpagedevice) (<</Duplex true>> setpagedevice) ] /LockDistillerParams true >> setdistillerparams ' -f merged.ps

(2)使用psdscript通过ps2write设备和/ PSPageOptions setdistillerparams合并所有2个pdf文件,以实现多合一操作。我发现这仅适用于某些pdf文件。如果pdf文件是使用Firefox使用的cairographics库生成的,则该文件不起作用,即使重新安装了ghostscript也是如此。

我在这里的测试是针对两个12页表现良好的pdf文件。结果根据需要在第13页显示%page3字符串。可以根据需要更改字符串以使用setpagedevice:

gs -dBATCH -dNOPAUSE -sDEVICE=ps2write -sOutputFile=file1+2.ps -c '<< /PSPageOptions [(% page1)(% page2)(% page3)(% page4)(% page5)] /LockDistillerParams true >>setdistillerparams' -f file1.pdf file2.pdf

P.S。请编辑您的原始信息以显示正确的sDEVICE标注。反斜杠可以根据用户的需要省略。