Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The easiest way to do that is to put your individual tables into a master 3-column table. Below is code that does that. You'll probably want to adjust margins, widths and borders but this should get you started at least.</p> <p>Also, since you said you were new to iTextSharp I'm going to assume that you don't have a specific need for using <code>DirectContent</code>. DC is very powerful but most of what you need to do with iTextSharp you can do through specific objects instead. The code below has all DC stuff removed.</p> <pre><code>//(iText 5.1.1.0) using System; using System.Collections.Generic; using System.Linq; using System.Text; using iTextSharp.text; using iTextSharp.text.pdf; using System.IO; namespace ConsoleApplication1 { class Program { static void Main(string[] args) { string filePath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "column_example.pdf"); Console.WriteLine("-&gt; Creates a PDF file with a block of Text."); Document document = new Document(PageSize.LETTER); try { PdfWriter writer = PdfWriter.GetInstance(document, new FileStream(filePath, FileMode.Create)); document.Open(); //Create a master table with 3 columns PdfPTable masterTable = new PdfPTable(3); //Set the column widths, this should probably be adjusted masterTable.SetWidths(new float[] { 200, 200, 200 }); PdfPTable table; PdfPCell cell; Phrase phrase; string line = "Line{0}"; for (int i = 0; i &lt; 50; i++) { table = new PdfPTable(1); table.SpacingAfter = 9F; cell = new PdfPCell(new Phrase("Header for table " + i)); table.AddCell(cell); for (int j = 0; j &lt; (i % 2 == 0 ? 5 : 7); j++) { phrase = new Phrase(string.Format(line, i)); cell = new PdfPCell(phrase); table.AddCell(cell); } //Add the sub-table to our master table instead of the writer masterTable.AddCell(table); } //Add the master table to our document document.Add(masterTable); } catch (DocumentException de) { Console.Error.WriteLine(de.Message); } catch (IOException ioe) { Console.Error.WriteLine(ioe.Message); } finally { document.Close(); } Console.ReadLine(); } } } </code></pre> <p><strong>EDIT</strong></p> <p>Sorry, I didn't understand from your original post what you were looking for but do now. Unfortunately you are entering the realm of Math and Mod. I don't have to time (or the brain power this morning) to go through this completely but hopefully I can give you a start.</p> <p>The entire programming world is based on left-to-right and <em>then</em> top-to-bottom, when you switch it around you tend to have to jump through giant hoops to do what you want (like making an HTML list into 3 columns alphabetized with A's in column 1, B's in column 2, etc.)</p> <p>In order to do what you want you need to know the heights of the tables so that you can calculate how many vertically that you can get on the page. Unfortunately table height isn't known until render time. The solution (at least for me) is to draw each table to a temporary document which allows us to know the height, then we store the table in an array and throw away the document. Now we've got an array of tables with known heights that we can walk through.</p> <p>The snippet below does all of this. I changed your row count rule to a random number from 2 to 9 just to get more variety in the sample data. Also, starting with iTextSharp 5.1 (I think that's the right version) many of the "big" objects support <code>IDisposable</code> so I'm <code>using</code>. If you are using an older version you'll need to drop the <code>using</code> and switch to normal variable declaration. Hopefully the comments make sense. You'll see that I pulled out some magic numbers into variables, too.</p> <pre><code> //Our array of tables List&lt;PdfPTable&gt; Tables = new List&lt;PdfPTable&gt;(); //Create a random number of rows to get better sample data int rowCount; Random r = new Random(); string line = "Line {0}"; PdfPTable table; //This is the horizontal padding between tables float hSpace = 5; //Total number of columns that we want int columnCount = 3; //Create a temporary document to write our table to so that their sizes can be calculated using (Document tempDoc = new Document(PageSize.LETTER)) { using (MemoryStream tempMS = new MemoryStream()) { using (PdfWriter tempW = PdfWriter.GetInstance(tempDoc, tempMS)) { tempDoc.Open(); //Calculate the table width which is the usable space minus the padding between tables divided by the column count float documentUseableWidth = tempDoc.PageSize.Width - tempDoc.LeftMargin - tempDoc.RightMargin; float totalTableHPadding = (hSpace * (columnCount - 1)); float tableWidth = (documentUseableWidth - totalTableHPadding) / columnCount; for (int i = 0; i &lt; 50; i++) { table = new PdfPTable(1); table.AddCell(new PdfPCell(new Phrase("Header for table " + i))); rowCount = r.Next(2, 10); for (int j = 0; j &lt; rowCount; j++) { table.AddCell(new PdfPCell(new Phrase(string.Format(line, i)))); } //In order to use WriteSelectedRows you need to set the width of the table table.SetTotalWidth(new float[] { tableWidth }); //Write the table to our temporary document in order to calculate the height table.WriteSelectedRows(1, table.Rows.Count, 0, 0, tempW.DirectContent); //Add the table to our array Tables.Add(table); } tempDoc.Close(); } } } </code></pre> <p>Once you've got your array of tables you can loop through those and draw them using:</p> <pre><code>Tables[i].WriteSelectedRows(1, Tables[i].Rows.Count, curX, curY, writer.DirectContent); </code></pre> <p>Where <code>i</code> is your current table index and <code>curX</code> and <code>curY</code> are your current coordinates.</p> <p>Hopefully this gets you going in the right direction. <code>WriteSelectedRows</code> does a great job of putting a table exactly where you want it.</p> <p>One last thing to remember, the <code>Y</code> coordinate that it takes starts at the bottom of the document, not the top, so 0 is the bottom and 720 is "above" it and not below.</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. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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