We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. (Last updated on: June 24, 2019).
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

Dynamically Create Rows And Columns According To Number Of Questions

Thread ID:

Created:

Updated:

Platform:

Replies:

142895 Feb 25,2019 11:00 AM UTC Feb 26,2019 01:15 PM UTC Xamarin.Forms 1
loading
Tags: PDF
Nouman Khalid
Asked On February 25, 2019 11:00 AM UTC

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);
            }
}

Surya Kumar [Syncfusion]
Replied On 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:
 
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 
 


CONFIRMATION

This post will be permanently deleted. Are you sure you want to continue?

Sorry, An error occured while processing your request. Please try again later.

Please sign in to access our forum

This page will automatically be redirected to the sign-in page in 10 seconds.

Warning Icon You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.Close Icon

Live Chat Icon For mobile
Live Chat Icon