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. Image for the cookie policy date
close icon

Pdf Keep Rows together

I'm creating a pdf and adding a PdfGrid. In the PDF grid I need to keep certain rows together on the same
page. So if the, say, 2 rows didn't fit at the end of a page they would be printed on the next page.

I have previously used PDFSharp which had a "KeepWith" property on a table row to specify blocks that 
must be kept together. I can't find a similar keepwith property and was wondering is there one?

I've attached a project that prints a PdfGrid - in it there are two rows ( row part 1, row part 2) that need to be kept on
the same page. If "
row part 1" is at the bottom of a page and "row part 2" is going to be printed on the next page,
then "
row part 1" and "row part 2" both need to be printed on the next page.

Thanks in advance,

Attachment: PdfGridTable_3a593728.7z

9 Replies

SK Surya Kumar Syncfusion Team April 27, 2017 12:58 PM UTC

Hi Matt,  

Thank you for using Syncfusion products, 

We do not have support for Keeping multiple set of PdfGrid rows together in a page, but we can fit individual element (PdfGrid rows) within a page with the help of code snippet below: 

            //Setting Layout format for the PdfGrid 
            PdfGridLayoutFormat format = new PdfGridLayoutFormat(); 
            format.Break = PdfLayoutBreakType.FitElement; 
            //Draws grid to the page of PDF document with speified format. 
            pdfGrid.Draw(page, new PointF(10, 10), format); 

Please find the screenshot for the moved Grid row element to the next page in case if there is lack of space for drawing the next row: 

Please let us know if you need any further information regarding this. 

Surya Kumar 

MT Matt Tracey April 28, 2017 09:19 AM UTC

Thanks for your help - I'll try that,

SK Surya Kumar Syncfusion Team May 1, 2017 06:58 AM UTC

Hi Matt,   
Thanks for the update, we will wait to hear from you. 
Surya Kumar 

EL Emi Lee May 1, 2017 09:31 AM UTC

Thanks for update http://help4assignment.co.uk

MT Matt Tracey May 1, 2017 09:51 AM UTC

I was unable to use FitElement to get it working - I had 2+ rows to keep together.

What I ended up doing was adding just one row to the datatable for each of the "rows" I had and using "\r" to put the next virtual "row"
on a new line:
     string lcCol0 = "Row 1 Col 1 Text";
     string lcCol1 = "Row 1 Col 2 Text";

     lcCol0 += "\r" + "Row 2 Col 1 Text";
     lcCol1 += "\r" + "Row 2 Col 2 Text";

     lcCol0 += "\r" + "Row 3 Col 1 Text";
     lcCol1 += "\r" + "Row 3 Col 2 Text";

     DataTable loTableData = new DataTable();
     loTableData.Rows.Add(new string[] { lcCol0, lcCol1 } );

Then for the Grid I used a BeginCellLayout:
     PdfGrid moGrid = new PdfGrid();
     moGrid.DataSource = loTableData;
     moGrid.BeginCellLayout += moGrid_BeginCellLayout;

The benefit of this was I was able to bold certain strings within the text using BeginCellLayout and PdfHTMLTextElement,
It also allowed me to draw a border at the top and using BeginCellLayout I had to fudge the border - It was too close to the top.

Below is my BeginCellLayout for my specific need, I only needed to do it for col 6:

private static PdfFont moFont10 = new PdfTrueTypeFont(new Font("Calibri", 10), true);

public static void moGrid_BeginCellLayout(object sender, PdfGridBeginCellLayoutEventArgs args)
            PdfHTMLTextElement textElement = new PdfHTMLTextElement();
            textElement.Font = moFont10;

            if (args.CellIndex == 6)
                if (args.Value != "Event category, Consequence and event description")
                    float lnX = args.Bounds.X;
                    float lnY = args.Bounds.Y;
                    float lnL = args.Bounds.Left;
                    float lnT = args.Bounds.Top;
                    float lnW = args.Bounds.Width;
                    float lnH = args.Bounds.Bottom;

                    args.Skip = true;
                    textElement.HTMLText = args.Value;
                    args.Graphics.DrawRectangle(PdfPens.Black, lnL, lnT + 1, lnW, 0);
                    textElement.Draw(args.Graphics, new RectangleF(lnX, lnY+4, lnW, lnH + 4));

There was one little gotcha in the BeginCellLayout when I moved the textElement down by "4" the end of the text
moved down as well, so in the Col6 in the datatable I had to append 
"\r" + "\r" at the end of a text.

Anyway - It works and the plus is it allows me to use HTML formatting in the text.

Thanks again for your help - just leaving this update in case anyone needs a similar solution.


MT Matt Tracey May 1, 2017 09:55 AM UTC

BTW - just an addendum:
I also used the FitElement that was suggested before and this made sure the rows stayed on new pages if the
rows are too big.


PV Prakash Viswanathan Syncfusion Team May 2, 2017 09:44 AM UTC

Hi Matt, 
Thanks for your update. 
Please let us know if you need any further assistance on this. 
Prakash V 

MT Matt Tracey replied to Prakash Viswanathan July 24, 2022 06:43 PM UTC

Hi Prakash,

FYI just received that this message was updated above with SPAM.

From user Arjun Joshi



AA Anitha Azhagesan Syncfusion Team July 25, 2022 05:41 AM UTC

Hi Matt,

We removed the Spam message. Thanks for noticing and letting us know. 



Live Chat Icon For mobile
Up arrow icon