1: | using System; | |
2: | using System.IO; | |
3: | using System.Collections; | |
4: | ||
5: | using iTextSharp.text; | |
6: | using iTextSharp.text.pdf; | |
7: | ||
8: | ||
9: | namespace iTextSharp.tutorial.Chap02 | |
10: | { | |
11: | /// <summary> | |
12: | /// Some advanced stuff | |
13: | /// </summary> | |
14: | class Glossary : PdfPageEventHelper | |
15: | { | |
16: | ||
17: | // we will keep a glossary of words and the pages they appear on in a TreeMap | |
18: | SortedList glossary = new SortedList(); | |
19: | ||
20: | // we override only the onGenericTag method | |
21: | public override void OnGenericTag(PdfWriter writer, Document document, Rectangle rect, String text) | |
22: | { | |
23: | glossary.Add(text, writer.PageNumber); | |
24: | } | |
25: | ||
26: | // we Add a method to retrieve the glossary | |
27: | public SortedList GetGlossary() | |
28: | { | |
29: | return glossary; | |
30: | } | |
31: | ||
32: | } | |
33: | ||
34: | /// <summary> | |
35: | /// Chap0209 ժҪ˵ | |
36: | /// </summary> | |
37: | public class Chap0209 | |
38: | { | |
39: | public Chap0209() | |
40: | { | |
41: | Console.WriteLine("Chapter 2 example 9: generic tags"); | |
42: | ||
43: | // step 1: creation of a document-object | |
44: | Document document = new Document(); | |
45: | ||
46: | try | |
47: | { | |
48: | ||
49: | // step 2: | |
50: | // we create a writer that listens to the document | |
51: | // and directs a PDF-stream to a file | |
52: | PdfWriter writer = PdfWriter.GetInstance(document, new FileStream("Chap0209.pdf", FileMode.Create)); | |
53: | ||
54: | // step 3: we open the document | |
55: | document.Open(); | |
56: | ||
57: | // step 4: | |
58: | // we create an Event and Add it to the writer | |
59: | Glossary pageEvent = new Glossary(); | |
60: | writer.PageEvent = pageEvent; | |
61: | ||
62: | // we Add some content | |
63: | String[] f = new String[14]; | |
64: | f[0] = "Courier"; | |
65: | f[1] = "Courier Bold"; | |
66: | f[2] = "Courier Italic"; | |
67: | f[3] = "Courier Bold Italic"; | |
68: | f[4] = "Helvetica"; | |
69: | f[5] = "Helvetica bold"; | |
70: | f[6] = "Helvetica italic"; | |
71: | f[7] = "Helvetica bold italic"; | |
72: | f[8] = "Times New Roman"; | |
73: | f[9] = "Times New Roman bold"; | |
74: | f[10] = "Times New Roman italic"; | |
75: | f[11] = "Times New Roman bold italic"; | |
76: | f[12] = "Symbol"; | |
77: | f[13] = "Zapfdingbats"; | |
78: | Font[] fonts = new Font[14]; | |
79: | fonts[0] = FontFactory.GetFont(FontFactory.COURIER, 12, Font.NORMAL); | |
80: | fonts[1] = FontFactory.GetFont(FontFactory.COURIER, 12, Font.BOLD); | |
81: | fonts[2] = FontFactory.GetFont(FontFactory.COURIER, 12, Font.ITALIC); | |
82: | fonts[3] = FontFactory.GetFont(FontFactory.COURIER, 12, Font.BOLD | Font.ITALIC); | |
83: | fonts[4] = FontFactory.GetFont(FontFactory.HELVETICA, 12, Font.NORMAL); | |
84: | fonts[5] = FontFactory.GetFont(FontFactory.HELVETICA, 12, Font.BOLD); | |
85: | fonts[6] = FontFactory.GetFont(FontFactory.HELVETICA, 12, Font.ITALIC); | |
86: | fonts[7] = FontFactory.GetFont(FontFactory.HELVETICA, 12, Font.BOLD | Font.ITALIC); | |
87: | fonts[8] = FontFactory.GetFont(FontFactory.TIMES_ROMAN, 12, Font.NORMAL); | |
88: | fonts[9] = FontFactory.GetFont(FontFactory.TIMES_ROMAN, 12, Font.BOLD); | |
89: | fonts[10] = FontFactory.GetFont(FontFactory.TIMES_ROMAN, 12, Font.ITALIC); | |
90: | fonts[11] = FontFactory.GetFont(FontFactory.TIMES_ROMAN, 12, Font.BOLD | Font.ITALIC); | |
91: | fonts[12] = FontFactory.GetFont(FontFactory.SYMBOL, 12, Font.NORMAL); | |
92: | fonts[13] = FontFactory.GetFont(FontFactory.ZAPFDINGBATS, 12, Font.NORMAL); | |
93: | for (int i = 0; i < 14; i++) | |
94: | { | |
95: | Chunk chunk = new Chunk("This is font ", fonts[i]); | |
96: | Paragraph p = new Paragraph(chunk); | |
97: | p.Add(new Phrase(new Chunk(f[i], fonts[i]).SetGenericTag(f[i]))); | |
98: | document.Add(p); | |
99: | if (i % 4 == 3) | |
100: | { | |
101: | document.NewPage(); | |
102: | } | |
103: | } | |
104: | ||
105: | // we Add the glossary | |
106: | document.NewPage(); | |
107: | SortedList glossary = pageEvent.GetGlossary(); | |
108: | foreach(string key in glossary.Keys) | |
109: | { | |
110: | int page = (int)glossary[key]; | |
111: | Paragraph g = new Paragraph(key); | |
112: | g.Add(" : page "); | |
113: | g.Add(page.ToString()); | |
114: | document.Add(g); | |
115: | } | |
116: | ||
117: | } | |
118: | catch(DocumentException de) | |
119: | { | |
120: | Console.Error.WriteLine(de.Message); | |
121: | } | |
122: | catch(IOException ioe) | |
123: | { | |
124: | Console.Error.WriteLine(ioe.Message); | |
125: | } | |
126: | ||
127: | // step 5: we close the document | |
128: | document.Close(); | |
129: | ||
130: | } | |
131: | } | |
132: | } |