Postscript: watermark with varying page sizes

霸气de小男生 提交于 2019-12-13 06:12:13

问题


I am trying to use postscript to watermark postscript files. I am doing this with setpagedevice like so:

    <<
       /EndPage {
       exch pop 2 lt {
          /Times-Roman 40 selectfont
          .6 setgray 300 300 moveto 30 rotate (Watermark) show
          true}
          {false} ifelse
       } bind
    >> setpagedevice

    (file_to_watermark.ps) run

This works great, but I would like the watermark to be centered on the page, regardless of page size (this code needs to work for varying sizes of file_to_watermark.ps). My code right now is positioning the watermark based on specific coordinates, which obviously doesn't center the mark if a different file_to_watermark.ps is used with a different page size (i.e. legal, letter, etc.). Is there someway to retrieve the page size of the current file_to_watermark.ps and center-on-page the watermark based on that rather than predefined coordinates?


回答1:


You could extract the current media size from the pagedevice dictionary:

currentpagedevice /PageSize get

which returns the width and height (in points) on the stack. You can then use the stringwidth operator to calculate the amount of space occupied by the given string when printed. There's no simple way to get the vertical height, but the pointsize is as good a guide as anything for Latin fonts.

Subtracting the string width from the page width and dividing by two should be good enough, similarly for the y co-ordinate.

For real printers, instead of using the MediaSize the initial clip is sometimes a better bet:

initclip clippath flattenpath pathbbox


来源:https://stackoverflow.com/questions/16966106/postscript-watermark-with-varying-page-sizes

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!