【发布时间】:2020-07-26 05:28:38
【问题描述】:
我需要使用 Ghostscript 在 PDF 文档每一页的左下角添加一个白色矩形和一些文本。为此,我创建了以下 Postscript 脚本:
<<
/EndPage
{
2 eq { pop false }
{
newpath
0 0 moveto
0 20 lineto
200 20 lineto
200 0 lineto
closepath
%%gsave
1 setgray
fill
%%grestore
1 setlinewidth
0 setgray
stroke
gsave
/Times-Roman 9 selectfont
30 5 moveto
(My text) show
grestore
true
} ifelse
} bind
>> setpagedevice
与 Ghostscript 命令结合使用时效果很好:
gs -dBATCH -dNOPAUSE -sDEVICE=pdfwrite -sOutputFile=output.pdf my_script.ps input.pdf
但是,如果 input.pdf 处于横向模式,则白框和文本将打印在左上角而不是左下角。我可以通过添加:
90 rotate 0 -595 translate
但我无法确定页面何时处于横向模式与纵向模式。我可以获得页面宽度和高度,但即使对于横向模式页面,宽度也小于高度。我尝试了以下方法,但失败了:
/orient currentpagedevice /Orientation get def
我已经被这个问题困扰了一段时间。非常感谢任何帮助!
(Ghostscript 版本为 9.25)
[更新]
为了说明横向模式下页面的宽度如何小于高度,这是我正在使用的 script.ps:https://gist.github.com/irinkaa/9faadf30b3a5a381a0b621d72b712020
这里是input.pdf 和output.pdf。如您所见,612.0 - 792.0 打印在输出文件中,显示宽度 (612)
当我在输出文件上重新运行相同的命令时,它会打印相同的宽度和高度值,但该框随后会正确放置在左下角。
当我在脚本中添加以下内容时:
/orient currentpagedevice /Orientation get def
我收到一条提示未设置方向的错误消息(如果我理解正确的话):
Error: /undefined in --get--
Operand stack:
orient --dict:212/312(ro)(L)-- Orientation
Execution stack:
%interp_exit .runexec2 --nostringval-- --nostringval-- --nostringval-- 2 %stopped_push --nostringval-- --nostringval-- --nostringval-- false 1 %stopped_push 1999 1 3 %oparray_pop 1998 1 3 %oparray_pop 1982 1 3 %oparray_pop 1868 1 3 %oparray_pop --nostringval-- %errorexec_pop .runexec2 --nostringval-- --nostringval-- --nostringval-- 2 %stopped_push --nostringval--
Dictionary stack:
--dict:977/1684(ro)(G)-- --dict:0/20(G)-- --dict:80/200(L)--
Current allocation mode is local
Current file position is 151
GPL Ghostscript 9.25: Unrecoverable error, exit code 1
【问题讨论】:
标签: pdf ghostscript postscript