Blog

Friend's Note
Writing 70-270 and 220-601 is easy as long as one is done with 70-291 and 646-204 so that one can concentrate fully on 70-649.
 
1:   using System;
2:   using System.Drawing;
3:   using System.IO;
4:  
5:   using iTextSharp.text;
6:   using iTextSharp.text.pdf;
7:  
8:  
9:   namespace iTextSharp.tutorial.Chap05
10:   {
11:       /// <summary>
12:       /// Nested tables
13:       /// </summary>
14:       public class Chap0514
15:       {
16:           public Chap0514()
17:           {
18:               Console.WriteLine("Chapter 5 example 14: nested tables");
19:               // step 1: creation of a document-object
20:               Document document new Document();
21:               try 
22:               {
23:                   // step 2:
24:                   // we create a writer that listens to the document
25:                   // and directs a PDF-stream to a file
26:                   PdfWriter.GetInstance(documentnew FileStream("Chap0514.pdf"FileMode.Create));
27:                   // step 3: we open the document
28:                   document.Open();
29:                   // step 4: we create a table and add it to the document
30:               
31:                   // simple example
32:               
33:                   Table secondTable new Table(2);
34:                   secondTable.AddCell("2nd table 0.0");
35:                   secondTable.AddCell("2nd table 0.1");
36:                   secondTable.AddCell("2nd table 1.0");
37:                   secondTable.AddCell("2nd table 1.1");
38:               
39:                   Table aTable new Table(4,4);    // 4 rows, 4 columns
40:                   aTable.AutoFillEmptyCells true;
41:                   aTable.AddCell("2.2"new Point(2,2));
42:                   aTable.AddCell("3.3"new Point(3,3));
43:                   aTable.AddCell("2.1"new Point(2,1));
44:                   aTable.InsertTable(secondTablenew Point(1,3));
45:                   document.Add(aTable);
46:               
47:                   // example with 2 nested tables
48:               
49:                   Table thirdTable new Table(2);
50:                   thirdTable.AddCell("3rd table 0.0");
51:                   thirdTable.AddCell("3rd table 0.1");
52:                   thirdTable.AddCell("3rd table 1.0");
53:                   thirdTable.AddCell("3rd table 1.1");
54:               
55:                   aTable new Table(5,5);
56:                   aTable.AutoFillEmptyCells true;
57:                   aTable.AddCell("2.2"new Point(2,2));
58:                   aTable.AddCell("3.3"new Point(3,3));
59:                   aTable.AddCell("2.1"new Point(2,1));
60:                   aTable.InsertTable(secondTablenew Point(1,3));
61:                   aTable.InsertTable(thirdTablenew Point(6,2));
62:                   document.Add(aTable);        
63:               
64:                   // relative column widths are preserved
65:               
66:                   Table a new Table);
67:                   a.Widths new float[] { 8515 };
68:                   a.AddCell("a-1");
69:                   a.AddCell("a-2");
70:               
71:                   Table b new Table(5);
72:                   b.Widths new float[] { 15777};
73:                   b.AddCell("b-1");
74:                   b.AddCell("b-2");
75:                   b.AddCell("b-3");
76:                   b.AddCell("b-4");
77:                   b.AddCell("b-5");
78:               
79:                   // now, insert these 2 tables into a third for layout purposes
80:                   Table c new Table3);
81:                   c.WidthPercentage 100.0f;
82:                   c.Widths new float[] { 20278 };
83:                   c.InsertTable(anew Point(0,0) );
84:                   c.InsertTable(bnew Point(0,2) );
85:  
86:                   document.Add(c);
87:               
88:                   // adding extra cells after adding a table
89:               
90:                   Table t1 new Table(3);
91:                   t1.AddCell("1.1");
92:                   t1.AddCell("1.2");
93:                   t1.AddCell("1.3");
94:                   // nested
95:                   Table t2 new Table(2);
96:                   t2.AddCell("2.1");
97:                   t2.AddCell("2.2");
98:               
99:                   // now insert the nested
100:                   t1.InsertTable(t2);
101:                   t1.AddCell("new cell");    // correct row/column ?
102:                   document.Add(t1);       
103:               
104:                   // deep nesting
105:               
106:                   t1=new Table(2,2); 
107:                   for (int 04i++) 
108:                   {
109:                       t1.AddCell("t1");
110:                   }
111:               
112:                   t2=new Table(3,3);
113:                   for (int 09i++) 
114:                   {
115:                       if (== 4t2.InsertTable(t1);
116:                       else t2.AddCell("t2"); 
117:                   }
118:               
119:                   Table t3=new Table(4,4);
120:                   for (int 016i++) 
121:                   {
122:                       if (== 10t3.InsertTable(t2);
123:                       else t3.AddCell("t3"); 
124:                   }
125:  
126:                   document.Add(t3); 
127:               }
128:               catch(DocumentException de
129:               {
130:                   Console.Error.WriteLine(de.Message);
131:               }
132:               catch(IOException ioe
133:               {
134:                   Console.Error.WriteLine(ioe.Message);
135:               }
136:               // step 5: we close the document
137:               document.Close();
138:  
139:           }
140:       }
141:   }