Category / Section
How to save the data from fields in Xamarin form page to PdfGrid
1 min read
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 pdfdocument PdfDocument doc = new PdfDocument(); //Add the page PdfPage page = doc.Pages.Add(); //Create a new PdfGrid. PdfGrid pdfGrid = new PdfGrid(); //Add three columns. pdfGrid.Columns.Add(2); //Setting string format for all the columns of 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 Xamarin form page pdfGridRow1.Cells[1].Value = Name.Text; PdfGridRow pdfGridRow2 = pdfGrid.Rows.Add(); pdfGridRow2.Cells[0].Value = "Address"; //Get the Address field from Xamarin form page pdfGridRow2.Cells[1].Value = Address.Text; PdfGridRow pdfGridRow3 = pdfGrid.Rows.Add(); pdfGridRow3.Cells[0].Value = "Account Number"; //Get the Account number field from Xamarin form 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: