Claudio
Claudio

Reputation: 143

pdfStamper.AcroFields.SetField dont keep appearance settings

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

Upvotes: 2

Views: 1206

Answers (1)

mkl
mkl

Reputation: 96029

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.

Upvotes: 1

Related Questions