Blog

 
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:       /// Tables at an absolute position
12:       /// iTextSharp.text.Table is a rather simple class that generates tables in a standard way, 
13:       /// but sometimes you want a table to have some specific behaviour. 
14:       /// In those cases you will need to use the more complex class com.lowagie.text.pdf.PdfPTable
15:       /// </summary>
16:       public class Chap0518
17:       {
18:           public Chap0518()
19:           {
20:               Console.WriteLine("Chapter 5 example 18: PdfPTable");
21:               // step 1: creation of a document-object
22:               Document document new Document(PageSize.A4.Rotate(), 10101010);
23:               try 
24:               {
25:                   // step 2: we create a writer that listens to the document
26:                   PdfWriter.GetInstance(documentnew FileStream("Chap0518.pdf"FileMode.Create));
27:                   // step 3: we open the document
28:                   document.Open();
29:                   // step 4: we add content to the document (this happens in a seperate method)
30:                   loadDocument(document);
31:               }
32:               catch (Exception e2
33:               {
34:                   Console.WriteLine(e2);
35:               }
36:               // step 5: we close the document
37:               document.Close();
38:           }
39:       
40:           public static void loadDocument(Document document
41:           {
42:               String[] bogusData = { "M0065920",
43:                                        "SL",
44:                                        "FR86000P",
45:                                        "PCGOLD",
46:                                        "119000",
47:                                        "96 06",
48:                                        "2001-08-13",
49:                                        "4350",
50:                                        "6011648299",
51:                                        "FLFLMTGP",
52:                                        "153",
53:                                        "119000.00"
54:                                    };
55:               int NumColumns 12;
56:               try 
57:               {
58:                   // we add some meta information to the document
59:               
60:                   PdfPTable datatable new PdfPTable(NumColumns);
61:               
62:                   datatable.DefaultCell.Padding 3;
63:                   float[] headerwidths = {9481081197910410}; // percentage
64:                   datatable.SetWidths(headerwidths);
65:                   datatable.WidthPercentage 100// percentage
66:               
67:                   datatable.DefaultCell.BorderWidth 2;
68:                   datatable.DefaultCell.HorizontalAlignment Element.ALIGN_CENTER;
69:                   datatable.AddCell("Clock #");
70:                   datatable.AddCell("Trans Type");
71:                   datatable.AddCell("Cusip");
72:                   datatable.AddCell("Long Name");
73:                   datatable.AddCell("Quantity");
74:                   datatable.AddCell("Fraction Price");
75:                   datatable.AddCell("Settle Date");
76:                   datatable.AddCell("Portfolio");
77:                   datatable.AddCell("ADP Number");
78:                   datatable.AddCell("Account ID");
79:                   datatable.AddCell("Reg Rep ID");
80:                   datatable.AddCell("Amt To Go ");
81:               
82:                   datatable.HeaderRows 1;  // this is the end of the table header
83:               
84:                   datatable.DefaultCell.BorderWidth 1;
85:               
86:                   int max 666;
87:                   for (int 1maxi++) 
88:                   {
89:                       if (== 1
90:                       {
91:                           datatable.DefaultCell.GrayFill 0.9f;
92:                       }
93:                       for (int 0NumColumnsx++) 
94:                       {
95:                           datatable.AddCell(bogusData[x]);
96:                       }
97:                       if (== 1
98:                       {
99:                           datatable.DefaultCell.GrayFill 0.0f;
100:                       }
101:                   }
102:                   document.Add(datatable);
103:               }
104:               catch(Exception e
105:               {
106:                   Console.Error.WriteLine(e.StackTrace);
107:               }
108:  
109:           }
110:       }
111:   }