How to save the data from fields in Xamarin form page to PdfGrid
How to save the data from fields in Xamarin form page to PdfGrid
The PdfGrid can be created with data fetched from different fields in Xamarin form page and can be drawn in a PDF document. The below code snippet demonstrates the same.
C#
// Create the PDF document
PdfDocument doc = new PdfDocument();// Add a page
PdfPage page = doc.Pages.Add();// Create a new PdfGrid
PdfGrid pdfGrid = new PdfGrid();// Add two columns
pdfGrid.Columns.Add(2);// Set string format for all the columns of the PdfGrid
PdfStringFormat format = new PdfStringFormat();
format.Alignment = PdfTextAlignment.Center;
format.LineAlignment = PdfVerticalAlignment.Bottom;
pdfGrid.Columns[0].Format = format;
pdfGrid.Columns[1].Format = format;// Add header
pdfGrid.Headers.Add(1);
PdfGridRow pdfGridHeader = pdfGrid.Headers[0];
pdfGridHeader.Cells[0].Value = "Field";
pdfGridHeader.Cells[1].Value = "Data";
pdfGridHeader.Style.Font = new PdfStandardFont(PdfFontFamily.Helvetica, 10, PdfFontStyle.Bold);// Add rows
PdfGridRow pdfGridRow1 = pdfGrid.Rows.Add();
pdfGridRow1.Cells[0].Value = "Name";// Get the Name field from the Xamarin Forms page
pdfGridRow1.Cells[1].Value = Name.Text;
PdfGridRow pdfGridRow2 = pdfGrid.Rows.Add();
pdfGridRow2.Cells[0].Value = "Address";// Get the Address field from the Xamarin Forms page
pdfGridRow2.Cells[1].Value = Address.Text;
PdfGridRow pdfGridRow3 = pdfGrid.Rows.Add();
pdfGridRow3.Cells[0].Value = "Account Number";// Get the Account Number field from the Xamarin Forms page
pdfGridRow3.Cells[1].Value = Account.Text;// Draw the PdfGrid
pdfGrid.Draw(page, PointF.Empty);// Save and close the modified document
MemoryStream stream = new MemoryStream();
doc.Save(stream);
doc.Close();
Sample link:
Note:
Starting with v16.2.0.x, if you reference Syncfusion® assemblies from a trial setup or from the NuGet feed, include a license key in your projects. Refer to the link to learn about generating and registering the Syncfusion® license key in your application to use the components without a trial message.
Conclusion
I hope you enjoyed learning about how to save the data from fields in Xamarin form page to PdfGrid.