Blog

 
1:   using System;
2:   using System.IO;
3:  
4:   using iTextSharp.text;
5:   using iTextSharp.text.pdf;
6:  
7:   namespace iTextSharp.tutorial.Chap10
8:   {
9:       public class Chap1004 
10:       {
11:       
12:           public Chap1004() 
13:           {
14:           
15:               Console.WriteLine("Chapter 10 example 4: Templates");
16:           
17:               // step 1: creation of a document-object
18:               Document document new Document();
19:           
20:               try 
21:               {
22:               
23:                   // step 2:
24:                   // we create a writer that listens to the document
25:                   // and directs a PDF-stream to a file
26:                   PdfWriter writer PdfWriter.GetInstance(documentnew FileStream("Chap1004.pdf"FileMode.Create));
27:               
28:                   // step 3: we open the document
29:                   document.Open();
30:               
31:                   // step 4: we grab the ContentByte and do some stuff with it
32:                   PdfContentByte cb writer.DirectContent;
33:               
34:                   // we create a PdfTemplate
35:                   PdfTemplate template cb.CreateTemplate(5050);
36:                   BaseFont bf BaseFont.CreateFont(BaseFont.HELVETICABaseFont.CP1252BaseFont.NOT_EMBEDDED);
37:                   // we add a number of pages
38:                   int i;
39:                   for (15i++) 
40:                   {
41:                       String text "Page " writer.PageNumber " of ";
42:                       float len bf.GetWidthPoint(text12);
43:                       cb.BeginText();
44:                       cb.SetFontAndSize(bf12);
45:                       cb.SetTextMatrix(28040);
46:                       cb.ShowText(text);
47:                       cb.EndText();
48:                       cb.AddTemplate(template280 len40);
49:                       document.NewPage();
50:                   }
51:                   template.BeginText();
52:                   template.SetFontAndSize(bf12);
53:                   template.ShowText((writer.PageNumber 1).ToString());
54:                   template.EndText();
55:               }
56:               catch(DocumentException de
57:               {
58:                   Console.Error.WriteLine(de.Message);
59:               }
60:               catch(IOException ioe
61:               {
62:                   Console.Error.WriteLine(ioe.Message);
63:               }
64:           
65:               // step 5: we close the document
66:               document.Close();
67:           }
68:       }
69:   }