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:       /// Table offset
12:       /// Before a table is added to a document. A newline is added using the current leading 
13:       /// (i.e. the leading of the previous object that was added). 
14:       /// Sometimes you don't want this because the space between the table and the previous object is to large/small.
15:       ///  If you want to change this space, you have to set the offset of the table 
16:       /// </summary>
17:       public class Chap0517
18:       {
19:           public Chap0517()
20:           {
21:               Console.WriteLine("Chapter 5 example 17: table offset");
22:               // step 1: creation of a document-object
23:               Document document new Document();
24:               try 
25:               {
26:                   // step 2:
27:                   // we create a writer that listens to the document
28:                   // and directs a PDF-stream to a file
29:                   PdfWriter.GetInstance(documentnew FileStream("Chap0517.pdf"FileMode.Create));
30:                   // step 3: we open the document
31:                   document.Open();
32:                   // step 4: we create a table and add it to the document
33:                   Table table new Table(3);
34:                   table.BorderWidth 1;
35:                   table.BorderColor new Color(00255);
36:                   table.Padding 5;
37:                   table.Spacing 5;
38:                   Cell cell new Cell("header");
39:                   cell.Header true;
40:                   cell.Colspan 3;
41:                   table.AddCell(cell);
42:                   cell new Cell("example cell with colspan 1 and rowspan 2");
43:                   cell.Rowspan 2;
44:                   cell.BorderColor new Color(25500);
45:                   table.AddCell(cell);
46:                   table.AddCell("1.1");
47:                   table.AddCell("2.1");
48:                   table.AddCell("1.2");
49:                   table.AddCell("2.2");
50:                   table.AddCell("cell test1");
51:                   cell new Cell("big cell");
52:                   cell.Rowspan 2;
53:                   cell.Colspan 2;
54:                   cell.BackgroundColor new Color(0xC00xC00xC0);
55:                   table.AddCell(cell);
56:                   table.AddCell("cell test2");
57:                   document.Add(new Paragraph("repeating the same table 10 times, but with different offsets:"));
58:                   document.Add(table);
59:                   document.Add(new Paragraph("blah blah."));
60:                   document.Add(table);
61:                   document.Add(new Paragraph("we increase the offset."));
62:                   table.Offset 32;
63:                   document.Add(table);
64:                   document.Add(new Paragraph("blah blah."));
65:                   document.Add(table);
66:                   document.Add(new Paragraph("blah blah."));
67:                   document.Add(table);
68:                   document.Add(new Paragraph("we use an offset 0."));
69:                   table.Offset 0;
70:                   document.Add(table);
71:                   document.Add(new Paragraph("blah blah."));
72:                   document.Add(table);
73:                   document.Add(new Paragraph("blah blah."));
74:                   document.Add(table);
75:                   document.Add(new Paragraph("A negative offset."));
76:                   table.Offset = -16;
77:                   document.Add(table);
78:                   document.Add(new Paragraph("blah blah."));
79:                   document.Add(table);
80:               }
81:               catch(DocumentException de
82:               {
83:                   Console.Error.WriteLine(de.Message);
84:               }
85:               catch(IOException ioe
86:               {
87:                   Console.Error.WriteLine(ioe.Message);
88:               }
89:               // step 5: we close the document
90:               document.Close();
91:  
92:           }
93:       }
94:   }