How can we help you today?

Generate a custom report page from a grid

When you attempt to export a form to PDF, data contained in Grid fields is not included. The solution to this problem is to create a custom report page to display the values in regular form fields.

Take a look at the example form "ReportWithGrid.mfd" attached to this article.

> Page 1 - A grid with several rows of data. In this case we have patient drug dosage data.

> Page 2 - The "report page" - and it is formatted to display those values in a way that will be visible in a PDF export. This one is quite simple, in that it has fields that match each column in the grid. Each grid row is mapped to the dynamic labels on the report page.

> Code - The .NET script editor is leveraged to create a PDF file and manage the field mapping. The function GeneratePDF() handles all of the logic.

''' <summary>
''' Generate our PDF And store an export result indicating success Or failure
''' </summary>
Private Sub GeneratePDF()
' First we find the ID of the blank PDF that's attached. In this specific case,
' the attachment was manually named blank.pdf
Dim xattid As String = GetPDFAttachmentID("blank.pdf")
' Create a render settings object that tells our PDF generator how it should
' handle all types of fields
Dim xrs As New RenderSettings(CTextRenderType.FieldValues, FreeformRenderType.Ink, CheckboxRenderType.Symbol, PageRenderType.Ink)
' Define the filename where the PDF will be written to
Dim strFilename As String = "c:\temp\GridOutput.pdf"
Try
' The object of type PDFDoc (which is in the MiCo.MiForms.DatapathExportModule assembly)
' is the class that will do all the "heavy lifting" of creating the actual
' PDF file
Dim xdoc As New MiCo.MiForms.DataPaths.PDFDoc(_form, xattid, strFilename)
' Here we take a row from the grid and populate data on the report page
Dim table As DataTable = _Grid.Value
For Each row As DataRow In table.Rows
LogMessage(row.Item("Dosage"))
' Clear the field values first
_Dosage.Value = ""
_Drug.Value = ""
_Patient.Value = ""
_Date.Value = ""
' Now populate them from the grid
_Dosage.Value = row.Item("Dosage")
_Drug.Value = row.Item("Drug")
_Patient.Value = row.Item("Patient")
_Date.Value = row.Item("Date")
' Now we create a pdf page
xdoc.AddPage(xrs, 0, 1)
Next
' We close the PDF document to let the engine know we're done with adding pages
xdoc.Close()
' Finally we record that we successfully exported the PDF
Me.RecordExportResult("EXP_PDF", True, "Export to PDF", "Export details to multi-page PDF", "Success in writing PDF", strFilename)
' Show the report
System.Diagnostics.Process.Start(strFilename)
Catch ex As Exception
LogMessage(ex.Message)
' Or that we failed somewhere along the line
Me.RecordExportResult("EXP_PDF", False, "Export to PDF", "Export details to multi-page PDF", "Failed to write PDF: " & ex.Message, strFilename)
End Try
End Sub