Setting character spacing with PDFBox

≡放荡痞女 提交于 2020-01-03 17:10:44

问题


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.


回答1:


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");

The SetCharSpacing method you mentioned is for parsing existing PDFs.

PS: don't forget to call close after finishing writing to the content stream!

PPS: setCharacterSpacing() is available in version 2.0.4 and higher.



来源:https://stackoverflow.com/questions/30144632/setting-character-spacing-with-pdfbox

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