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


#1

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.


  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 knowledge base article).
    // 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.
    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 process, unzip it, and import the XPDL into WorkflowGen to see it in action.


#2

Is the example solution “INVESTMENT_APP_ITEXTSHARPv1” still available to download?

Thanks,

James


#3

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


#4

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.

Regards,
Eddy.