在 'showpage' 之前注入 PostScript 代码

Inject PostScript code before 'showpage'

我想在激光打印机上打印一本书,所以我准备了一个 PostScript 文件以供打印,其中包含重新排序和 2-up 的页面。现在我想在适当的页面上添加小册子标记,如下图所示:

从其他 SO 问题中我知道它的 showpage 命令分隔 PS 文件中的各个页面,所以我编写了简单的 Perl 脚本来计算 showpage 的出现次数并在 PostSript 前面加上必要时编写代码,只是为了测试这种方法是否有效:

#!/usr/bin/env perl
use strict;
use warnings;

my $bookletSheets = 6;

my $occurence = 1;
while (my $line = <>) {
    if ($line !~ /^showpage/) {
        print $line;
        next;
    }

    my $mod = $occurence % (2*$bookletSheets);
    if ($mod == 1) {
        print " 1    setlinewidth\n";
        print " 5  5 newpath moveto\n";
        print "-5  5 lineto\n";
        print "-5 -5 lineto\n";
        print " 5 -5 lineto\n";
        print " 5  5 lineto\n";
        print "0 setgray\n";
        print "stroke\n";
        print "%NOP\n"
    }

    print $line;
    $occurence++;
}

但在运行之后:

$ cat book.ps | ./preprocess.pl > book-marked.ps

尽管代码已正确注入,但我在文档查看器中看不到任何其他标记的迹象。我做错了什么?

我的想法基于一些链接:

经过一番排查,原来是BoundingBox裁剪导致的图像缺失。边界框已移动,因为 PS 文件是从 PDF 中获取的,带有剥离边距。