Search code examples
filepdftextadditionghostscript

add text on 1st page of a pdf file


I'm trying to add a text comment (not a note) to a pdf file. I create a date.ps file which contains the text comment:

%! 
/Arial findfont 
30 scalefont 
setfont 
newpath 
10 720 moveto 
(PAID on 5.1.2013) show 
showpage

and I launch the shell command with $i=name of the pdf file to tag:

gs \
    -q \
    -dNOPAUSE \
    -dSAFER \
    -dBATCH \
    -sOutputFile="$RFP/$DOMAINE/$NEWNAME" \
    -sDEVICE=pdfwrite \
    -sPAPERSIZE=a4 \
    date.ps \
    $i

This works, but it creates a new 1st page - empty - with just the text "PAID on 5.1.2013" alone.

I do not find the trick to overlay the text on the 1st page of the original pdf.


Solution

  • Because your PostScript executed a showpage it ejects the first page after marking it, so the remaining content is therefore on the 2nd and subsequent pages. If you don;t execute showpage then the marks you make will be on the first page, and the first PDF page will be drawn 'on top' of it.

    More complex code can use BeginPage and EndPage to draw over and under the page contents, and to do so on specified pages, among other things.

    [added later]

    Try this:

    %!
    << 
    /EndPage 
    {
      0 eq
      {
        0 eq
        {
          /Arialabold findfont 22 scalefont setfont newpath 250 820 moveto 1 0 0 setrgbcolor (PAYE PAR CCP LE $DATEPMT) show
        } if
        true
      }
      {
        pop false
      } ifelse
    } >> setpagedevice
    

    Works for me.