Strikethrough data in pdf field using iTextSharp

1.7k views Asked by At

Maybe someone now how to StrikeThrough data in PDF Form?

i tried

AcroFields pdfFromFields = pdfStamper.AcroFields;
pdfFormFields.SetFieldProperty(fieldname, "sthhere", FontFactory.GetFont(FontFactory.TIMES_ROMAN, 12 , Font.STRIKETHRU),null);
pdfFormFields.SetField(fieldname, fieldvalue);

but error appears "unable to cast object of type iTextSharp.text.Font to type iTextSharp.text.pdf.BaseFont" Thank you for any help.

I used

Chunk c1 = new Chunk ("insideText", FonctFactory.GetFont(FonctFactory.TIMES_ROMAN,12,Font.STRIKETHRU));
AcroFields form = stamper.getAcroFields();
Rectangle rect = form.getFieldPositions("fieldName").ElementAt(0).position;
ColumnText ct = new ColumnText(canvas);
ct.SetSimpleColumn(rect);
ct.AddElement(c1);
ct.Go();

and added Text is placed lower than middle fielde position and too much left. How too centralize it?

1

There are 1 answers

2
Bruno Lowagie On

Using Font.STRIKETHRU as a font property in iText was a design flaw. I am responsible for that poor design choice, mea culpa. If you consult ISO-32000-1, you'll notice that strikethru is not a property of a font (BaseFont), nor is it possible to set that property on a field. Hence, it is impossible to fill out a field with "stroked through" content, unless you'd find a font that contains such characters (as indicated by mkl in his comment).

There are two other possible workarounds depending on the final result you want to get.

  1. You can define your own default appearance (/DA) for the form field. In this appearance, you could draw the text and a line that strikes through it. I am not going to give you an example on how to do this, because it is not a good solution: the form remains interactive and the moment the end user clicks the field, Adobe Reader will replace the /DA with an appearance created on the fly in the viewer. This appearance will show the text without the strikethru.
  2. If it is acceptable for you that the form is flattened, then I would fill out the field the hard way. That is: retrieve the coordinates of the field and draw the text at those coordinates using ColumnText. For an example, see my answer to this question: How to continue field output on a second page?