【问题标题】:Pdf's fields should remain editable using itextsharp in asp.netPdf 的字段应该在 asp.net 中使用 itextsharp 保持可编辑
【发布时间】:2013-03-14 19:54:35
【问题描述】:

我有一个可填写的 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();
}

【问题讨论】:

  • 您确定您的 PDFHelper 是 iTextsharp 类吗?它可能建立在 iTextSharp 类的基础上,但它本身很可能不是。
  • @mkl PDFHelper 是我的自定义类。请检查我的问题的编辑部分。
  • 您的代码行 stamper.FormFlattening = true; 指示 iText 展平表单字段,即将它们整合到页面内容中。由于您希望表单字段保持可编辑字段,因此不要展平表单。
  • @mkl 非常感谢。它真的如我所愿。再次感谢。请将其发布为答案,它可能对其他人有所帮助。

标签: asp.net pdf itextsharp


【解决方案1】:

你的代码行

stamper.FormFlattening = true;

指示 iTextSharp 将表单域展平,即将它们整合到页面内容中并移除表单域注释。

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

【讨论】:

    【解决方案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”

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-03-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-01-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多