Blog

 
1:   using System;
2:   using System.IO;
3:  
4:   using iTextSharp.text;
5:   using iTextSharp.text.pdf;
6:  
7:   namespace iTextSharp.tutorial.Chap12
8:   {
9:       class MyTableEvent : IPdfPTableEvent 
10:       {    
11:           public void TableLayout(PdfPTable table, float[][] width, float[] heights, int headerRows, int rowStart, PdfContentByte[] canvases) 
12:           {
13:               float[] widths = width[0];
14:               PdfContentByte cb = canvases[PdfPTable.TEXTCANVAS];
15:               cb.SaveState();
16:               cb.SetLineWidth(2);
17:               cb.SetRGBColorStroke(255, 0, 0);
18:               cb.Rectangle(widths[0], heights[heights.Length - 1], widths[widths.Length - 1] - widths[0], heights[0] - heights[heights.Length - 1]);
19:               cb.Stroke();
20:               if (headerRows > 0) 
21:               {
22:                   float headerHeight = heights[0];
23:                   for (int k = 0; k < headerRows; ++k)
24:                       headerHeight += heights[k];
25:                   cb.SetRGBColorStroke(0, 0, 255);
26:                   cb.Rectangle(widths[0], heights[headerRows], widths[widths.Length - 1] - widths[0], heights[0] - heights[headerRows]);
27:                   cb.Stroke();
28:               }
29:               cb.RestoreState();
30:               cb = canvases[PdfPTable.BASECANVAS];
31:               cb.SaveState();
32:               cb.SetLineWidth(.5f);
33:               Random r = new Random();
34:               for (int line = 0; line < heights.Length - 1; ++line) 
35:               {
36:                   widths = width[line];
37:                   for (int col = 0; col < widths.Length - 1; ++col) 
38:                   {
39:                       if (line == 0 && col == 0)
40:                           cb.SetAction(new PdfAction("http://www.Geocities.Com/itextpdf"),
41:                               widths[col], heights[line + 1], widths[col + 1], heights[line]);
42:                       cb.SetRGBColorStrokeF((float)r.NextDouble(), (float)r.NextDouble(), (float)r.NextDouble());
43:                       cb.MoveTo(widths[col], heights[line]);
44:                       cb.LineTo(widths[col + 1], heights[line]);
45:                       cb.Stroke();
46:                       cb.SetRGBColorStrokeF((float)r.NextDouble(), (float)r.NextDouble(), (float)r.NextDouble());
47:                       cb.MoveTo(widths[col], heights[line]);
48:                       cb.LineTo(widths[col], heights[line + 1]);
49:                       cb.Stroke();
50:                   }
51:               }
52:               cb.RestoreState();
53:           }
54:       }
55:  
56:       public class Chap1202 
57:       {
58:       
59:           public Chap1202() 
60:           {
61:           
62:               Console.WriteLine("Chapter 12 example 2: Table events");
63:           
64:               // step 1: creation of a document-object
65:               Document document = new Document(PageSize.A4, 50, 50, 50, 50);
66:               try 
67:               {
68:                   // step 2: we create a writer that listens to the document
69:                   PdfWriter writer = PdfWriter.GetInstance(document, new FileStream("Chap1202.pdf", FileMode.Create));
70:                   // step 3: we open the document
71:                   document.Open();
72:                   // step 4: we add some content
73:                   BaseFont bf = BaseFont.CreateFont(BaseFont.HELVETICA, BaseFont.WINANSI, BaseFont.NOT_EMBEDDED);
74:                   // table 1
75:                   PdfPTable table = new PdfPTable(4);
76:                   table.DefaultCell.Border = Rectangle.NO_BORDER;
77:                   for (int k = 0; k < 24; ++k) 
78:                   {
79:                       if (k != 0)
80:                           table.AddCell("" + k);
81:                       else
82:                           table.AddCell("This is an URL");
83:                   }
84:                   MyTableEvent evnt = new MyTableEvent();
85:                   table.TableEvent = evnt;
86:                   table.TotalWidth = 300;
87:                   // write table 1 at some position
88:                   table.WriteSelectedRows(0, -1, 100, 600, writer.DirectContent);
89:                   // add table 1 (default position)
90:                   document.Add(table);
91:                   document.NewPage();
92:                   // table 2
93:                   table = new PdfPTable(4);
94:                   float fontSize = 12;
95:                   table.DefaultCell.PaddingTop = bf.GetFontDescriptor(BaseFont.ASCENT, fontSize) - fontSize + 2;
96:                   table.DefaultCell.Border = Rectangle.NO_BORDER;
97:                   for (int k = 0; k < 500 * 4; ++k) 
98:                   {
99:                       if (k == 0)
100:                           table.AddCell(new Phrase("This is an URL", new Font(bf, fontSize)));
101:                       else
102:                           table.AddCell(new Phrase("" + k, new Font(bf, fontSize)));
103:                   }
104:                   table.TableEvent = evnt;
105:                   table.HeaderRows = 3;
106:                   document.Add(table);
107:               }
108:               catch (Exception de) 
109:               {
110:                   Console.Error.WriteLine(de.Message);
111:                   Console.Error.WriteLine(de.StackTrace);
112:               }
113:               // step 5: close the document
114:               document.Close();
115:           }
116:       }
117:   }