2

I use iTextSharp 5.5.13 to create pdf file with text AcroFields and in a second step edit the pdf filling the AcroFields with some values.

For some fields i have to set a character spacing, so i use CreateAppearance method. this is the code:

var appearance = writer.DirectContent.CreateAppearance(box.Width, box.Height);
appearance.SetFontAndSize(baseFont, obj.FontSize);
appearance.SetColorFill(new iTextSharp.text.BaseColor(obj.Color));
appearance.SetCharacterSpacing(obj.CharSpacing);
formField.DefaultAppearanceString = appearance;
formField.SetAppearance(iTextSharp.text.pdf.PdfAnnotation.APPEARANCE_NORMAL, appearance);

writer.AddAnnotation(formField);

this code produce expected pdf result with fine character spacing in editable fields.

The problem is when i edit the pdf to fill AcroFields with:

pdfStamper.FormFlattening = true;
pdfStamper.AcroFields.GenerateAppearances = true;
pdfStamper.AcroFields.SetField(fieldName, fieldValue);

the resulting flattened pdf does not mantain the appearence character spacing...

What's wrong with my code?

Thanks

Claudio
  • 133
  • 2
  • 11

1 Answers1

1

For generating text field appearances iText 5.x only uses the font, font size, and color information from the DA default appearance string (and the color information only if set using the g, rg, or k instructions), cf. the AcroFields method SplitDAelements which is used to extract information from the DA string.

So the iText 5.x appearance generation is quite limited and in particular does not support character spacing.

A possible work-around is for you to explicitly create all the appearances in your own code.

mkl
  • 90,588
  • 15
  • 125
  • 265
  • Thanks @mkl, how can i create the appearances in my own code? can you provide me a little example? do you know if this feature is supported in iText 7? – Claudio Feb 22 '19 at 09:01
  • @Claudio Essentially you already *do* create an appearance in your own code, you merely do so before setting the `formField` value. All you have to do is to *first* set that value and *thereafter* use `formField.SetAppearance` with an appearance that includes the values layout'ed as you desire. – mkl Mar 25 '19 at 15:40