Search code examples
c#pdf.net-coreitextitext7

Remove or replace string input PdfFormField to a button so I can add an image using iText 7 Core c#


I am wondering if anyone can help me, I have an existing PDF template file, I want to be able to change a PdfFormField to a button so that I can add an image.

I cannot seem to be able to remove/replace string input to a button so I can add an image as the icon or even change it an image. Please see my attempt below, I would be gratefully for any help or even if this is possible.

I have looked at Itext7 man pages and other questions here and I cannot seem to get it to work. I am wondering if this is even possible.

Method to handle to fill the form

private byte[] FillForm(Dictionary<string, string> items)
{
    using var memoryStream = new MemoryStream();
    var pdfDoc = new PdfDocument(new PdfReader(_template), new PdfWriter(memoryStream));
    var form = PdfAcroForm.GetAcroForm(pdfDoc, true);
    var fields = form.GetFormFields();

    items.Aggregate(fields, (current, item) => UpdateDictionaryEntry(current, item.Key, item.Value, pdfDoc));

    form.FlattenFields();
    pdfDoc.Close();
    return memoryStream.ToArray();
}

and to write the values: (I have changed this to just try and remove each form field and change it to a button using it sizing with still no success )

private static IDictionary<string, PdfFormField> UpdateDictionaryEntry(IDictionary<string, PdfFormField> formFields, string key, string value, PdfDocument form)
{
    if (!formFields.ContainsKey(key))
        return formFields;

    formFields.TryGetValue(key, out var toSet);
    // toSet?.SetValue(value);

    var sizingArray = toSet.GetWidgets()[0].GetRectangle();

    formFields.Remove(key);

    var button = PdfFormField.CreatePushButton(
                    form, new Rectangle(sizingArray.GetAsNumber(0).FloatValue(),
                                             sizingArray.GetAsNumber(1).FloatValue(),
                                          sizingArray.GetAsNumber(2).FloatValue(),
                                         sizingArray.GetAsNumber(3).FloatValue()),
                    key, "Test Me");
    formFields.Add(key, button);

    formFields.TryGetValue(key, out var testy);

    return formFields;
}

Update:

I have tried retrieving the coordinates of the field, then removing the field and adding a new button at those coordinates. But I am unsure if i took the correct approach and it did not work as above.


Solution

  • The mistake I was making was using :

    formFields.Remove(key);
    

    Instead if of:

     form.ReplaceField(key, button);
    

    So basically what I did is below (height, width calculations and x,y):

            formFields.TryGetValue(key, out var toSet);
    
            PdfArray sizingArray = toSet.GetWidgets()[0].GetRectangle();
            var width = (float)(sizingArray.GetAsNumber(2).GetValue() - sizingArray.GetAsNumber(0).GetValue());
            var height = (float)(sizingArray.GetAsNumber(3).GetValue() - sizingArray.GetAsNumber(1).GetValue());
    
            var imagbase64 = imaged.Draw(image);
    
            var button = PdfFormField.CreatePushButton(
                pdfDoc, new Rectangle(sizingArray.GetAsNumber(0).FloatValue(),
                    sizingArray.GetAsNumber(1).FloatValue(),
                    width,
                    height), key, "");
            button.SetValue(imagbase64);
    
            form.ReplaceField(key, button);