PDF格式的领域应保持在编辑使用asp.net iTextSharp的 [英] Pdf's fields should remain editable using itextsharp in asp.net

查看:153
本文介绍了PDF格式的领域应保持在编辑使用asp.net iTextSharp的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个可填写的PDF。在我所一些文本框。

我用下面的code(iTextSharp的)填写这些字段。

  DataTable的DT =新的DataTable();
            字符串pdfPath1 =使用Server.Mappath(PDF文件\\\\ transmittal2.pdf);
            如果(File.Exists(pdfPath1))
            {                DT = objClsTransmittal.GetTransmittal(作业ID,CID);
                字符串评论= + dt.Rows生成的通信[0] [收件人]的ToString()。
                VAR formfield = PDFHelper.GetFormFieldNames(pdfPath1);
                formfield [DocDate] = DateTime.Now.ToLongDateString();
                formfield [地址1] = dt.Rows [0] [公司]的ToString()。
                formfield [地址2] = dt.Rows [0] [地址1]的ToString()。
                formfield [PropertyAddress] = dt.Rows [0] [PropertyAddress]的ToString()。
                formfield [工作] = dt.Rows [0] [作业ID]的ToString()。
                formfield [名称] = dt.Rows [0] [收件人]的ToString()。
                formfield [CityStateZip] = dt.Rows [0] [地址2]的ToString()。
                formfield [电子邮件] = dt.Rows [0] [电子邮件]的ToString()。
                VAR pdfcontent = PDFHelper.GeneratePDF(pdfPath1,formfield);
                PDFHelper.ReturnPDF(pdfcontentTransmittal.pdf);            }

目前其作为只读PDF downloded。

在此PDF下载的内容,我想所有的领域仍然可填写,与我填补了PDF格式的文本。这样我就可以编辑文本。

我期待着您的答复。

感谢。

修改

PdfHelper是我的自定义类。在我所使用以下code:

 使用系统;
   使用System.Collections.Generic;
  System.Collections中使用;
  使用System.Linq的;
  使用的System.Web;
   使用System.IO;
   使用iTextSharp.text.pdf;  公共类PDFHelper
  {
    公共静态字典<字符串,字符串> GetFormFieldNames(字符串pdfPath)
    {
    VAR领域=新的解释和LT;字符串,字符串>();    VAR读卡器=新PdfReader(pdfPath);
    的foreach(在reader.AcroFields.Fields的DictionaryEntry进入)
        fields.Add(entry.Key.ToString(),的String.Empty);
    reader.Close();    返回领域;
}公共静态的byte [] GeneratePDF(字符串pdfPath,字典<字符串,字符串> formFieldMap)
{
    无功输出=新的MemoryStream();
    VAR读卡器=新PdfReader(pdfPath);
    VAR模子=新PdfStamper(读卡器,输出);
    VAR formFields = stamper.AcroFields;    的foreach(在formFieldMap.Keys VAR字段名)
        formFields.SetField(字段名,formFieldMap [字段名]);    stamper.FormFlattening = TRUE;
    stamper.Close();
    reader.Close();    返回output.ToArray();
}
公共静态字符串GetExportValue(AcroFields.Item项)
{
    变种valueDict = item.GetValue(0);
    VAR appearanceDict = valueDict.GetAsDict(PdfName.AP);    如果(appearanceDict!= NULL)
    {
        VAR normalAppearances = appearanceDict.GetAsDict(PdfName.N);        如果(normalAppearances!= NULL)
        {
            的foreach(在normalAppearances.Keys VAR curKey)
                如果(!PdfName.OFF.Equals(curKey))
                    返回curKey.ToString()子串(1)。 //字符串有一个前导/字符,因此将其删除!
        }
    }
    VAR CURVAL = valueDict.GetAsName(PdfName.AS);
    如果(CURVAL!= NULL)
        返回curVal.ToString()子串(1)。
    其他
        返回的String.Empty;
}公共静态无效ReturnPDF(字节[]的内容)
{
    ReturnPDF(内容,NULL);
}公共静态无效ReturnPDF(字节[]内容,串attachmentFilename)
{
    VAR响应= HttpContext.Current.Response;    如果(!string.IsNullOrEmpty(attachmentFilename))
        response.AddHeader(内容处置,附件;文件名=+ attachmentFilename);    response.ContentType =应用程序/ PDF
    response.BinaryWrite(内容);
    到Response.End();
}


解决方案

您code线

  stamper.FormFlattening = TRUE;

指示iTextSharp的扁平化表单域,也就是将它们集成到网页内容,并删除表单域的注释。

由于要保留表单字段为可编辑的字段,不平整的形式。

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

解决方案

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.

这篇关于PDF格式的领域应保持在编辑使用asp.net iTextSharp的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆