- Home
- Forum
- Xamarin.Forms
- Dynamically Create Rows And Columns According To Number Of Questions
Dynamically Create Rows And Columns According To Number Of Questions
Hello i am creating a simple math application in which we generate simple maths questions and then save these questions in PDF. We are using your pdf services for achieving this functionality. Now Questions are generated successfully in pdf but there are somple problems we are using your table system in this we want to add dynamically rows and columns in pdf according to questions and then show these different questions in a grid form in pdf here is my code thanks
public void GenerateTable()
{
using (PdfDocument document = new PdfDocument())
{
//Add a page
PdfPage page = document.Pages.Add();
//Acquire pages graphics
PdfGraphics graphics = page.Graphics;
PdfStringFormat CenterHeader = new PdfStringFormat(PdfTextAlignment.Center);
PdfFont font = new PdfStandardFont(PdfFontFamily.TimesRoman, 20);
PdfColor color = new PdfColor(240, 248, 255);
PdfSolidBrush brush = new PdfSolidBrush(color);
PdfPen borderPen = new PdfPen(PdfBrushes.LightGray);
borderPen.Width = 2;
//Default Style
PdfCellStyle defStyle = new PdfCellStyle();
defStyle.Font = font;
defStyle.BackgroundBrush = PdfBrushes.WhiteSmoke;
defStyle.BorderPen = borderPen;
defStyle.StringFormat = CenterHeader;
//Alternative Style
PdfCellStyle altStyle = new PdfCellStyle();
altStyle.Font = font;
altStyle.BackgroundBrush = brush;
altStyle.BorderPen = borderPen;
//Header Style
PdfCellStyle headerStyle = new PdfCellStyle(font, PdfBrushes.White, PdfPens.DarkBlue);
brush = new PdfSolidBrush(new PdfColor(0,0,0));
headerStyle.BackgroundBrush = brush;
headerStyle.StringFormat = CenterHeader;
//Declare a PdfLightTable
PdfLightTable pdfLightTable = new PdfLightTable();
PdfMargins margins = new PdfMargins();
PdfLightTableLayoutFormat pdflayout = new PdfLightTableLayoutFormat();
//Set the Data source as direct
pdfLightTable.DataSourceType = PdfLightTableDataSourceType.TableDirect;
// pdfLightTable.Style.AlternateStyle = defStyle;
//Create columns
for(int i=0;i<4;i++)
{
pdfLightTable.Columns.Add(new PdfColumn("Questions"));
}
pdfLightTable.Style.CellPadding = 10;
pdfLightTable.Style.ShowHeader = false;
for (int i = 0; i < NumberOfQuestion; i++)
{
LeftOperant = LeftOperantList[i];
RightOperant = RightOperantList[i];
result = Calculation(LeftOperant, Operator, RightOperant);
QuestionsList.Add(" " + LeftOperant + "\n" + Operator + " " + RightOperant + "\n ------------ \n" + result + "\n ------------ \n");
// pdfLightTable.Rows.Add(new object[] { " "+LeftOperant+"\n"+ Operator+" "+ RightOperant + "\n ------------ \n" + result + "\n ------------ \n", "Noman","Waheed","Developers"});
}
foreach (var item in QuestionsList)
{
pdfLightTable.Rows.Add(new object[] { item,"","",""});
}
//Draw the PdfLightTable
// pdfLightTable.Draw(page, new PointF(50, 50));
pdfLightTable.Style.AlternateStyle = defStyle;
pdfLightTable.Style.DefaultStyle = defStyle;
pdfLightTable.Style.HeaderStyle = headerStyle;
pdfLightTable.Draw(page, PointF.Empty);
//pdfLightTable.Draw(page, 50, 50, 300, pdflayout);
//Save the document
MemoryStream stream = new MemoryStream();
document.Save(stream);
document.Close(true);
//Save the stream as a file in the device and invoke it for viewing
Xamarin.Forms.DependencyService.Get<ISave>().SaveAndView("PDFTable.pdf", "application / pdf", stream);
}
}
SIGN IN To post a reply.
1 Reply
SK
Surya Kumar
Syncfusion Team
February 26, 2019 01:15 PM UTC
Hi Nouman,
Greetings from Syncfusion.
You can dynamically create a data source using "IEnumerable<object>" and set that collection as data source to PdfLighTable using the code snippet below:
Greetings from Syncfusion.
You can dynamically create a data source using "IEnumerable<object>" and set that collection as data source to PdfLighTable using the code snippet below:
| using (PdfDocument document = new PdfDocument()) { //Add a page PdfPage page = document.Pages.Add(); //Acquire pages graphics PdfGraphics graphics = page.Graphics; //Declare a PdfLightTable PdfLightTable pdfLightTable = new PdfLightTable(); //Add values to list List<object> Questions = new List<object>(); for (int i = 0; i < NumberOfQuestion; i++) { LeftOperant = LeftOperantList[i]; RightOperant = RightOperantList[i]; result = Calculation(LeftOperant, Operator, RightOperant); object row = new { Value1 = LeftOperant, Value2 = Operator, Value3 = RightOperant, Value4 = result }; Questions.Add(row); } //Add list to IEnumerable IEnumerable<object> table = Questions; //Assign data source. pdfLightTable.DataSource = table; //Draw the PdfLightTable pdfLightTable.Draw(page, PointF.Empty); //Save the document MemoryStream stream = new MemoryStream(); document.Save(stream); document.Close(true); } |
Refer the below UG documentation link to know more about the table creation in PDF:
https://help.syncfusion.com/file-formats/pdf/working-with-tables
Regards,
Surya Kumar
SIGN IN To post a reply.
- 1 Reply
- 2 Participants
-
NK Nouman Khalid
- Feb 25, 2019 11:00 AM UTC
- Feb 26, 2019 01:15 PM UTC