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(document, new 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(50, 50); | |
36: | BaseFont bf = BaseFont.CreateFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.NOT_EMBEDDED); | |
37: | // we add a number of pages | |
38: | int i; | |
39: | for (i = 1; i < 5; i++) | |
40: | { | |
41: | String text = "Page " + writer.PageNumber + " of "; | |
42: | float len = bf.GetWidthPoint(text, 12); | |
43: | cb.BeginText(); | |
44: | cb.SetFontAndSize(bf, 12); | |
45: | cb.SetTextMatrix(280, 40); | |
46: | cb.ShowText(text); | |
47: | cb.EndText(); | |
48: | cb.AddTemplate(template, 280 + len, 40); | |
49: | document.NewPage(); | |
50: | } | |
51: | template.BeginText(); | |
52: | template.SetFontAndSize(bf, 12); | |
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: | } |