Code Rider
Code Rider

Reputation: 2063

Pdf's fields should remain editable using itextsharp in asp.net

I have a fillable pdf. In which i have few textboxes.

I fill these fields by using following code(itextsharp).

 DataTable dt = new DataTable();
            String pdfPath1 = Server.MapPath("pdfs\\transmittal2.pdf");
            if (File.Exists(pdfPath1))
            {                  

                dt = objClsTransmittal.GetTransmittal(jobid, cid);
                String comment = "Correspondence generated for " + dt.Rows[0]["Recipient"].ToString();                  
                var formfield = PDFHelper.GetFormFieldNames(pdfPath1);
                formfield["DocDate"] = DateTime.Now.ToLongDateString();
                formfield["Address1"] = dt.Rows[0]["Company"].ToString();
                formfield["Address2"] = dt.Rows[0]["Address1"].ToString();
                formfield["PropertyAddress"] = dt.Rows[0]["PropertyAddress"].ToString();
                formfield["Job"] = dt.Rows[0]["JobID"].ToString();
                formfield["Name"] = dt.Rows[0]["Recipient"].ToString();
                formfield["CityStateZip"] = dt.Rows[0]["address2"].ToString();
                formfield["E-mail"] = dt.Rows[0]["Email"].ToString();
                var pdfcontent = PDFHelper.GeneratePDF(pdfPath1, formfield);                    
                PDFHelper.ReturnPDF(pdfcontent, "Transmittal.pdf");

            }

Currently its downloded as read only pdf.

when this pdf gets downloaded, i want that all fields still remain fillable, with the text i have filled in pdf. So that i can edit the text.

I'm looking forward for your replies.

Thanks.

EDIT

PdfHelper is my custom class. In which i have used following code:

  using System;
   using System.Collections.Generic;
  using System.Collections;
  using System.Linq;
  using System.Web;
   using System.IO;
   using iTextSharp.text.pdf;

  public class PDFHelper
  {
    public static Dictionary<string, string> GetFormFieldNames(string pdfPath)
    {
    var fields = new Dictionary<string, string>();

    var reader = new PdfReader(pdfPath);
    foreach (DictionaryEntry entry in reader.AcroFields.Fields)
        fields.Add(entry.Key.ToString(), string.Empty);
    reader.Close();

    return fields;
}

public static byte[] GeneratePDF(string pdfPath, Dictionary<string, string> formFieldMap)
{
    var output = new MemoryStream();
    var reader = new PdfReader(pdfPath);
    var stamper = new PdfStamper(reader, output);
    var formFields = stamper.AcroFields;

    foreach (var fieldName in formFieldMap.Keys)
        formFields.SetField(fieldName, formFieldMap[fieldName]);

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

    return output.ToArray();
}


public static string GetExportValue(AcroFields.Item item)
{
    var valueDict = item.GetValue(0);
    var appearanceDict = valueDict.GetAsDict(PdfName.AP);

    if (appearanceDict != null)
    {
        var normalAppearances = appearanceDict.GetAsDict(PdfName.N);

        if (normalAppearances != null)
        {
            foreach (var curKey in normalAppearances.Keys)
                if (!PdfName.OFF.Equals(curKey))
                    return curKey.ToString().Substring(1); // string will have a leading '/' character, so remove it!
        }
    }


    var curVal = valueDict.GetAsName(PdfName.AS);
    if (curVal != null)
        return curVal.ToString().Substring(1);
    else
        return string.Empty;
}

public static void ReturnPDF(byte[] contents)
{
    ReturnPDF(contents, null);
}

public static void ReturnPDF(byte[] contents, string attachmentFilename)
{
    var response = HttpContext.Current.Response;

    if (!string.IsNullOrEmpty(attachmentFilename))
        response.AddHeader("Content-Disposition", "attachment; filename=" + attachmentFilename);

    response.ContentType = "application/pdf";
    response.BinaryWrite(contents);
    response.End();
}

Upvotes: 1

Views: 3983

Answers (2)

cholegm
cholegm

Reputation: 1

Error: Cannot convert type in PDFHelper.cs

public static Dictionary<string, string> GetFormFieldNames(string pdfPath)
    {
        var fields = new Dictionary<string, string>();

        var reader = new PdfReader(pdfPath);
        foreach (DictionaryEntry entry in reader.AcroFields.Fields) //ERROR: 'System.Collections.Generic.KeyValuePair' to 'System.Collections.DictionaryEntry'
        {
            fields.Add(entry.Key.ToString(), string.Empty);
        }
        reader.Close();

        return fields;
    }

'System.Collections.Generic.KeyValuePair' to 'System.Collections.DictionaryEntry'

Upvotes: -2

mkl
mkl

Reputation: 95918

Your code line

stamper.FormFlattening = true;

instructs iTextSharp to flatten the form fields, i.e. to integrate them into the page content and remove the form field annotations.

As you want to keep the form fields as editable fields, don't flatten the form.

Upvotes: 7

Related Questions