1: | using System; | |
2: | using System.IO; | |
3: | ||
4: | using iTextSharp.text; | |
5: | using iTextSharp.text.pdf; | |
6: | ||
7: | ||
8: | namespace iTextSharp.tutorial.Chap05 | |
9: | { | |
10: | /// <summary> | |
11: | /// Memory management | |
12: | /// When you add objects to a Document, | |
13: | /// these objects are written to the outputstream as soon as possible. | |
14: | /// But when you construct a table, the Table-object keeps on growing. | |
15: | /// For really large tables, this can become a problem. | |
16: | /// </summary> | |
17: | public class Chap0513 | |
18: | { | |
19: | public Chap0513() | |
20: | { | |
21: | Console.WriteLine("Chapter 5 example 13: large tables with fitspage"); | |
22: | // creation of the document with a certain size and certain margins | |
23: | Document document = new Document(PageSize.A4.Rotate(), 50, 50, 50, 50); | |
24: | ||
25: | try | |
26: | { | |
27: | // creation of the different writers | |
28: | PdfWriter writer = PdfWriter.GetInstance(document, new FileStream("Chap0513.pdf", FileMode.Create)); | |
29: | ||
30: | // we add some meta information to the document | |
31: | document.AddAuthor("Alan Soukup"); | |
32: | document.AddSubject("This is the result of a Test."); | |
33: | ||
34: | document.Open(); | |
35: | ||
36: | Table datatable = getTable(); | |
37: | ||
38: | for (int i = 1; i < 30; i++) | |
39: | { | |
40: | ||
41: | datatable.DefaultHorizontalAlignment = Element.ALIGN_LEFT; | |
42: | ||
43: | datatable.AddCell("myUserId"); | |
44: | datatable.AddCell("Somebody with a very, very, very, very, very, very, very, very, very, very, very, very, very, very, very, very, very long long name"); | |
45: | datatable.AddCell("No Name Company"); | |
46: | datatable.AddCell("D" + i); | |
47: | ||
48: | datatable.DefaultHorizontalAlignment = Element.ALIGN_CENTER; | |
49: | datatable.AddCell("No"); | |
50: | datatable.AddCell("Yes"); | |
51: | datatable.AddCell("No"); | |
52: | datatable.AddCell("Yes"); | |
53: | datatable.AddCell("No"); | |
54: | datatable.AddCell("Yes"); | |
55: | ||
56: | if (!writer.FitsPage(datatable)) | |
57: | { | |
58: | datatable.DeleteLastRow(); | |
59: | i--; | |
60: | document.Add(datatable); | |
61: | document.NewPage(); | |
62: | datatable = getTable(); | |
63: | } | |
64: | ||
65: | } | |
66: | ||
67: | ||
68: | document.Add(datatable); | |
69: | } | |
70: | catch(Exception e) | |
71: | { | |
72: | Console.WriteLine(e.StackTrace); | |
73: | } | |
74: | ||
75: | // we close the document | |
76: | document.Close(); | |
77: | } | |
78: | ||
79: | private static Table getTable() | |
80: | { | |
81: | Table datatable = new Table(10); | |
82: | ||
83: | datatable.Padding = 4; | |
84: | datatable.Spacing = 0; | |
85: | //datatable.setBorder(Rectangle.NO_BORDER); | |
86: | float[] headerwidths = {10, 24, 12, 12, 7, 7, 7, 7, 7, 7}; | |
87: | datatable.Widths = headerwidths; | |
88: | datatable.WidthPercentage = 100; | |
89: | ||
90: | // the first cell spans 10 columns | |
91: | Cell cell = new Cell(new Phrase("Administration -System Users Report", FontFactory.GetFont(FontFactory.HELVETICA, 24, Font.BOLD))); | |
92: | cell.HorizontalAlignment = Element.ALIGN_CENTER; | |
93: | cell.Leading = 30; | |
94: | cell.Colspan = 10; | |
95: | cell.Border = Rectangle.NO_BORDER; | |
96: | cell.BackgroundColor = new Color(0xC0, 0xC0, 0xC0); | |
97: | datatable.AddCell(cell); | |
98: | ||
99: | // These cells span 2 rows | |
100: | datatable.DefaultCellBorderWidth = 2; | |
101: | datatable.DefaultHorizontalAlignment = 1; | |
102: | datatable.DefaultRowspan = 2; | |
103: | datatable.AddCell("User Id"); | |
104: | datatable.AddCell(new Phrase("Name", FontFactory.GetFont(FontFactory.HELVETICA, 14, Font.BOLD))); | |
105: | datatable.AddCell("Company"); | |
106: | datatable.AddCell("Department"); | |
107: | ||
108: | // This cell spans the remaining 6 columns in 1 row | |
109: | datatable.DefaultRowspan = 1; | |
110: | datatable.DefaultColspan = 6; | |
111: | datatable.AddCell("Permissions"); | |
112: | ||
113: | // These cells span 1 row and 1 column | |
114: | datatable.DefaultColspan = 1; | |
115: | datatable.AddCell("Admin"); | |
116: | datatable.AddCell("Data"); | |
117: | datatable.AddCell("Expl"); | |
118: | datatable.AddCell("Prod"); | |
119: | datatable.AddCell("Proj"); | |
120: | datatable.AddCell("Online"); | |
121: | ||
122: | datatable.DefaultCellBorderWidth = 1; | |
123: | datatable.DefaultRowspan = 1; | |
124: | ||
125: | return datatable; | |
126: | ||
127: | } | |
128: | } | |
129: | } |