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

You can easily integrate PDF document generation into a workflow. This article provides an example of how to use the Free Spire.PDF library 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 Free Spire.PDF 3.2 (free version) from http://www.e-iceblue.com/Download/download-pdf-for-net-free.html.

    Note: Free Spire.PDF for .NET is a Community Edition of Spire.PDF for .NET (see https://freepdf.codeplex.com/documentation for details).

  2. Copy the Free Spire.PDF DLLs (Spire.Pdf.dll and Spire.License.dll) from the ...\e-iceblue\Spire.pdf-fe\Bin\NET4.0 folder to the DRIVE:\Inetpub\YOURSITE\wfgen\WfApps\WebForms\Bin folder.

  3. Create a new WorkflowGen process with a grid, such as the Investment Application Process (see below for a download link).

  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.

  6. Hide this textbox for all actions by editing the textbox settings and checking Hide checkbox for all actions.

  7. On the Mapping screen in the Form Designer, select the first action, and click the Value OUT button associated to this new textbox.

  8. In the Existing data field, select the FORM_PDF data.

  9. In the code-behind, create your new private Gridview2PDF method. In this method, you have to save the generated file name in the FORM_PDF process data. 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 topic).

    private void Gridview2PDF()
    {
            System.IO.FileStream fileStream = new System.IO.FileStream(this.StoragePath + @"\investment_pdf.pdf", System.IO.FileMode.Create); 
        
            // Create a pdf document.
            Spire.Pdf.PdfDocument doc = new Spire.Pdf.PdfDocument();
        
            // Create one page
            Spire.Pdf.PdfPageBase page = doc.Pages.Add(Spire.Pdf.PdfPageSize.A4);
        
            String[,] dataSource = new String[this.INVESTMENTS_GRIDVIEW.Rows.Count, (this.INVESTMENTS_GRIDVIEW.HeaderRow.Cells.Count - 1)];
            int index = 0;
    
            // Add header
            for (int j = 0; j < this.INVESTMENTS_GRIDVIEW.HeaderRow.Cells.Count - 1; j++) {
                dataSource[index, j] = ((Label)this.INVESTMENTS_GRIDVIEW.HeaderRow.Cells[0].Controls[0]).Text;
            }
    
            index++;
        
            // 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++) {
                    dataSource[index, j] = ((Label)this.INVESTMENTS_GRIDVIEW.Rows[i].Cells[j].Controls[0]).Text;
                }
                index++;
            }
    
            // Create table
            Spire.Pdf.Tables.PdfTable table = new Spire.Pdf.Tables.PdfTable();
            table.Style.CellPadding = 2;
            table.Style.HeaderSource = Spire.Pdf.Tables.PdfHeaderSource.Rows;
            table.Style.HeaderRowCount = 1;
            table.Style.ShowHeader = true;
            table.DataSource = dataSource;
        
            Spire.Pdf.Graphics.PdfLayoutResult result = table.Draw(page, new System.Drawing.PointF(0, 10));
    
            // Save pdf file.
            doc.SaveToStream(fileStream);
            fileStream.Close();
            doc.Close();
        
            this.INVESTMENTS_PDF.Text = "investment_pdf.pdf";
    }
    
  10. 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();
    }
    
  11. 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_SPIREPDFv1 process, unzip it, and import the XPDL into WorkflowGen to see it in action.

So the very question I have, can it be done from a fillable form that is in PDF already, like this one: online-form-builder.pdffiller.com with Spire?

Hi @pHumphrey,

No, the form needs to be readable so that Spire can take the values of the gridview by taking its datasource and converting it to a PDF file.

Regards,
Eddy.