# How to generate a PDF file from a form’s gridview with iText

**URL:** <https://discuss.workflowgen.com/t/how-to-generate-a-pdf-file-from-a-form-s-gridview-with-itext/300>\
**Category:** Form Designer\
**Created:** [June 15, 2016, 11:01pm UTC](https://discuss.workflowgen.com/t/how-to-generate-a-pdf-file-from-a-form-s-gridview-with-itext/300 "2016-06-15T23:01:49Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![wfg-admin](https://yyz2.discourse-cdn.com/flex030/user_avatar/discuss.workflowgen.com/wfg-admin/32/714_2.png) [@wfg-admin](https://discuss.workflowgen.com/u/wfg-admin)\
**Post date:** [June 15, 2016, 11:01pm UTC](https://discuss.workflowgen.com/t/how-to-generate-a-pdf-file-from-a-form-s-gridview-with-itext/300/1 "2016-06-15T23:01:49Z")

</div>

You can easily integrate PDF document generation into a workflow. This article provides an example of how to use the iText PDF .NET library (iTextSharp) to generate a PDF file from a form’s gridview, such as the Investment Application process (template) form grid.

The generated file will be available as one of the first action’s OUT parameters.

1. Download iTextSharp 5.5.9.  
  

  - A 30-day trial license can be obtained at [http://developers.itextpdf.com/downloads](http://developers.itextpdf.com/downloads).
  
  - A quote request can be obtained at [http://itextpdf.com/Pricing/unit-based](http://itextpdf.com/Pricing/unit-based).
  
  - **2019 UPDATE** : iTextSharp can now be downloaded directly from the NuGet Manager in Visual Studio. Create a solution, download the package and then copy the generated DLLs as indicated in step 2.

  
 
2. Copy the iTextSharp DLL ( **itextsharp.dll** ) into the **DRIVE:\Inetpub\YOURSITE\wfgen\WfApps\WebForms\Bin** folder.
  
3. Create a new WorkflowGen process that contains a grid, such as the Investment Application Process (template).
  
4. Create a "FORM\_PDF" process data and set its data type to file. This process data will contain the generated PDF file.
  
5. Create a new textbox field in the same section as the grid with the ID "PDF". Hide this textbox for all actions (edit the textbox settings and check **Hide checkbox for all actions** ).
  
6. Using the Mapping tool in the Form Designer, select the first action, then click the Value OUT button associated to this new textbox. Select the "FORM\_PDF" data from the **Existing data** drop-down list.
  
7. In the code-behind, create your new private Gridview2PDF method. In this method you have to save the generated file name in the process data "FORM\_PDF". This is how the generated file will be accessible from the follow-up form (for more information on this topic, see the [Form designer: How to generate a PDF version of the form archive](https://discuss.workflowgen.com/t/form-designer-how-to-generate-a-pdf-version-of-the-form-archive/268/9) knowledge base article). 

```auto
// Generate PDF file with Gridview datas
private void Gridview2PDF()
{
        // Create the PDF file
        System.IO.FileStream fileStream = new System.IO.FileStream(this.StoragePath + @"\investment_pdf.pdf", System.IO.FileMode.Create); 
        
        // Create a document
        iTextSharp.text.Document doc = new iTextSharp.text.Document(iTextSharp.text.PageSize.A4, 80, 50, 30, 65);
        iTextSharp.text.pdf.PdfWriter writer = iTextSharp.text.pdf.PdfWriter.GetInstance(doc, fileStream);
        doc.Open();
        
        iTextSharp.text.pdf.PdfPTable myTable = new iTextSharp.text.pdf.PdfPTable(this.INVESTMENTS_GRIDVIEW.HeaderRow.Cells.Count - 1);
        
        // Add grid header
        for (int i = 0; i this.INVESTMENTS_GRIDVIEW.HeaderRow.Cells.Count - 1; i++) {
            myTable.AddCell(((Label)this.INVESTMENTS_GRIDVIEW.HeaderRow.Cells[i].Controls[0]).Text);
        }
      
        // Add grid values
        for (int i = 0; i this.INVESTMENTS_GRIDVIEW.Rows.Count - 1; i++) {
            for (int j = 0; j this.INVESTMENTS_GRIDVIEW.Rows[i].Cells.Count - 1; j++) {
                myTable.AddCell(((Label)this.INVESTMENTS_GRIDVIEW.Rows[i].Cells[j].Controls[0]).Text);
            }
        }
        
        // Add the table to the document
        doc.Add(myTable);
        
        doc.Close();
        writer.Close();
        fileStream.Close();
        
        // Save file name in PDF textbox field
        this.INVESTMENTS_PDF.Text = "investment_pdf.pdf";
}

```

8. In the code-behind MySubmitButton\_Click method, call your Gridview2PDF method. 

```auto
protected void MySubmitButton_Click(object sender, EventArgs e)  
{
        
      // Generate PDF file
      Gridview2PDF();
      
      // Your code...
      this.INVESTMENTS_TOTAL.Text = ((Label)this.INVESTMENTS_GRIDVIEW.FooterRow.Cells[2].Controls[0]).Text;

      // If you modify the form field's value then you have to call the 2 instructions below at the end.
      this.SaveFormData(this.FormData, true);
      SubmitToWorkflow();
}

```

9. To test, launch a request using this newly created process, enter information into the form's grid, submit the request, go to the follow-up form, and click the link associated to the FORM\_PDF data description to download the generated PDF file.

Download the [INVESTMENT\_APP\_ITEXTSHARPv1](https://www.workflowgen.com/wp-content/uploads/2016/06/INVESTMENT_APP_ITEXTSHARPv1.zip) process, unzip it, and import the XPDL into WorkflowGen to see it in action.

---

<div class="post-metadata">

**Author:** ![james](https://avatars.discourse-cdn.com/v4/letter/j/bc8723/32.png) [@james](https://discuss.workflowgen.com/u/james)\
**Post date:** [December 18, 2018, 3:46pm UTC](https://discuss.workflowgen.com/t/how-to-generate-a-pdf-file-from-a-form-s-gridview-with-itext/300/2 "2018-12-18T15:46:56Z")

</div>

Is the example solution “INVESTMENT\_APP\_ITEXTSHARPv1” still available to download?

Thanks,

James

---

<div class="post-metadata">

**Author:** ![james](https://avatars.discourse-cdn.com/v4/letter/j/bc8723/32.png) [@james](https://discuss.workflowgen.com/u/james)\
**Post date:** [December 19, 2018, 8:05pm UTC](https://discuss.workflowgen.com/t/how-to-generate-a-pdf-file-from-a-form-s-gridview-with-itext/300/3 "2018-12-19T20:05:15Z")

</div>

I am having a problem with step 6. I am able to create “FORM\_PDF” on the “Data” tab as step 4 instructs, but in step 6 it is not a choice in the “Existing Data” dropdownlist and the “New Data” field already has the ID name using the section ID name and textbox ID. If I try changing the “New Data” field to FORM\_PDF I receive the popup message “A data with this name already exists in the current process. You must enter a new data name or select an existing data from the list.”

I am using version 7.6.

Thanks,

James

---

<div class="post-metadata">

**Author:** ![eddy.daouk](https://yyz2.discourse-cdn.com/flex030/user_avatar/discuss.workflowgen.com/eddy.daouk/32/672_2.png) [@eddy.daouk](https://discuss.workflowgen.com/u/eddy.daouk)\
**Post date:** [December 27, 2018, 3:59pm UTC](https://discuss.workflowgen.com/t/how-to-generate-a-pdf-file-from-a-form-s-gridview-with-itext/300/4 "2018-12-27T15:59:36Z")

</div>

Hi @james,

Instead of using the Mapping tool, go directly in the action’s parameter and add an additional parameter

You can search for the appropriate TextBox ID by using the “Browse…” button, then select the FORM\_PDF data as an output parameter.

 ![Capture](https://canada1.discourse-cdn.com/flex030/uploads/workflowgen/original/1X/a2b3400e16d804104ab7adeef0aefc2cf75198f7.png)

Regards,  
Eddy.
