I need to print some portion of input text data with an underline. We are currently using Perl script to generate the text file and the 'enscript' & 'ps2pdf' to generate the PDF files of generated text files.

Need some portions of texts in PDF as underlined. any suggestions will be helpful.

-- requirement

enter image description here

but the resulting pdf based on the reply from @meuh, looks like

enter image description here

do we need to do any modifications in the sed script?

  • Can you give us some more details? Perhaps an example of your input? What markup language would you use to get the text underlined? ASCII can't do that, so I guess your perl is returning some sort of XML or HTML or MarkDown or similar, right? Or are you doing the underlining with unicode? Please edit your question and clarify what you have and what you still require. – terdon Sep 9 at 13:27
  • @terdon The Perl script returns a .txt file. And converting into PDF using enscript and ps2pdf. – Suhail Abdul Gafoor Sep 10 at 3:45
  • So how do you mark the regions you want underlined? And why not have the perl script print HTML or anything else that can do underlining, and then convert that to pdf? – terdon Sep 10 at 8:23

If you find no other way, you can always edit the PostScript that is generated for enscript's bggray special escape (in a similar way to my answer to your previous question).

When you use in your input bggray{0.5} after the special escape character (which you can change with -e), enscript will output code to call the PostScript function bgs to draw the background shade. You can modify this function between the output of enscript and the input of ps2pdf to look for a particular shade value, say 0.01 and draw a thin line instead of a background box.

My version of enscript defines bgs with

/bgs {  % x y height blskip gray str -> -  show string with bg color
    x y blskip sub str stringwidth pop height Box

We can change the line doing the drawing, x y ... Box, with a sed script

enscript ... |
sed '
    /x y blskip sub str stringwidth pop height Box/{
      s/^/ %--% /
   /lh height .05 mul def \
   /y2 y height .2 mul add def \
   gray 0.01 eq { x y2 blskip sub str stringwidth pop lh Box /gray 0 store } \
                { x y blskip sub str stringwidth pop height Box } ifelse
}' |
ps2pdf ...

I've added two parts you can easily change: the line beginning /lh defines the thickness of the line as a fraction of the height. Change the .05 to make the line thicker (i.e. bolder). The next line beginning /y2 moves the underline up by 0.2 times the box height; this corresponds to the fix I made in your last question.

Note that you must be careful not to underline trailing spaces by making sure the bggray escapes are right next to the text. For example,

enscript -fCourierBold12 -e@ -p- | ...
@bggray{0.01}underlined text@bggray{1} normal text

produces this image: enter image description here

  • Thank you so much for your reply, It helps very much, but had some style issues within it. Modified the question to attach the screenshots of expected and resulting pdfs. What modifications do we need to do? Any replies will be appreciable. – Suhail Abdul Gafoor Sep 10 at 6:47
  • Also what we need to do to make the underline as Bold? – Suhail Abdul Gafoor Sep 10 at 6:59
  • I've updated the code in my answer to move the line up, and make the line fatter. You should be able to see how to modify the numbers .05 and .2 in the new code to adjust these to your enscript. However, you need to remove by yourself trailing spaces from the text that you want to underline. I dont know how to do that in PostScript. – meuh Sep 10 at 8:44
  • Thank you for the detailed reply. It works. – Suhail Abdul Gafoor Sep 10 at 10:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.