Rajesh Narravula
Rajesh Narravula

Reputation: 1463

Underline for AcroFields in java(iTextSharp )

Populating the content to template, fine but i want underline for the AcroField.
I'm using below code. ClassCastException is coming.

Font font = FontFactory.getFont("Courier-Bold", 16, Font.UNDERLINE, BaseColor.BLACK);
fields.setFieldProperty(fieldName, ApplicationConstants.TEXT_FONT, font , null);

please help anyone.
Thanks.

Upvotes: 0

Views: 1356

Answers (2)

Rajesh Narravula
Rajesh Narravula

Reputation: 1463

These changes i did for underline. i added add the text using ColumnText in the AcroFields place. this is working fine.

private void underlineText(String fieldName, String fieldValue, AcroFields fields, PdfStamper stamper) throws Exception
{
    Rectangle targetPosition = fields.getFieldPositions(fieldName).get(0).position;
    Font fontNormal = FontFactory.getFont("Courier-Bold", 8, Font.UNDERLINE, BaseColor.BLACK);
    Anchor url = new Anchor(fieldValue.trim(), fontNormal);
    ColumnText data = new ColumnText(stamper.getOverContent(1));
    data.setSimpleColumn(url, targetPosition.getLeft(), targetPosition.getBottom(), targetPosition.getRight(), targetPosition.getTop(), 0,0);
    data.go();
}

Upvotes: 3

Bruno Lowagie
Bruno Lowagie

Reputation: 77528

Allow me to break your question up into two different problems.

Problem 1: You are creating a Font object and you are using this object in the setFieldProperty() method.

This is wrong. The setFieldProperty() method only accepts BaseFont objects as fonts. This explains the ClassCastException: you can't cast a Font to a BaseFont.

This is how you'd correct this:

BaseFont bf = BaseFont.createFont(BaseFont.COURIER_BOLD, BaseFont.WINANSI, BaseFont.NOT_EMBEDDED);

fields.setFieldProperty(fieldName, ApplicationConstants.TEXT_FONT, bf , null);

Problem 2: You want to fill out a normal text field and underline the text that was added.

If you read ISO-32000-1, you'll find out that this isn't supported.

Sub-problem a: "underline" is not a property of a font (nor is color for that matter). It's something that was added to the Font class in iText for your convenience (just like the color).

Sub-problem b: the PDF specification doesn't define any flag that underlines text added to a text field.

If it's really your requirement to underline text added to a field, your options are:

  1. Get the position of the field, and add the text using ColumnText. This is only an option if it's acceptable to flatten the form. If the filled out form needs to remain interactive, use a different approach.
  2. Define the field as a Rich Text field. See the PDF reference to find out more about this type of field. You'll probably be on your own if you choose this option. You can post another question on StackOverflow, but there's little chance anyone will answer.
  3. Construct your own /DA. This is not for the faint of heart. It will require a good insight in PDF and iText. If you don't have this insight, the chance that anyone will do this in your place is very small. Also: if the form isn't flattened, the line under the text will disappear as soon as somebody clicks the interactive field because of Sub-problem b. (In other words: option 3 is probably not a good idea.)

Upvotes: 2

Related Questions