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.
-
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).
-
Copy the Free Spire.PDF DLLs (
Spire.Pdf.dll
andSpire.License.dll
) from the...\e-iceblue\Spire.pdf-fe\Bin\NET4.0
folder to theDRIVE:\Inetpub\YOURSITE\wfgen\WfApps\WebForms\Bin
folder. -
Create a new WorkflowGen process with a grid, such as the Investment Application Process (see below for a download link).
-
Create a
FORM_PDF
process data and set its data type to file. This process data will contain the generated PDF file. -
Create a new textbox field in the same section as the grid with the ID
PDF
. -
Hide this textbox for all actions by editing the textbox settings and checking Hide checkbox for all actions.
-
On the Mapping screen in the Form Designer, select the first action, and click the Value OUT button associated to this new textbox.
-
In the Existing data field, select the
FORM_PDF
data. -
In the code-behind, create your new private
Gridview2PDF
method. In this method, you have to save the generated file name in theFORM_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"; }
-
In the code-behind
MySubmitButton_Click
method, call yourGridview2PDF
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(); }
-
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.