The PDF DataPath is a useful utility to create a PDF version of your form. It allows you considerable flexibility regarding how the export will look with features like selective pages, raster or vector output, custom locations, etc.  But when you are implementing some custom functionality in your forms the static nature of predefining the export can necessitate alternate methods. For example, this may become necessary if your form has pages that are hidden or visible based on some conditional statement. Alternatively, you may need to perform the PDF export while the form is still in use, and since the DataPath only runs upon finishing the form we need to seek out alternates as well.


Here I will exaplain a very flexible method we use to create PDF exports of forms. 


Prerequisites: Adobe Acrobat Standard and Mi-Forms Designer. 


Step 1. Create a PDF version of your form. From the Mi-Forms Designer, select File>Print (or Ctrl+P) to open the printer dialog box. You can name this file as whatever you like as long as you take note of the name. I chose background.pdf.



Step 2. Attach the PDF as an attachment to the form. From the Mi-Forms Designer, select File>Form Properties and choose the Attachments Tab. Pick the Add Button and select background.pdf 



Step 3. Add the DatapathExportModule to the form script references. From the Mi-Form Script Editor (Ctrl+R) click on the References Tab at the lower left portion of the screen. Right click in the box below that contains the name of all of the form references and select Add Reference. Select Browse and navigate to your Mi-Forms Designer installation folder,select MiCo.MiForms.DatapathExportModule.dll, and select Open. Click Add on the resulting screen and the window will close. You can now see the file in the Reference Name list.



Step 4. Create a custom function to handle the PDF export. Here is an example of a how to generate PDF pages from script. You will want to make note of the following parts:

             - We used a constant DATAPATH_EXPORT_LOCATION to define the location of the export.

             - The name of the resulting PDF file will be the [SessionId].pdf. This will result in a unique name for all exports.

             - The method RecordExportResult() sends a datapath export status message to the server. 

     

Private Const DATAPATH_EXPORT_LOCATION As String = "c:\temp\FormExport\"

' Dynamically create a PDF and store it locally.
Public Sub GeneratePDF(ByVal pdfAttachment As String)
	Try
		' construct pdf file name
		Dim pdfFilePath As String = DATAPATH_EXPORT_LOCATION & _form.SessionData(0).SessionID & ".pdf"

		' get attachment ID
		Dim attachmentID As String
		For Each xatt As AttachmentData In _Form.Attachments
			If (xatt.Name = pdfAttachment) Then
				attachmentID = xatt.ID
			End If
		Next
		
		' get attachment pdf - must include a reference to MiCo.MiForms.DataPathExportModule in the Designer install directory
		Dim pdfDoc As New MiCo.MiForms.DataPaths.PDFDoc(_form, attachmentID, pdfFilePath)
		
		' render settings init
		Dim xrs As New RenderSettings(CTextRenderType.FieldValues, FreeformRenderType.Ink, CheckboxRenderType.Symbol, PageRenderType.Ink, RegionRenderType.Render)
		
		' copy pages to the new PDF document
		AddPagesToPDF(pdfDoc, xrs)

		' close the pdf doc
		pdfDoc.Close()
		
		' record export result
		Me.RecordExportResult("PDF Export", True, "Success", "PDF Export Successful", "Success")
	Catch ex As Exception
		LogMessage(ex.Message)
		Me.RecordExportResult("PDF Export", False, "Failed - " & ex.Message, "PDF Export Failed - " & ex.Message, "Failed - " & ex.Message)
	End Try
End Sub

     

Step 5. Determine the pages you want to export. This function is separated to let you handle the logic regarding which pages will be exported. The loop, if/then statement, and field value setting methods are only used as a demonstration of how complex logic can look. In some cases all you will need is to specify the PDFDoc.AddPage(xrs, 0, 0) part. Remember, that the pages here are 0-indexed. The first 0 relates to the page on the background.pdf image, the second 0 is the form page you want to export.


 

Public Sub AddPagesToPDF(ByRef pdfDoc As MiCo.MiForms.DataPaths.PDFDoc, ByRef xrs As MiCo.MiForms.RenderSettings)
	' add logic to determine which pages will be added to the PDF file with PDFDoc function
	PDFDoc.AddPage(xrs, 0, 0)
	For i As Int16 = 1 To Integer.Parse(_SRO_LINE_COUNT.Value)
		If i > 1 Then
			_SRO_LINE_CURRENT_COUNT.Value = i
			PDFDoc.AddPage(xrs, 1, i)
		End If
	Next 
	' signature page always exists
	PDFDoc.AddPage(xrs, 2, 2)
End Sub

 


Step 6. Select an event to trigger this export. If you are simply using this method to facilitate complex PDF generation during the form finish, the standard place to make the call is in the AfterDataPathsRun event. However, you can associate the function to any event in the form such as an AfterSetData for a Picklist, or the AfterInkAdded method in a Hotspot. Note that we are sending GeneratePDF() the parameter "background.pdf." This is the name of the attachment we added earlier.


 

<MiCode(FormScriptEventType.AfterDataPathsRun)> _ 
Public Sub Form_AfterDataPathsRun(ByVal e As AfterDataPathsRunEventArgs)
	GeneratePDF("background.pdf")
End Sub

 

This is just one example of how you can accomplish PDF Exports with more flexibility. I have seen this done many different ways but this is meant to be used as a good starting point and to give you some new ideas.