3

How can I make a Ghostscript overlay text on top of a PostScript file?

I found part of a solution here: How can I make a program overlay text on a postscript file? which suggests to do:

gs -o figLabel.pdf -sDEVICE=pdfwrite \
   -c "/Helvetica findfont 15 scalefont setfont 50 200 moveto (text) show" \
   -f fig.eps` 

However in that case the text is behind the image.

Is there an option in Ghostscript to force the text to be in front of the image?

Community
  • 1
  • 1
obelixk
  • 33
  • 5

1 Answers1

3

PostScript uses an opaque painting model, so each new thing drawn obscures anything previously drawn. In your command line, the text is drawn and then the EPS file is drawn. It sounds like you want the reverse behavior. So do

gs  -o figLabel.pdf -sDEVICE=pdfwrite \
    -f fig.eps \
    -c "/Helvetica findfont 15 scalefont setfont 50 200 moveto (text) show showpage"

This should work IF the EPS file obeys the rule that it should not call showpage itself. Otherwise, we'll need to add workarounds for that.

If the EPS file calls showpage (even though it ought not to do this), we need to redefine the name /showpage so it does nothing, and save the old definition to call at the end.

gs  -o figLabel.pdf -sDEVICE=pdfwrite \
    -c "/realshowpage /showpage load def /showpage {} def" \
    -f fig.eps \
    -c "/Helvetica findfont 15 scalefont setfont 50 200 moveto (text) show realshowpage"
luser droog
  • 18,988
  • 3
  • 53
  • 105
  • Using `showpage` put the results on two different pages. I could use `pstops` to merge them in one page but I am wondering if there is a more straight forward way to do it? Not using showpage doesn't writte the text in the image. – obelixk Jan 30 '15 at 20:14
  • My guess is that the eps file is calling `showpage`, even though technically it should not do this. So, I've added the workaround to the answer. Let me know if it doesn't work or if you need more explanation about anything. – luser droog Jan 31 '15 at 06:12