Blog

 
1:   using System;
2:   using System.IO;
3:  
4:   using iTextSharp.text;
5:   using iTextSharp.text.pdf;
6:  
7:   namespace iTextSharp.tutorial.Chap01
8:   {
9:       /// <summary>
10:       /// Page initializations
11:       /// </summary>
12:       public class Chap0107
13:       {
14:           public Chap0107()
15:           {
16:               Console.WriteLine("Chapter 1 example 7: newPage()");
17:           
18:               // step 1: creation of a document-object
19:               Document document new Document();
20:           
21:               try 
22:               {
23:               
24:                   // step 2:
25:                   // we create a writer that listens to the document
26:                   // and directs a PDF-stream to a file
27:               
28:                   PdfWriter.GetInstance(documentnew FileStream("Chap0107.pdf"FileMode.Create));
29:               
30:                   // step 3:
31:               
32:                   // we Add a Watermark that will show up on PAGE 1
33:                   try 
34:                   {
35:                       Watermark watermark new Watermark(Image.GetInstance("watermark.jpg"), 200420);
36:                       document.Add(watermark);
37:                   }
38:                   catch 
39:                   {
40:                       Console.Error.WriteLine("Are you sure you have the file 'watermark.jpg' in the right path?");
41:                   }
42:               
43:                   // we Add a Header that will show up on PAGE 1
44:                   HeaderFooter header new HeaderFooter(new Phrase("This is a header"), false);
45:                   document.Header header;
46:               
47:                   // we open the document
48:                   document.Open();
49:               
50:                   // we rotate the page, starting from PAGE 2
51:                   document.SetPageSize(PageSize.A4.Rotate());
52:               
53:                   // we need to change the position of the Watermark
54:                   try 
55:                   {
56:                       Watermark watermark new Watermark(Image.GetInstance("watermark.jpg"), 320200);
57:                       document.Add(watermark);
58:                   }
59:                   catch 
60:                   {
61:                       Console.Error.WriteLine("Are you sure you have the file 'watermark.jpg' in the right path?");
62:                   }
63:               
64:                   // we Add a Footer that will show up on PAGE 2
65:                   HeaderFooter footer new HeaderFooter(new Phrase("This is page: "), true);
66:                   document.Footer footer;
67:               
68:                   // step 4: we Add content to the document
69:               
70:                   // PAGE 1
71:               
72:                   document.Add(new Paragraph("Hello World"));
73:               
74:                   // we trigger a page break
75:                   document.NewPage();
76:               
77:                   // PAGE 2
78:               
79:                   // we Add some more content
80:                   document.Add(new Paragraph("Hello Earth"));
81:               
82:                   // we remove the header starting from PAGE 3
83:                   document.ResetHeader();
84:               
85:                   // we trigger a page break
86:                   document.NewPage();
87:               
88:                   // PAGE 3
89:               
90:                   // we Add some more content
91:                   document.Add(new Paragraph("Hello Sun"));
92:                   document.Add(new Paragraph("Remark: the header has vanished!"));
93:               
94:                   // we reset the page numbering
95:                   document.ResetPageCount();
96:               
97:                   // we trigger a page break
98:                   document.NewPage();
99:               
100:                   // PAGE 4
101:               
102:                   // we Add some more content
103:                   document.Add(new Paragraph("Hello Moon"));
104:                   document.Add(new Paragraph("Remark: the pagenumber has been reset!"));
105:               
106:               }
107:               catch(DocumentException de
108:               {
109:                   Console.Error.WriteLine(de.Message);
110:               }
111:               catch(IOException ioe
112:               {
113:                   Console.Error.WriteLine(ioe.Message);
114:               }
115:           
116:               // step 5: we close the document
117:               document.Close();
118:  
119:           }
120:       }
121:   }