Blog

 
1:   using System;
2:   using System.IO;
3:  
4:   using iTextSharp.text;
5:   using iTextSharp.text.pdf;
6:  
7:  
8:   namespace iTextSharp.tutorial.Chap04
9:   {
10:       /// <summary>
11:       ///HeaderFooter
12:       /// </summary>
13:       public class Chap0401
14:       {
15:           public Chap0401()
16:           {
17:               Console.WriteLine("Chapter 4 example 1: Headers en Footers");
18:           
19:               // step 1: creation of a document-object
20:               Document document new Document();
21:           
22:               try 
23:               {
24:                   // step 2: we create a writer that listens to the document
25:                   PdfWriter.GetInstance(documentnew FileStream("Chap0401.pdf"FileMode.Create));
26:               
27:                   // we Add a Footer that will show up on PAGE 1
28:                   HeaderFooter footer new HeaderFooter(new Phrase("This is page: "), true);
29:                   footer.Border Rectangle.NO_BORDER;
30:                   document.Footer footer;
31:               
32:                   // step 3: we open the document
33:                   document.Open();
34:               
35:                   // we Add a Header that will show up on PAGE 2
36:                   HeaderFooter header new HeaderFooter(new Phrase("This is a header"), false);
37:                   document.Header header;
38:               
39:                   // step 4: we Add content to the document
40:               
41:                   // PAGE 1
42:                   document.Add(new Paragraph("Hello World"));
43:                   // we trigger a page break
44:                   document.NewPage();
45:               
46:                   // PAGE 2
47:                   // we Add some more content
48:                   document.Add(new Paragraph("Hello Earth"));
49:                   // we remove the header starting from PAGE 3
50:                   document.ResetHeader();
51:                   // we trigger a page break
52:                   document.NewPage();
53:               
54:                   // PAGE 3
55:                   // we Add some more content
56:                   document.Add(new Paragraph("Hello Sun"));
57:                   document.Add(new Paragraph("Remark: the header has vanished!"));
58:                   // we reset the page numbering
59:                   document.ResetPageCount();
60:                   // we trigger a page break
61:                   document.NewPage();
62:               
63:                   // PAGE 4
64:                   // we Add some more content
65:                   document.Add(new Paragraph("Hello Moon"));
66:                   document.Add(new Paragraph("Remark: the pagenumber has been reset!"));
67:               
68:               }
69:               catch(DocumentException de
70:               {
71:                   Console.Error.WriteLine(de.Message);
72:               }
73:               catch(IOException ioe
74:               {
75:                   Console.Error.WriteLine(ioe.Message);
76:               }
77:           
78:               // step 5: we close the document
79:               document.Close();
80:  
81:           }
82:       }
83:   }