Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to write and split a datatable on different pages using itextsharp?
    primarykey
    data
    text
    <p>first at all, here is my code:</p> <pre><code> string pdfTemplate = Application.StartupPath + "\\Templates\\Template Medico.pdf"; string newFile = Application.StartupPath + "\\Relatórios\\Relatório Médico_" + dataArquivo + ".pdf"; PdfReader pdfReader = new PdfReader(pdfTemplate); PdfStamper pdfStamper = new PdfStamper(pdfReader, new FileStream(newFile, FileMode.Create)); AcroFields pdfFormFields = pdfStamper.AcroFields; // Form Filling pdfFormFields.SetField("data", data); pdfFormFields.SetField("medico_nome", campo); pdfFormFields.SetField("numero_consultas", numeroConsultas); // Table Building int numColumns = ds.Tables[0].Columns.Count; PdfPTable datatable = new PdfPTable(numColumns); datatable.DefaultCell.Padding = 10; datatable.WidthPercentage = 100; datatable.DefaultCell.HorizontalAlignment = Element.ALIGN_LEFT; float[] columnWidths = { 80, 80, 80, 80, 80, 80, 80, 80, 80 }; datatable.SetWidths(columnWidths); float[] totalWidth = { 80, 80, 80, 80, 80, 80, 80, 80, 80 }; datatable.SetTotalWidth(totalWidth); // Table Header for (int k = 0; k &lt; ds.Tables[0].Columns.Count; k++) { Phrase collumname = new Phrase(ds.Tables[0].Columns[k].ColumnName, FontFactory.GetFont("Verdana", 9)); datatable.AddCell(collumname); } // Lines and Columns if (ds.Tables[0].Rows.Count &lt;= 9) // less than 9 rows = no problem, no new pages and table spliting needed { for (int i = 0; i &lt; ds.Tables[0].Rows.Count; i++) { for (int j = 0; j &lt; ds.Tables[0].Columns.Count; j++) { datatable.DefaultCell.BackgroundColor = iTextSharp.text.BaseColor.WHITE; datatable.DefaultCell.HorizontalAlignment = iTextSharp.text.Element.ALIGN_LEFT; Phrase phrase = new Phrase(ds.Tables[0].Rows[i][j].ToString(), FontFactory.GetFont("Verdana", 9)); datatable.AddCell(phrase); } } // Write down the table on page 2 PdfContentByte content = pdfStamper.GetUnderContent(2); datatable.WriteSelectedRows(0, -1, 70.0f, 495.0f, content); } else { int newPage = 3; int currentPage = 2; for (int i = 0; i &lt; ds.Tables[0].Rows.Count; i++) { for (int j = 0; j &lt; ds.Tables[0].Columns.Count; j++) { datatable.DefaultCell.BackgroundColor = iTextSharp.text.BaseColor.WHITE; datatable.DefaultCell.HorizontalAlignment = iTextSharp.text.Element.ALIGN_LEFT; Phrase phrase = new Phrase(ds.Tables[0].Rows[i][j].ToString(), FontFactory.GetFont("Verdana", 9)); datatable.AddCell(phrase); } if (i &gt; 0 &amp;&amp; i % 9 == 0) { //How do i print the table ONLY every 9 iterations? (9 iterations = 9 rows) PdfContentByte content = pdfStamper.GetUnderContent(currentPage); // Or how do i make a loop using the rowStart and rowEnd arguments of the // WriteSelectedRows function in order to write only a set of 9 rows for each page? datatable.WriteSelectedRows(0, -1, 70.0f, 495.0f, content); pdfStamper.InsertPage(newPage, pdfReader.GetPageSize(2)); newPage++; currentPage++; } } } </code></pre> <p>The code is well commented with the main issues i'm having at the moment, but the real bigger one, as described on the thread title is to somehow "split" or "control" the loop/iteration through the rows and columns to match the page, jump to the other page, and keep writing the table.</p> <p>Any help will be apreciated.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload