Blog

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