2013-03-14 26 views
1

我有一个可输入的pdf。我有几个文本框。在asp.net中使用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"); 

      } 

目前它的downloded作为只读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(); 
} 
+0

你确定你的PDFHelper是iTextSharp的课吗?它可能建立在iTextSharp类上,但它最有可能本身不是。 – mkl 2013-03-14 20:36:54

+0

@mkl PDFHelper是我的自定义类。请检查我的问题的编辑部分。 – 2013-03-14 20:50:23

+1

您的代码行'stamper.FormFlattening = true;'指示iText平整表单域,即将它们集成到页面内容中。由于您希望保留表单域可编辑字段,请勿将表单扁平化。 – mkl 2013-03-14 21:01:05

回答

5

你的代码行

stamper.FormFlattening = true; 

指示iTextSharp的扁平化的表单字段,即它们整合到网页内容,并删除表单域注释。

由于您希望将表单字段保留为可编辑字段,因此请勿将表单放平。

+0

+1听起来很对我 – Merlin 2013-03-14 23:19:34

-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'

相关问题