How to create pdfformfields using iTextSharp?

前端 未结 3 1304
再見小時候
再見小時候 2020-12-21 14:25

I am using iTextSharp and CSharp for creating the pdf. I need to add formfields like checkbox, radiobutton and dropdown which can not be edited.

I used this..

<
3条回答
  •  南方客
    南方客 (楼主)
    2020-12-21 14:58

    You can create your custom form template using LiveCycle and then data bind the form fields using iTextSharp like this

    string randomFileName = Helpers.GetRandomFileName();
    string formTemplate = Server.MapPath("~/FormTemplate.pdf");
     string formOutput = Server.MapPath(string.Format("~/downloads/Forms/Form-{0}.pdf",    randomFileName));
    
    PdfReader reader = new PdfReader(formTemplate);
    PdfStamper stamper = new PdfStamper(reader, new System.IO.FileStream(formOutput,   System.IO.FileMode.Create));
    AcroFields fields = stamper.AcroFields;
    
    // set form fields
    fields.SetField("Date", DateTime.Now.ToShortDateString());
    fields.SetField("FirstName", user.FirstName);
    fields.SetField("LastName", user.LastName);
    fields.SetField("Address1", user.Address1);
    fields.SetField("Address2", user.Address2);
    fields.SetField("City", user.City);
    fields.SetField("State", user.State);
    fields.SetField("Zip", user.Zip);
    fields.SetField("Email", user.Email);
    fields.SetField("Phone", user.Phone);
    
    // set document info
    System.Collections.Hashtable info = new System.Collections.Hashtable();
    info["Title"] = "User Information Form";
    info["Author"] = "My Client";
    info["Creator"] = "My Company";
    stamper.MoreInfo = info;
    
     // flatten form fields and close document
    stamper.FormFlattening = true;
    stamper.Close();
    

提交回复
热议问题