使用asp.net中的itextsharp,Pdf的字段应保持可编辑状态

时间:2013-03-14 19:54:35

标签: asp.net pdf itextsharp

我有一个可填写的pdf。其中我的文本框很少。

我使用以下代码填充这些字段(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");

            }

目前已下载为只读pdf。

当这个pdf下载时,我希望所有字段仍然可填写,文本我填写了pdf。这样我就可以编辑文本。

我很期待你的回复。

感谢。

修改

PdfHelper是我的自定义类。我在其中使用了以下代码:

  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();
}

2 个答案:

答案 0 :(得分:6)

您的代码行

stamper.FormFlattening = true;

指示iTextSharp展平表单字段,即将它们集成到页面内容中并删除表单字段注释。

由于您希望将表单字段保留为可编辑字段,请不要压缩表单。

答案 1 :(得分:-2)

错误:无法转换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'到'System.Collections.DictionaryEntry'