Blog

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