Reputation: 19110
I’m using iText 2.1.7, included through the following dependency …
<dependency>
<groupId>com.lowagie</groupId>
<artifactId>itext</artifactId>
<version>2.1.7</version>
</dependency>
How do I change the text size of a PDF field in a PDF I’m trying to export? I created a PDF template using OpenOffice 4.1.0 and then have tried this code to set the text size:
final PdfReader pdfTemplate = new PdfReader(pdfTemplateFile.toString());
// Prepare to generate a byte output stream
ByteArrayOutputStream out = new ByteArrayOutputStream();
PdfStamper stamper = new PdfStamper(pdfTemplate, out);
stamper.setFormFlattening(true);
…
stamper.getAcroFields().setField(PARTICIPANT_FIELD_NAME, user.getFirstName() + " " + user.getLastName());
stamper.getAcroFields().setFieldProperty(PARTICIPANT_FIELD_NAME, "textsize", new Float(36), null);
final BaseFont font = BaseFont.createFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.EMBEDDED);
stamper.getAcroFields().setFieldProperty(PARTICIPANT_FIELD_NAME, "textfont", font, null);
No matter what value I put in between “new Float(xxx)”, the font size is always rendered the same. Where am I going wrong?
Upvotes: 3
Views: 5796
Reputation: 77528
The appearance of the field is generated at the moment you set the field with the setField()
method. This means that you're doing things in the wrong order. Change your code like this:
AcroFields form = stamper.getAcroFields();
form.setFieldProperty(PARTICIPANT_FIELD_NAME, "textsize", new Float(36), null);
final BaseFont font = BaseFont.createFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.EMBEDDED);
form.setFieldProperty(PARTICIPANT_FIELD_NAME, "textfont", font, null);
form.setField(PARTICIPANT_FIELD_NAME, user.getFirstName() + " " + user.getLastName());
As for your use of iText 2.1.7, obviously that's not a good idea, but we'd like to know why you prefer using obsolete software, so we've hired Black Duck Software to help us with this survey.
Upvotes: 7