Percentage width for columns in PdfGrid

  1. In our requirement we often need to set column width in percentages. 

Can you assist on how we can achieve it? Please see bold text in code snippet.


        public void ColumnPercentages()

        {

            using (PdfDocument pdfDocument = new PdfDocument())

            {

                //Create the page

                var section = pdfDocument.Sections.Add();

                var pdfPage = section.Pages.Add();

                PdfGrid parentGrid = new PdfGrid();

                parentGrid.Columns.Add(2);

                parentGrid.Rows.Add().Height = 200;


                // Put text to the left cell

                parentGrid.Rows[0].Cells[0].Value = "Grid on the right should have justified columns";


                // Put grid to the right cell

                PdfGrid rightGrid = new PdfGrid();

                parentGrid.Rows[0].Cells[1].Value = rightGrid;


                rightGrid.Columns.Add(3);


                // How to set in percentages?

                rightGrid.Columns[0].Width = 20%;

                rightGrid.Columns[1].Width = 30%;

                rightGrid.Columns[2].Width = 50%;


                var row = rightGrid.Rows.Add();

                row.Height = 200;

                row.Cells[0].Style.BackgroundBrush = new PdfSolidBrush(new PdfColor(Color.FromArgb(255, 109, 175, 61)));

                row.Cells[1].Style.BackgroundBrush = new PdfSolidBrush(new PdfColor(Color.FromArgb(255, 10, 17, 161)));

                row.Cells[2].Style.BackgroundBrush = new PdfSolidBrush(new PdfColor(Color.FromArgb(255, 109, 17, 61)));


                //Draw the parent PdfGrid

                parentGrid.Draw(pdfPage, PointF.Empty, new PdfGridLayoutFormat { Break = PdfLayoutBreakType.FitPage, Layout = PdfLayoutType.Paginate });


                //Save the document

                pdfDocument.Save("NestedTable.pdf");

                //Close the document

                pdfDocument.Close(true);

                //This will open the PDF file so, the result will be seen in default PDF viewer

                System.Diagnostics.Process.Start("NestedTable.pdf");

            }

        }


For the most part as a workaround we try to maintain a reference to a column of a parent grid. Get the width of that column which than allows converting percentages into actual column width. But sometimes parent column doesn't have width set either.


1 Reply 1 reply marked as answer

GK Gowthamraj Kumar Syncfusion Team December 31, 2021 09:46 AM UTC

Hi umlprog, 
 
On our further analysis, we do not have support to set the PdfGrid column width as percentage value. However, we can achieve this requirement in sample level to calculate width based on the percentage value from the total width. Please refer the below code snippet, 
 
 
PdfGrid parentGrid = new PdfGrid(); 
 
parentGrid.Columns.Add(2); 
 
parentGrid.Rows.Add().Height = 200; 
 
// Put text to the left cell 
 
parentGrid.Rows[0].Cells[0].Value = "Grid on the right should have justified columns"; 
                                
// Put grid to the right cell 
 
PdfGrid rightGrid = new PdfGrid(); 
 
parentGrid.Rows[0].Cells[1].Value = rightGrid; 
 
rightGrid.Columns.Add(3); 
 
// How to set in percentages? 
float totaColumnslWidth = 180; 
 
//rightGrid.Columns[0].Width = 20 %; 
rightGrid.Columns[0].Width = totaColumnslWidth * (20f / 100); 
 
//rightGrid.Columns[1].Width = 30 %; 
rightGrid.Columns[1].Width = totaColumnslWidth * (30f / 100); 
 
//rightGrid.Columns[2].Width = 50 %; 
rightGrid.Columns[2].Width = totaColumnslWidth * (50f / 100); 
 
var row = rightGrid.Rows.Add(); 
 
row.Height = 200; 
 
row.Cells[0].Style.BackgroundBrush = new PdfSolidBrush(new PdfColor(Color.FromArgb(255, 109, 175, 61))); 
 
row.Cells[1].Style.BackgroundBrush = new PdfSolidBrush(new PdfColor(Color.FromArgb(255, 10, 17, 161))); 
 
row.Cells[2].Style.BackgroundBrush = new PdfSolidBrush(new PdfColor(Color.FromArgb(255, 109, 17, 61))); 
 
 
 
Kindly try the above workaround solution in your end and let us know the result. 
 
Regards, 
Gowthamraj K 


Marked as answer
Loader.
Up arrow icon