How to (horizontally) align text of PDTextField in PDFBox?

*爱你&永不变心* 提交于 2020-01-20 08:33:07

问题


I have a program that create TextFields inside a PDF-file so it can be used as a form. I would like to have the text I write in the TextFields I created to be centered though. How is that possible? My code currently looks like this:

PDTextField textBox = new PDTextField(acroForm);
textBox.setPartialName("Field " + j + " " + i);
defaultAppearanceString = "/Helv 8 Tf 0 g"; //Textsize: 8
textBox.setDefaultAppearance(defaultAppearanceString);
acroForm.getFields().add(textBox);

PDAnnotationWidget widget = textBox.getWidgets().get(0);
PDRectangle rect = new PDRectangle(inputField.getX(), inputField.getY(), inputField.getWidth(), inputField.getHeight());
widget.setRectangle(rect);
widget.setPage(page);
widget.setPrinted(true);
page.getAnnotations().add(widget);

and I thought of an easy function to align text like this:

textBox.setAlignment(Alignment.CENTER);

but I didn't find it.


回答1:


Use the Q flag:

textBox.setQ(PDTextField.QUADDING_CENTERED);

Other possible values are QUADDING_RIGHT and QUADDING_LEFT (which is the default).



来源:https://stackoverflow.com/questions/47767319/how-to-horizontally-align-text-of-pdtextfield-in-pdfbox

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