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:       /// Force a table or a cell to fit a page
12:       /// </summary>
13:       public class Chap0511
14:       {
15:           public Chap0511()
16:           {
17:               Console.WriteLine("Chapter 5 example 11: avoid table splitting");
18:               // step 1: creation of a document-object
19:               Document document new Document();
20:               try 
21:               {
22:                   // step 2:
23:                   // we create a writer that listens to the document
24:                   // and directs a PDF-stream to a file
25:                   PdfWriter.GetInstance(documentnew FileStream("Chap0511.pdf"FileMode.Create));
26:                   // step 3: we open the document
27:                   document.Open();
28:                   // step 4: we create a table and add it to the document
29:                   Table table new Table(3);
30:                   table.TableFitsPage true;
31:                   table.BorderWidth 1;
32:                   table.BorderColor new Color(00255);
33:                   table.Padding 5;
34:                   table.Spacing 5;
35:                   Cell cell new Cell("header");
36:                   cell.Header true;
37:                   cell.Colspan 3;
38:                   table.AddCell(cell);
39:                   cell new Cell("example cell with colspan 1 and rowspan 2");
40:                   cell.Rowspan 2;
41:                   cell.BorderColor new Color(25500);
42:                   table.AddCell(cell);
43:                   table.AddCell("1.1");
44:                   table.AddCell("2.1");
45:                   table.AddCell("1.2");
46:                   table.AddCell("2.2");
47:                   table.AddCell("cell test1");
48:                   cell new Cell("big cell");
49:                   cell.Rowspan 2;
50:                   cell.Colspan 2;
51:                   cell.BackgroundColor new Color(0xC00xC00xC0);
52:                   table.AddCell(cell);
53:                   table.AddCell("cell test2");
54:                   document.Add(new Paragraph("repeating the same table 10 times:"));
55:                   for (int 010i++) 
56:                   {
57:                       document.Add(table);
58:                   }
59:               }
60:               catch(DocumentException de
61:               {
62:                   Console.Error.WriteLine(de.Message);
63:               }
64:               catch(IOException ioe
65:               {
66:                   Console.Error.WriteLine(ioe.Message);
67:               }
68:               // step 5: we close the document
69:               document.Close();
70:  
71:           }
72:       }
73:   }