我正试图使脚本添加几行文本到PDF格式。我无法控制表单的创建,也无法编辑,但我发现问题的答案是"我可以使用Ghostscript将文本(传真)标题覆盖到PDF和/或TIFF上吗?"几乎解决了我的问题。除了偶尔需要对插入的文本进行换行,以便将其分成两行。我事先知道目标"框"的宽度。
我目前使用GhostScript与pdfwrite,例如:gs -o /tmp/desc.$$.pdf -sDEVICE=pdfwrite -c "/Helvetica findfont 9 scalefont setfont" -c "87 328 moveto ($2) show showpage"
简短的回答是'不',因为PostScript不这样工作。你需要自己知道并跟踪PostScript程序中对象的位置和大小。
然而…PostScript 是一种编程语言,所以你可以写一个PostScript程序来做这件事,我希望在so上搜索一下会得到一些答案。
否则,搜索comp.lang.postscript UseNet新闻组归档文件将找到确定文本字符串宽度并将其缩短直至合适的方法。
感谢KenS给了我希望。我去了新闻组,根据我在http://computer-programming-forum.com/36-postscript/509be34895135813.htm中发现的内容,我在这里给出了一个MWE:
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"
函数基本上逐字执行,如果下一个单词超过texttwidth,则执行CRLF。迭代算法很简单:注意输入文本的和处的空格。