I found similar question that was answered Overlay two postscript files (command line approach)?, However, that solution does not work for me and my requirements are bit different (one of my files is multi-page file)
I have a PDF File (DrawingSheet.pdf
) that is generated outside my control. I have a PostScript file (Table.ps
) that I generate by converting XML file using XSLFO and Apache FOP. Table.ps
contains one table.
The DrawingSheet.pdf
has multiple pages. Table.ps
has only one page. Both DrawingSheet.pdf
and Table.ps
has same paper size - A size, Landscape. I want to place that table from the Table.ps
on top of the last page of the DrawingSheet.pdf
. I don't want to "append" table page as additional page at the end of the DrawingSheet.pdf
. I am using command-line GhostScript (v9.27).
Per the suggestion from that post, I wrote following Overlay.ps file:
/origshowpage /showpage load def
/showpage {} def
gsave
(DrawingSheet.pdf) run
grestore
(Table.ps) run
origshowpage
That does not work. What I get is the table page "appended" to the DrawingSheet.pdf
rather than overlaying on top of the last page.
What am I doing wrong? How can I achieve what I want?
Edited-1:
If it helps to have same number of pages in both files to simplify logic, then I can generate Table.ps
to have same number of pages as DrawingSheet.pdf
file and have the table appear only on the last page of the Table.ps
.
Edited-2:
Alternatively, I tried following approach (similar to adding image watermark to PDF file). Net result of this is that I as many blank pages as number of pages in DrawingSheet.pdf
. Interestingly though, when I load that blank resulting PDF, I can see that the Acrobat Reader flashes the table across the screen, but when the entire file is loaded all I see in the end are blank pages.
Any help in solving this much appreciated.
<<
/EndPage
{
2 eq
{
pop false
}
{
gsave
1 dict begin
/showpage {} def
(Table.ps) run
end
grestore
pop true
} ifelse
} bind
>> setpagedevice
Edited-2 - Additional Notes: If I comment out gsave
and grestore
from above code, then I don't get blank pages but instead every page is now "replaced" (no overlay) with the table from Table.ps
.
Edited-3 - Partial Success:
After coming across this post: Edit (every page of a) Postscript file manually, I tried that approach. I converted the DrawingSheet.pdf
to DrawingSheet.ps
, identified the last %%EndPageSetup
line, and inserted following code after that line:
% 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 %
With this edits (gsave and grestore commented out - if I uncomment them then this does not work), I get my table super imposed on the last page. Table appears fine, however the original last page from the pdf appears as flipped over the center on X axis after super imposing. The screenshot of the page is:
Text that you can read is from the Table.ps
, while the text that is flipped is the original text from the last page of the DrawingSheet.pdf
(of course that page in original pdf is not flipped).
Not sure what I am doing wrong to get that flipped image?
Edited-4:
Added additional notes to "Edited-2"