使用PDFBox设置字符间距 [英] Setting character spacing with PDFBox
问题描述
我目前正在使用Java和PDFBox库即时创建一些PDF.
I'm currently using Java and the PDFBox library to create some PDFs on the fly.
我需要能够设置某些文本的字符间距/跟踪,但似乎无法弄清楚.
I need to be able to set the character spacing/tracking of some text but can't seem to figure it out.
It looks as there is a method to do so : http://ci.apache.org/projects/pdfbox/javadoc/index.html?org/apache/pdfbox/util/operator/SetCharSpacing.html
但是我不太确定如何在这种情况下应用它.
But I'm not quite sure how to apply this in the situation.
cs.beginText();
cs.setFont( font, fontSize );
cs.setNonStrokingColor(color);
cs.moveTextPositionByAmount(position[0], position[1]);
cs.drawString(text);
cs.endText();
任何帮助将不胜感激!谢谢.
Any help would be appreciated! Thanks.
推荐答案
您需要用困难的方式完成操作,因为PDPageContentStream类不支持"Tc"运算符:
You need to do it the hard way, because the "Tc" operator isn't supported by the PDPageContentStream class:
cs.appendRawCommands("0.25 Tc\n");
您提到的SetCharSpacing方法用于解析现有的PDF.
The SetCharSpacing method you mentioned is for parsing existing PDFs.
PS:别忘了在写完内容流之后调用close!
PS: don't forget to call close after finishing writing to the content stream!
PPS:setCharacterSpacing()
在2.0.4和更高版本中可用.
PPS: setCharacterSpacing()
is available in version 2.0.4 and higher.
这篇关于使用PDFBox设置字符间距的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!