Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.6k views
in Technique[技术] by (71.8m points)

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

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.

My problem

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Use the Q flag:

textBox.setQ(PDTextField.QUADDING_CENTERED);

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...