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.
- Download iTextSharp 5.5.9.
- A 30-day trial license can be obtained at http://developers.itextpdf.com/downloads .
- A quote request can be obtained at 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.
- Copy the iTextSharp DLL (itextsharp.dll) into the DRIVE:\Inetpub\YOURSITE\wfgen\WfApps\WebForms\Bin folder.
- Create a new WorkflowGen process that contains a grid, such as the Investment Application Process (template).
- 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 (edit the textbox settings and check Hide checkbox for all actions).
- 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.
- 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"; }
- 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(); }
- 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.