I need to make a printed form (report) in which, among other things ( header, footer ), the table will be displayed as shown below in snippet.

I want to give the user a pdf file, for the formation of which I use nuget package version 5.5.10

Tell me. how to carry out, if necessary, the transfer of the entire line ( T1 , T2 , etc.) to a new page

 th, td{ border: 2px solid black; } 
 <table> <tr> <td rowspan = 2>T1</td> <td>OrderCode 1</td> <td>Vendor</td> <td rowspan = 2>Overhang</td> <td rowspan = 2>Runtime</td> </tr> <tr> <td>OrderCode2</td> <td>Vendor</td> </tr> <td rowspan = 2>T2</td> <td>OrderCode 3</td> <td>Vendor</td> <td rowspan = 2>Overhang</td> <td rowspan = 2>Runtime</td> </tr> <tr> <td>OrderCode4</td> <td>Vendor</td> </tr> <td rowspan = 2>T3</td> <td>OrderCode 4</td> <td>Vendor</td> <td rowspan = 2>Overhang</td> <td rowspan = 2>Runtime</td> </tr> <tr> <td>OrderCode5</td> <td>Vendor</td> </tr> </table> 

Code for creating pdf file

 using(var fs = new FileStream(path)) { var document = new Document(PageSize.A4.Rotate(), 10,10,10,10); var pdfWriter = PdfWriter.GetInstance(document, fs); var table = new PdfPTable(5); var tools = new List<int> {1,2,3}; for(var row = 0; row<50; row++) { table.AddCell(new PdfPTable(new Phrase($"T{row}")){Rowspan = tools.Count}); table.AddCell($"Cutter №{tools.First()}); table.AddCell("Vendor"); table.AddCell(new PdfPTable(new Phrase("Overhang")){Rowspan = tools.Count}); table.AddCell(new PdfPTable(new Phrase("Runtime")){Rowspan = tools.Count}); foreach(var tool in tools.Skip(1)) { table.AddCell($"Cutter №{tool}); table.AddCell("Vendor"); } } document.Add(table); document.Close(); pdfWriter.Close(); } 

    1 answer 1

    The problem was solved as follows:

    1. Not one table is added to the document, but a new table is created for each group. 1.1. Add a variable counter in which will be the value of the remaining free space on the sheet.

    2. Before adding a table to a document, it is checked whether there is enough space to add a whole row .

      2.1. If there is enough space, add a line to the document and reduce the variable counter showing how much free space is left.

      2.2. If there is not enough space, we add a new page to the document, reset the counter variable, add the line, reduce the variable by the value of the added line.


     float workspace = 0f; float elapsed = 0f; var random = new Random(); workspace = elapsed = document.PageSize.GetRectangle(document.Top,document.Bottom).Height; var tools = new List<int>{1,2,3,4,5}; for(var row; row<50;row++) { var _tools = tools.Take(random.Next(2,5)); var table = new PdfPTable(5); table.AddCell(new PdfPCell(new Phrase($"T{row+1}")), {Rowspan = _tools.Count()}); table.AddCell($"cutter #{_tools.First()}"); table.AddCell($"Vendor"); table.AddCell(new PdfPCell(new Phrase("125")), {Rowspan = _tools.Count()}); table.AddCell(new PdfPCell(new Phrase("25")), {Rowspan = _tools.Count()}); foreach(var tool in _tools.Skip(1)) { table.AddCell($"cutter #{tool}"); table.AddCell($"Vendor"); } if (elapsed > table.TotalHeight) { document.Add(table); elapsed -= table.TotalHeight; } else { document.NewPage(); document.Add(table); elapsed = workspace - table.TotalHeight; } }