Tech Off Post

Single Post Permalink

View Thread: ASP.NET and iTextSharp (free PDF Generator for .NET)
  • joachim

    OK, let's say I have a PDF document called "Form.pdf" with 4 form fields. The form fileds are "name", "address", "postal_code" and "email".

    Here's the code to create a new PDF file using "Form.pdf" as a template:

    private void fillForm()
    {
        string formFile = @"N:\.NET\Form.pdf";
        string newFile = @"N:\.NET\Filled-out Form.pdf";
        PdfReader reader = new PdfReader(formFile);
        PdfStamper stamper = new PdfStamper(reader, new FileStream(
                    newFile, FileMode.Create));
        AcroFields fields = stamper.AcroFields;

        // set form fields
        fields.SetField("name", "John Doe");
        fields.SetField("address", "2 Milky Way, London");
        fields.SetField("postal_code", "XX1 4YY");
        fields.SetField("email", "johndoe@hotmail.com");

        // flatten form fields and close document
        stamper.FormFlattening = true;
        stamper.Close();
    }

    Acrobat 5 should be enough to create a PDF form. Let me know if you're having any troubles.