pdf - 将 PS 文件叠加在 PDF 文件之上

标签 pdf ghostscript postscript

我发现类似的问题已得到解答 Overlay two postscript files (command line approach)? ,但是,该解决方案对我不起作用,而且我的要求有点不同(我的文件之一是多页文件)

我有一个在我的控制之外生成的 PDF 文件 (DrawingSheet.pdf)。我有一个 PostScript 文件 (Table.ps),是通过使用 XSLFO 和 Apache FOP 转换 XML 文件生成的。 Table.ps 包含一个表。

DrawingSheet.pdf 有多个页面。 Table.ps 只有一页。 DrawingSheet.pdfTable.ps 都具有相同的纸张尺寸 - A 尺寸、横向。我想将 Table.ps 中的表格放置在 DrawingSheet.pdf 最后一页的顶部。我不想在 DrawingSheet.pdf 末尾“附加”表格页面作为附加页面。我正在使用命令行 GhostScript (v9.27)。

根据该帖子的建议,我编写了以下 Overlay.ps 文件:

/origshowpage /showpage load def
/showpage {} def
gsave
    (DrawingSheet.pdf) run
grestore
    (Table.ps) run
origshowpage

那是行不通的。我得到的是表格页面“附加”到 DrawingSheet.pdf 而不是覆盖在最后一页的顶部。

我做错了什么?我怎样才能实现我想要的?

已编辑-1:

如果两个文件中的页数相同有助于简化逻辑,那么我可以生成 Table.ps 使其页数与 DrawingSheet.pdf 相同文件并让表格仅出现在 Table.ps 的最后一页。

已编辑 2:

或者,我尝试了以下方法(类似于向 PDF 文件添加图像水印)。最终结果是我的空白页与 DrawingSheet.pdf 中的页数一样多。但有趣的是,当我加载空白 PDF 时,我可以看到 Acrobat Reader 在屏幕上闪烁表格,但当加载整个文件时,我最终看到的只是空白页面。

非常感谢任何解决此问题的帮助。

<<
    /EndPage
    {
        2 eq
        {
            pop false
        }
        {
            gsave
                1 dict begin
                    /showpage {} def
                    (Table.ps) run
                end
            grestore
            pop true
        } ifelse
    } bind
>> setpagedevice

Edited-2 - 附加说明:如果我从上面的代码中注释掉 gsavegrestore,那么我不会得到空白页但现在每个页面都被 Table.ps 中的表格“替换”(无覆盖)。

Edit-3 - 部分成功:

看到这篇文章后:Edit (every page of a) Postscript file manually ,我尝试过这种方法。我将 DrawingSheet.pdf 转换为 DrawingSheet.ps,确定最后一个 %%EndPageSetup 行,并在该行后面插入以下代码:

% BUNCH OF LINES NOT SHOWN %
%%BeginPageSetup
4 0 obj
<</Type/Page/MediaBox [0 0 792 612]
/Parent 3 0 R
/Resources<</ProcSet[/PDF]
/Font 10 0 R
>>
/Contents 5 0 R
/CropBox 
[0 0 792.0 612.0]
/BleedBox 
[0 0 792.0 612.0]
/TrimBox 
[0 0 792.0 612.0]
>>
endobj
%%EndPageSetup
%gsave                           % <== My Edits
    1 dict begin                 % <== My Edits
        /showpage {} def         % <== My Edits
        (Table.ps) run           % <== My Edits
    end                          % <== My Edits
%grestore                        % <== My Edits
5 0 obj
<</Length 30050>>stream
% BUNCH OF LINES NOT SHOWN %

通过此编辑(gsave 和 grestore 被注释掉 - 如果我取消注释它们,那么这不起作用),我将我的表格 super 强加在最后一页上。表格看起来不错,但是 pdf 中的原始最后一页在叠加后显示为翻转到 X 轴的中心。页面截图为:

enter image description here

您可以阅读的文本来自Table.ps,而翻转的文本是来自DrawingSheet.pdf最后一页的原始文本(当然原始pdf中的页面没有翻转)。

不确定我做错了什么以获得翻转的图像?

已编辑 4:

向“Edited-2”添加了附加注释

最佳答案

我尝试了你的尾页,终于让这种方法发挥作用。 EndPage 将原因代码 0 或 1 以及页数放在堆栈上(对于某些 pdf 来说不可靠),因此替代方案是/PageCount 变量,我将两者都显示。这些计算页数并在页末尾递增。

在页面完成之前,您需要知道在页面上放置叠加层的页数。 “finalpage”变量也可以在 Ghostscript 命令行上定义,而不是在文件中定义。使用 endpage.ps 和 DrawingSheet.ps 文件作为参数运行 Ghostscript。需要做更多的工作才能将其适应 DrawingSheet.pdf,除非这是可能的。

看看更改最终页码后是否有效。在执行 showpage 之前,table.ps 应放置在原始最后一页的顶部。

%!
/finalpage 9 def
<<
/EndPage {
       2 ne {
       1 add finalpage eq {(found last page\n)print (Table.ps)run} if
       currentpagedevice /PageCount get ==
       true
       } if
     }
>> setpagedevice

编辑:Postscript 语言引用手册 3 第 176 页这样说:

The PostScript interpreter maintains an implicit current page that accumulates the marks made by the painting operators. When a program begins, the current page is completely blank. As each painting operator executes, it places marks on the current page. Each new mark completely obscures any marks it may overlay. This method is known as a painting model: no matter what color a mark has—white, black, gray, or color—it is put onto the current page as if it were applied with opaque paint. Once the page has been completely composed, invoking the showpage operator renders the accumulated marks on the output media and then clears the page to white again.

关于pdf - 将 PS 文件叠加在 PDF 文件之上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66554567/

相关文章:

html - 使用 ghostscript 缩小 PDF 会导致图像不对齐

linux - tiff2ps 和 ps2pdf 无法缩放到 A4 纸张大小

postscript - 双面后记 - 强制新页面

performance - 加快 Postscript 图像打印速度

javascript - Kendo 导出的 PDF 页面上的边距

java - 如何将镜框安装到瑞士二维码发票上

pdf - 后记 : Reading a file, 写入 pdf

javascript - 将 HTML 字符串(包括样式)转换为 PDF(Electron - React - Typescript)

python - 使用 Python 脚本从 FTP 下载 .pdf 文件

pdf - PDF裁剪框中的坐标指的是什么尺寸?