Overlay two postscript files (command line approach)?
A similar approach to KenS's would be to put the "composition" code in a single file and call the component files with the run
operator. It amounts to the same thing but with a little less shell-work. A limitation is that the -dSAFER option (which is ON by default in most Distiller installations) disables the run
operator.
combo.ps:
/Oldshowpage /showpage load def
/showpage {} def
gsave
(tmp-Front.ps) run
grestore
%additional scaling and translation to place the graphic?
(logo.ps) run
Oldshowpage
Overlaying or merging multiple .ps files
I've done something like this before, using GMT to produce two plots (of opposite sides of the Earth) to PS and overlaying them, one flipped 180 degrees and reflected (that is, depicting the Earth as a projective plane with each point equated with its antipode).
This was my code:
#!/bin/sh
GMT gmtset BASEMAP_FRAME_RGB +192/192/192
GMT gmtset BASEMAP_FRAME_RGB 0/0/0
GMT pscoast -JS-60/0/16 -R0/360/-90/90 -Di -A5000 -N1 -W -GP300/15:FLightRedB- -SWhite -P -K > mapa.ps
GMT pscoast -JS120/0/16 -R0/360/-90/90 -Bg30/g15 -Di -A5000 -N1 -W -GP300/15:FLightGreenB- -Sp1/50:F-B- -P -O > mapb.ps
sed -i 's/595 842/600 600/g' mapa.ps
sed -i 's/PSL/180 rotate -1 1 scale 0 -1890 translate \nPSL/' mapb.ps
cat mapa.ps mapb.ps > mapc.ps
ps2pdf mapc.ps
Commenting out the second sed
line overlays them without flipping the second one. You can probably achieve what you want by tweaking this script and replacing the GMT commands with whatever you're using. The -O
option used for the second plot sets overlay mode, which omits the PS code that triggers the creation of a new page. Then you can just cat them together.
overlay one pdf or ps file on top of another
You can do this with pdf files using the command line tool pdftk using the stamp
or background
option.
e.g.
$ pdftk file1.pdf background file2.pdf output combinedfile.pdf
This will only work with a one-page background file. If you have multiple pages, you can use the multibackground
command instead.
Overlay a PS file on top of PDF file
After many trials and errors and of course suggestions/comments from fellow SO users like @beginner6789 and @luserdroog (my thanks to both of them), I finally got something that works for my case. I am going to summarize my experiments and findings for benefits of others.
For this discussion, let's say you want to superimpose a page from Overlay.*
(.ps or .pdf) on top of one particular page from Base.*
(.ps or .pdf). There does not appear to be one solution fit all scenarios. It depends on:
- If the you have
Overlay.ps
orOverlay.pdf
. Although converting them is quite easy. - If the you have
Base.ps
orBase.pdf
. Although converting them is quite easy. - If the
Overlay.ps
orBase.ps
is auto-generated from corresponding pdf file or if they are handwritten. I found that the handwritten PostScript files are much cleaner, precise, and optimized compared to auto-generated PostScript files. - Logic becomes more complex depending upon which page/s of Overlay file you want super imposed on top of which page/s of Base file. You may have to do some extra work to identify those pages.
Following are 3 solutions that I tried. To summarize: Solution-3 worked for me as described by the last row of the corresponding results table.
Solution-1:
Create following Merge.ps
and run it through your GhostScript to generate PDF output:
1 dict begin
/showpage {} def
(Base.ps) run % or Base.pdf
(Overlay.ps) run % or Overlay.pdf
end
showpage
Results:
Base File | Overlay File | Results/Notes |
---|---|---|
Base.pdf | Overlay.ps (Handwritten) | Overlay file is appended at the end of the Base file |
Base.pdf | Overlay.ps (Converted from PDF) | Overlay file is appended at the end of the Base file |
Base.pdf | Overlay.pdf | Overlay file and a blank page is appended at the end of the Base file |
Base.ps (Converted from PDF) | Overlay.ps (Handwritten) | Single page PDF with all pages from Base file and Overlay file are superimposed on top of each other |
Base.ps (Converted from PDF) | Overlay.ps (Converted from PDF) | Single page PDF with page from Overlay file only |
Base.ps (Converted from PDF) | Overlay.pdf | Same as Overlay file with blank page appended at the end |
How to merge two postscript files together?
Here is an example Ghostscript commandline, which would convert and merge the two (or more) PostScript files into one PDF in a one go:
gswin32c.exe ^
-o c:/path/to/output.pdf ^
-sDEVICE=pdfwrite ^
-dPDFSettings=/Screen ^
[...more desired parameters (optional)...] ^
/path/to/first.ps ^
/path/to/second.ps ^
/path/to/third.pdf
Edit: my first shot had falsely assumed PDF input files. It works of course with PostScript as well (or even a mix of PS/PDF)... And the output may also be PS.
Concatenate and 4-up Postscript files
I think a2ps
is having difficulty because it doesn't process the postscript natively, but instead uses a delegate program. So it treats postscript files essentially as images, and inserts the file (with some small wrapping) more or less directly into its output. a2ps
really shines at pretty-printing source code, but concatenation and N-up compositing of already "rendered" ps documents is a little outside of its proper scope, IMO. It's the wrong tool for this job.
The tool for making 4up versions of postscript files is psnup
, part of psutils
, for which I found a nice link here.
Duh. I guess the given error output clearly shows what I "guessed" above. The link goes into details, but I'll summarize here. You need to take control of how the 1-page documents are rotated and concatenated (using ps2ps
) before they are fed to psnup
.
Embed once and reference many times an EPS file in postscript
You could define the EPS as a form, then execute the form as required. See the PostScript Language Reference Manual, section 4.7:
4.7 Forms A form is a self-contained description of any arbitrary graphics, text, or sampled images that are to be painted multiple
times, either on several pages or at several locations on the same
page. The appearance of a form is described by a PostScript procedure
that invokes graphics operators. Language support for forms is a
LanguageLevel 2 feature.
Note that the Ghostscript pswrite/ps2write devices don't support handling PDF forms as PostScript forms, so embed the olutput multiple times. My assumption is that this is why your approach [2] doesn't work.
How can I make a program overlay text on a postscript file?
Ok, the example file you linked to is well behaving (and has not re-defined the showpage
operator).
So I'm now assuming the following:
- All your .ps files are similar to your example file.
- All your .ps files are 1 page only (like .eps files).
- All your filenames are like constructed as
gnp-NNN.ps
(like gnp-544.ps is). - The number you want to appear in the top right corner is
NNN
from the filename.
I also assume you have Ghostscript installed, and it is the most recent version, 8.71. I'm currently on Windows -- if you're on Linux/Unix, just replace gswin32c.exe
by gs
and all line endings ^
by \
.
Test PDF Output
Now try this command first:
gswin32c.exe ^
-o gnp-with-number-544.pdf ^
-sDEVICE=pdfwrite ^
-g5030x5320 ^
-c "/Helvetica-Italic findfont 15 scalefont setfont 453 482 moveto (544) show" ^
-f gnp-544.ps
and see if the resulting gnp-with-number-544.pdf
looks like you want it.
The -c "..."
is used to pass PostScript snippets to Ghostscript and make it process these together with the main .ps file which has then to follow as next parameter, with -f ...
.
You can modify parameters:
- Replace the
/Helvetica-Italic
fontname by/Helvetica
,/Courier
,/Times
,Helvetica-Bold
or whatever you've available and prefer. - Or modify the fontsize:
15
here means 15 points (in PDF 72 points == 1 inch). - Or change the position:
453 482
moves the PostScript currentpoint to "453 points to the left, 482 points to the top" (with the oringin 0,0 set to the bottom left corner). - Or tweak the media size:
-g5030x5320
gives you 503x532 points (due to the default resolution of 720 dpi used by-sDEVICE=pdfwrite
. - Or set the string being printed to
(File No. 544) show
or whatever you want.
You could also add quite a few parameters to tweak the quality of the output file (resolution, font embedding etc.), but these ones will do for now.
Change to PostScript Output
Should you need PostScript output for some reason instead of PDF, change the command like this:
gswin32c.exe ^
-o gnp-with-number-544.ps ^
-sDEVICE=ps2write ^
-g5030x5320 ^
-c "/Helvetica-Italic findfont 15 scalefont setfont 453 482 moveto (544) show" ^
-f gnp-544.ps
Batch-convert Lots of Files
Now, how to batch-convert this? For this last step, I'm assuming:
- your
NNN
numbering scheme is not using leading0
s. - your range of files to be converted is gnp-1.ps..gnp-1000.ps.
- you want PDF output with the numbers added, not PS.
On Windows, create an addnumbers-make-pdf.bat
file with this content:
gswin32c.exe ^
-o gnp-with-number-%1.pdf ^
-sDEVICE=pdfwrite ^
-g5030x5320 ^
-c "/Helvetica-Italic findfont 15 scalefont setfont 453 482 moveto (%1) show" ^
-f gnp-%1.ps
Now run this command in a cmd.exe
console:
for /l %i in (1,1,100) do (addnumbers-make-pdfvim.bat %i)
On Linux, create an addnumbers-make-pdf.sh
Bash shell script with this content:
#!/bin/bash
gs \
-o gnp-with-number-${1}.pdf \
-sDEVICE=pdfwrite \
-g5030x5320 \
-c "/Helvetica-Italic findfont 15 scalefont setfont 453 482 moveto (${1}) show" \
-f gnp-${1}.ps
Now run this command in a shell:
for i in $(seq 1 1000); do addnumbers-make-pdf.sh ${i} ; done
Update:
Hah!, it even works ;-) I just tested it on Windows. Here is a screenshot with the original and the overlayed file (as PDFs):
Related Topics
Open a File Directly from a Gitlab Private Repository
Unzip a Bunch of Zips into Their Own Directories
Find All Files Matching 'Name' on Linux System, and Search with Them for 'Text'
How to Program for Linux's New 'Fanotify' File System Monitoring Feature
Accessing the Gpio (Of a Raspberry Pi) Without ''Sudo''
Linux Kernel - Add System Call Dynamically Through Module
Setup Cron Tab to Specific Time of During Weekdays
Linux Command to Delete All Files Except .Git Folder
/Usr/Bin/Perl: Bad Interpreter: Text File Busy
Add Blank Line After Every Result in Grep