0

I am trying to make a script add a few lines of text into a PDF form. I have no control of the form's creation and it is not editable, but I find the answer given to question "can I use Ghostscript to overlay a text (fax) header onto a PDF and/or TIFF?" almost solves my problem. Except I occasionally need to wrap the inserted text, so that it breaks into two lines. I know the width of the target "box" beforehand.

I am currently using GhostScript with pdfwrite, for instance: gs -o /tmp/desc.$$.pdf -sDEVICE=pdfwrite -c "/Helvetica findfont 9 scalefont setfont" -c "87 328 moveto ($2) show showpage"

Community
  • 1
  • 1
Frigo
  • 362
  • 2
  • 17

2 Answers2

0

The short answer is 'no' because PostScript doesn't work that way. You are expected to know and track the position and size of objects in a PostScript program yourself.

However.... PostScript is a prgramming language, so you can write a PostScript program to do so and I expect a search here on SO will get you some answers.

Otherwise a search of the comp.lang.postscript UseNet newsgroup archive will turn up means for determining the width of a string of text and shortening it until it fits.

KenS
  • 30,202
  • 3
  • 34
  • 51
0

Thanks to KenS for giving me hope. I went to the newsgroups and based on what I found mainly in http://computer-programming-forum.com/36-postscript/509be34895135813.htm I give a MWE here:

DESCRIPTION="This Text is so long that I decided to break it into multiple lines. "
XCOORD=88
YCOORD=328
TEXTWIDTH=100

read -d '' WRAPFUNCTION << EOF
/Helvetica findfont 9 scalefont setfont
/wordbreak ( ) def
/linewidth $TEXTWIDTH def
/cwz {/curwidth 0 def} def
cwz
/nlwrap { /y y 12 sub def x y moveto nextword show ( ) show } def
/wrap { /text exch def
      {text wordbreak search
         {/nextword exch def pop
          /text exch def
          /wordwidth nextword stringwidth pop def
          /breakwidth wordbreak stringwidth pop def
          curwidth wordwidth add linewidth gt
            {nlwrap /curwidth wordwidth breakwidth add def}
            {nextword show ( ) show /curwidth curwidth wordwidth add
breakwidth add def }
            ifelse
          }
            {pop exit}
          ifelse
      }loop
    }def
/x $XCOORD def
/y $YCOORD def
x y moveto ($DESCRIPTION) wrap
showpage
EOF

gs -o desc.pdf -sDEVICE=pdfwrite -c "$WRAPFUNCTION"

The function basically goes word by word, and does a CRLF if the next word exceeds TEXTWIDTH. The iteration algorithm is simplistic: note the space at the and of the input text.

Frigo
  • 362
  • 2
  • 17