This is a migrated thread and some comments may be shown as answers.

Prevent table from being split on 2 pages

2 Answers 384 Views
PdfProcessing
This is a migrated thread and some comments may be shown as answers.
Tim
Top achievements
Rank 3
Iron
Iron
Iron
Tim asked on 13 Jul 2020, 06:25 PM

I'm creating a Pdf document several pages long with several tables of varying heights. All of the tables are small enough to fit on a single page. If I just add the tables one after another, some will be split on more than one page. Is it possible to track the current y position on a page and add a page break if a table would be split onto a new page?

Thanks,

 

Tim

2 Answers, 1 is accepted

Sort by
0
Accepted
Dimitar
Telerik team
answered on 14 Jul 2020, 08:18 AM

Hello Tim,

Yes, this is possible. You will need to measure the table and check if it will fit on the same page. Here is a sample implementation: 

RadFixedDocument document = new RadFixedDocument();
var page = document.Pages.AddPage();

var editor = new FixedContentEditor(page);
var currentPosition = new Point(0, 0);

foreach (var item in tables)
{
    var size = item.Measure();

    if (size.Height < (page.Size.Height - currentPosition.Y))
    {
        editor.Position.Translate(currentPosition.X, currentPosition.Y);
        currentPosition = new Point(0, currentPosition.Y + size.Height + 10);
        editor.DrawTable(item);
    }
    else
    {
        page = document.Pages.AddPage();
        editor = new FixedContentEditor(page);
        editor.DrawTable(item);
        currentPosition = new Point(0, size.Height + 10);
    }

}

var provider = new PdfFormatProvider();
File.WriteAllBytes(@"..\..\result.pdf", provider.Export(document));

I hope this helps. Should you have any other questions, do not hesitate to ask.

Regards,
Dimitar
Progress Telerik

0
Tim
Top achievements
Rank 3
Iron
Iron
Iron
answered on 14 Jul 2020, 06:11 PM

This works great.

Thanks for your help!

 

Tim

Tags
PdfProcessing
Asked by
Tim
Top achievements
Rank 3
Iron
Iron
Iron
Answers by
Dimitar
Telerik team
Tim
Top achievements
Rank 3
Iron
Iron
Iron
Share this question
or