Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>It all really depends on how you are creating the documents. Are you appending to an existing document, or are you generating a new document from scratch? Either way, you are probably going to end up using <code>PageEvents</code> to accomplish this. You can write custom <code>OnPageEnd</code>events to "rewrite" the table headers when the writer reaches the beginning of a new page. if you are doing this from a "from-scratch" perspective, it could get tricky, but you should still be fine, as long as you fire the event <strong>during</strong> your table creation. IF you are appending to an existing document, it may be in your better interest to create a file containing the table(s), and merge them together after you finish making the table. It's not a particularly complicated process either way. If you are creating your table within a for or while loop, like:</p> <pre><code>for(int i = 0; a &lt; 100; i++) { //OnPageEnd Event that adds the Table Header table.addCell(cell) } </code></pre> <p>This way, for every iteration of the loop, it is checking to see if it has to write to a new page. If it doesn't, it will continue adding cells. If it does, it will insert your header row, and continue looping.</p> <p>It should be really simple. I usually create a class file containing my page events, and I call them as I need them. Once you get the class file created, and create your function, it's as simple as adding in the code you use to write the headers currently to the Event. Page Events seem much more daunting than they really are, but once you take a minute and read about them a little bit, you can save yourself a lot of headaches, and you can create much more dynamic documents.</p> <p>Hopefully this points you in a good direction, as I had my own struggles with table creation, and handling page breaks cleanly.</p> <p><strong>Edit</strong></p> <hr> <p>I'll show you a simple method I use for creating let's say a 1 cell table for ease, but it's expandable to as many as you need. This is assuming the position that you are creating a separate document for the table file, and merging them after. This isn't really a part of your question, so I'll leave it out unless you ask for more clarification on how to go about that.</p> <pre><code>private void appendTable(string inputFile, string outputFile, string id) { Document outputDoc = new Document(PageSize.LETTER, 15, 15, 35, 50); PdfReader reader = new PdfReader(inputFile); string sql = string.Format("SELECT * FROM table WHERE id='{0}'", id); try { DataTable dt = db.GetDataTable(sql); PdfPTable table = new PdfPTable(1); iTextSharp.text.Font arial = FontFactory.GetFont("Arial", 8); iTextSharp.text.Font arialheader = FontFactory.GetFont("Arial", 12); table.WidthPercentage = 95; PdfPCell headerCell = new PdfPCell(new Phrase("Cell Header", arialheader)); headerCell.HorizontalAlignment = 1; table.AddCell(headerCell); PdfWriter wr = PdfWriter.GetInstance(outputDoc, new FileStream(outputFile, FileMode.Create, FileAccess.ReadWrite)); foreach(DataRow row in dt.Rows) { PdfPCell cell = new PdfPCell(new Phrase("Cell Text", arial)); meterCell.HorizontalAlignment = 1; //Center Aligned table.AddCell(meterCell); } outputDoc.Open(); wr.PageEvent = new PageEvents.Header(); wr.PageEvent = new PageEvents.Footer(); outputDoc.Add(table); outputDoc.NewPage(); outputDoc.Close(); } catch (PdfException ex) { MessageBox.Show(ex.Message.ToString() + " **** " + ex.StackTrace); } catch (NpgsqlException ex) { MessageBox.Show(ex.Message.ToString() + " **** " + ex.StackTrace); } } </code></pre>
 

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