Change a detachable PDF field

2

I'm working on MVC 4 and I've installed iTextSharp to create PDF's, so far so good. But what I'm doing is replicating a PDF that already exists, to fill in the required fields in this my created PDF.

Doubt now appears:

The PDF I am replicating has those fields called detachable , ie we can open the PDF and write to them as if we were editing the PDF. Is it possible from iTextSharp to open this same PDF file, detect these fields and write to them from the controller ?

    
asked by anonymous 27.02.2014 / 12:17

2 answers

1

Friend, let's better understand the application of your system ... I could not understand what you need, so we can give you a better answer.

Will you have a Form, or contract, or something like that that needs to be filled out by several client people or employees? Through the right system?

Does the content of this PDF constantly change? Are there several types of forms? (If both are negative you can put the contents of it fixed in your code or save the text in a table from a screen where the user that forms the form type and click on any button that adds a gap and so on and so forth. at the time of generating the PDF within the application you replace these Tags with the fields.)

UPDATE:

I found an example that might help you!

//Read all 'Form values/keys' from an existing multi-page PDF document
public void ReadPDFformDataPageWise()
{
PdfReader reader = new PdfReader(Server.MapPath(P_InputStream3));
AcroFields form = reader.AcroFields;
try
{
for (int page = 1; page <= reader.NumberOfPages; page++)
{
    foreach (KeyValuePair<string, AcroFields.Item> kvp in form.Fields)
    {
        switch (form.GetFieldType(kvp.Key))
        {
            case AcroFields.FIELD_TYPE_CHECKBOX:
            case AcroFields.FIELD_TYPE_COMBO:
            case AcroFields.FIELD_TYPE_LIST:
            case AcroFields.FIELD_TYPE_RADIOBUTTON:
            case AcroFields.FIELD_TYPE_NONE:
            case AcroFields.FIELD_TYPE_PUSHBUTTON:
            case AcroFields.FIELD_TYPE_SIGNATURE:
            case AcroFields.FIELD_TYPE_TEXT:
                int fileType = form.GetFieldType(kvp.Key);
                string fieldValue = form.GetField(kvp.Key);
                string translatedFileName = form.GetTranslatedFieldName(kvp.Key);
                break;
        }
    }
}
}
catch
{
}
finally
{
    reader.Close();
}
}

This and other examples of how to use iTextSharp find in this link

    
13.03.2014 / 14:52
1

Yes, it is possible! You need to use the iTextSharp.text.pdf namespace. camposDict is a IDictionary traditional.

Here's the implementation I use:

public string PDFReplace(
               string path, // PATH do arquivo
               string arquivo, // NOME do arquivo com extensão
               object camposDict // IDictionary
               )
{
    string pathPdf = string.Empty;
    PdfReader pdf = new PdfReader(@path.ToString());
    MemoryStream outStream = new MemoryStream();
    PdfStamper stamper = new PdfStamper(pdf, outStream);

    AcroFields fields = stamper.AcroFields;

    foreach (DictionaryEntry de in (Hashtable)camposDict)
    {
        string Key = (string)de.Key;
        string value = de.Value.ToString();

        fields.SetField(Key, value);
    }

    stamper.FormFlattening = true;
    stamper.Close();
    pdf.Close();

    string urlPasta = path.ToString().Substring(0, path.ToString().LastIndexOf("\"));

    File.WriteAllBytes(urlPasta + "\" + arquivo.ToString().ToUpper(), outStream.ToArray());
    return urlPasta + "\" + arquivo.ToString().ToUpper();
}

Update

An addendum: IDictionary should be composed of the dynamic field name of the PDF (yes, they need to be named) and its value of it.

    
27.02.2014 / 14:08