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(document, new 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(secondTable, new 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(secondTable, new Point(1,3)); | |
61: | aTable.InsertTable(thirdTable, new Point(6,2)); | |
62: | document.Add(aTable); | |
63: | ||
64: | // relative column widths are preserved | |
65: | ||
66: | Table a = new Table( 2 ); | |
67: | a.Widths = new float[] { 85, 15 }; | |
68: | a.AddCell("a-1"); | |
69: | a.AddCell("a-2"); | |
70: | ||
71: | Table b = new Table(5); | |
72: | b.Widths = new float[] { 15, 7, 7, 7, 7 }; | |
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 Table( 3, 1 ); | |
81: | c.WidthPercentage = 100.0f; | |
82: | c.Widths = new float[] { 20, 2, 78 }; | |
83: | c.InsertTable(a, new Point(0,0) ); | |
84: | c.InsertTable(b, new 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 i = 0; i < 4; i++) | |
108: | { | |
109: | t1.AddCell("t1"); | |
110: | } | |
111: | ||
112: | t2=new Table(3,3); | |
113: | for (int i = 0; i < 9; i++) | |
114: | { | |
115: | if (i == 4) t2.InsertTable(t1); | |
116: | else t2.AddCell("t2"); | |
117: | } | |
118: | ||
119: | Table t3=new Table(4,4); | |
120: | for (int i = 0; i < 16; i++) | |
121: | { | |
122: | if (i == 10) t3.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: | } |