Carlos Cocom
Carlos Cocom

Reputation: 932

iTextSharp doesn't work after update

After updating iTextSharp with NuGet the PDF generation stopped working.

i updated from 4.1.2.0 to 5.5.4.0

My original code was

using (var stream = new MemoryStream())
{  
    var reader = new PdfReader(pdfPath);
    var stp = new PdfStamper(reader, stream);
    var writer = stp.Writer;
    var fieldsManager = stp.AcroFields;

    foreach (DictionaryEntry entry in fieldsManager.Fields)
    {
        var strEntry = entry.Key.ToString();
        if (strEntry == "txtNoServicio") fieldsManager.SetField(strEntry, orden.NoServicio.ToString(CultureInfo.InvariantCulture));
        if (strEntry == "txtFechaCreacionTicket") fieldsManager.SetField(strEntry, orden.FechaCreacionTicket.ToString("dd/MM/yyyy"));
        if (strEntry == "txtAgenteAsignado") fieldsManager.SetField(strEntry, orden.AgenteAsignado);
        ...
    }

    stp.FormFlattening = true;
    stp.Close();
    reader.Close();
    dataBytes = stream.GetBuffer();
}

return dataBytes;

After update I changed the foreach loop from

foreach (DictionaryEntry entry in fieldsManager.Fields)

to

foreach (KeyValuePair<string, AcroFields.Item> entry in fieldsManager.Fields)

The method continues to return the PDF successfully but SetValues with SetField in PDF doesn't work.

Any ideas?

Debugging I see fieldsManager.Fields contain a property called Values and it contains a property called IsReadOnly which is set to True. Could it mean something?

Upvotes: 0

Views: 979

Answers (1)

Bruno Lowagie
Bruno Lowagie

Reputation: 77546

You have jumped from a version that was released in 2008 to a version that was released in 2014. There are 6 years between these two versions.

One does not simply...

(Sorry, I couldn't resist creating a Game of Thrones meme ;-) )

Please provide a SSCCE that reproduces the problem. Many people are filling out forms with iTextSharp 5.5.x without experiencing any problem.

What happens if you add the following line:

form.GenerateAppearances = true;

See ITextSharp PDFTemplate FormFlattening removes filled data

If this fixes your problem, then your original form has a setting that prevents iTextSharp from generating the appearances. Old iTextSharp versions ignored this setting. More recent versions respect that setting.

Upvotes: 2

Related Questions