| 1: | using System; | |
| 2: | using System.IO; | |
| 3: | using System.Collections; | |
| 4: | using System.util; | |
| 5: | ||
| 6: | using iTextSharp.text; | |
| 7: | using iTextSharp.text.pdf; | |
| 8: | using iTextSharp.text.markup; | |
| 9: | using iTextSharp.text.xml; | |
| 10: | ||
| 11: | namespace iTextSharp.tutorial.Chap12 | |
| 12: | { | |
| 13: | ||
| 14: | /** | |
| 15: | * We want to change the behaviour of the handler in some cases | |
| 16: | */ | |
| 17: | ||
| 18: | class MyHandler : ITextmyHandler | |
| 19: | { | |
| 20: | ||
| 21: | /** | |
| 22: | * We have to override the constructor | |
| 23: | */ | |
| 24: | ||
| 25: | public MyHandler(Document document, Hashtable tagmap) : base(document, tagmap) {} | |
| 26: | ||
| 27: | /** | |
| 28: | * We only alter the handling of some endtags. | |
| 29: | */ | |
| 30: | ||
| 31: | public override void EndElement(String uri, String lname, String name) | |
| 32: | { | |
| 33: | if (myTags.ContainsKey(name)) | |
| 34: | { | |
| 35: | XmlPeer peer = (XmlPeer) myTags[name]; | |
| 36: | // we don't want the document to be close | |
| 37: | // because we are going to add a page after the xml is parsed | |
| 38: | ||
| 39: | if (IsDocumentRoot(peer.Tag)) | |
| 40: | { | |
| 41: | return; | |
| 42: | } | |
| 43: | HandleEndingTags(peer.Tag); | |
| 44: | // we want to add a paragraph after the speaker chunk | |
| 45: | if ("SPEAKER".Equals(name)) | |
| 46: | { | |
| 47: | try | |
| 48: | { | |
| 49: | ITextElementArray previous = (ITextElementArray) stack.Pop(); | |
| 50: | previous.Add(new Paragraph(16)); | |
| 51: | stack.Push(previous); | |
| 52: | } | |
| 53: | catch | |
| 54: | { | |
| 55: | } | |
| 56: | } | |
| 57: | } | |
| 58: | else | |
| 59: | { | |
| 60: | HandleEndingTags(name); | |
| 61: | } | |
| 62: | } | |
| 63: | } | |
| 64: | ||
| 65: | /** | |
| 66: | * We use the tagfile from chapter 7, but we want to change some tag definitions. | |
| 67: | */ | |
| 68: | ||
| 69: | class MyMap : TagMap | |
| 70: | { | |
| 71: | ||
| 72: | public MyMap(String tagfile) : base(tagfile) | |
| 73: | { | |
| 74: | XmlPeer peer = new XmlPeer(MarkupTags.CSS_KEY_FONTSIZE, "10"); | |
| 75: | //new XmlPeer(ElementTags.CHUNK, "SPEAKER");ظSPEAKER | |
| 76: | //peer.AddValue(MarkupTags.CSS_KEY_FONTSIZE, "10"); | |
| 77: | peer.AddValue(MarkupTags.CSS_KEY_FONTWEIGHT, MarkupTags.CSS_VALUE_BOLD); | |
| 78: | peer.AddValue(ElementTags.GENERICTAG, ""); | |
| 79: | Add(peer.Alias, peer); | |
| 80: | } | |
| 81: | } | |
| 82: | ||
| 83: | /** | |
| 84: | * This object contains a speaker and a number of occurrances in the play | |
| 85: | */ | |
| 86: | ||
| 87: | class Speaker | |
| 88: | { | |
| 89: | ||
| 90: | // name of the speaker | |
| 91: | private String name; | |
| 92: | ||
| 93: | // number of occurrances | |
| 94: | private int occurrance = 1; | |
| 95: | ||
| 96: | public Speaker(String name) | |
| 97: | { | |
| 98: | this.name = name; | |
| 99: | } | |
| 100: | ||
| 101: | public String Name | |
| 102: | { | |
| 103: | get { return name; } | |
| 104: | } | |
| 105: | ||
| 106: | public int Occurrance | |
| 107: | { | |
| 108: | get { return occurrance; } | |
| 109: | set { occurrance = value; } | |
| 110: | } | |
| 111: | } | |
| 112: | ||
| 113: | /** | |
| 114: | * Your own page events. | |
| 115: | */ | |
| 116: | ||
| 117: | class MyPageEvents : PdfPageEventHelper | |
| 118: | { | |
| 119: | ||
| 120: | // we will keep a list of speakers | |
| 121: | SortedList speakers = new SortedList(); | |
| 122: | ||
| 123: | // This is the contentbyte object of the writer | |
| 124: | PdfContentByte cb; | |
| 125: | ||
| 126: | // we will put the final number of pages in a template | |
| 127: | PdfTemplate template; | |
| 128: | ||
| 129: | // this is the BaseFont we are going to use for the header / footer | |
| 130: | BaseFont bf = null; | |
| 131: | ||
| 132: | // this is the current act of the play | |
| 133: | String act = ""; | |
| 134: | ||
| 135: | // we override the onGenericTag method | |
| 136: | public override void OnGenericTag(PdfWriter writer, Document document, Rectangle rect, String text) | |
| 137: | { | |
| 138: | if (speakers.Contains(text)) | |
| 139: | { | |
| 140: | ((Speaker)speakers[text]).Occurrance++; | |
| 141: | } | |
| 142: | else | |
| 143: | { | |
| 144: | speakers.Add(text, new Speaker(text)); | |
| 145: | } | |
| 146: | } | |
| 147: | ||
| 148: | // we override the onOpenDocument method | |
| 149: | public override void OnOpenDocument(PdfWriter writer, Document document) | |
| 150: | { | |
| 151: | try | |
| 152: | { | |
| 153: | bf = BaseFont.CreateFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.NOT_EMBEDDED); | |
| 154: | cb = writer.DirectContent; | |
| 155: | template = cb.CreateTemplate(50, 50); | |
| 156: | } | |
| 157: | catch(DocumentException de) | |
| 158: | { | |
| 159: | } | |
| 160: | catch(IOException ioe) | |
| 161: | { | |
| 162: | } | |
| 163: | } | |
| 164: | ||
| 165: | // we override the onChapter method | |
| 166: | public override void OnChapter(PdfWriter writer,Document document,float paragraphPosition,Paragraph title) | |
| 167: | { | |
| 168: | System.Text.StringBuilder buf = new System.Text.StringBuilder(); | |
| 169: | foreach (Chunk chunk in title.Chunks) | |
| 170: | { | |
| 171: | buf.Append(chunk.Content); | |
| 172: | } | |
| 173: | act = buf.ToString(); | |
| 174: | } | |
| 175: | ||
| 176: | // we override the onEndPage method | |
| 177: | public override void OnEndPage(PdfWriter writer, Document document) | |
| 178: | { | |
| 179: | int pageN = writer.PageNumber; | |
| 180: | String text = "Page " + pageN + " of "; | |
| 181: | float len = bf.GetWidthPoint(text, 8); | |
| 182: | cb.BeginText(); | |
| 183: | cb.SetFontAndSize(bf, 8); | |
| 184: | cb.SetTextMatrix(280, 30); | |
| 185: | cb.ShowText(text); | |
| 186: | cb.EndText(); | |
| 187: | cb.AddTemplate(template, 280 + len, 30); | |
| 188: | cb.BeginText(); | |
| 189: | cb.SetFontAndSize(bf, 8); | |
| 190: | cb.SetTextMatrix(280, 820); | |
| 191: | if (pageN % 2 == 1) | |
| 192: | { | |
| 193: | cb.ShowText("Romeo and Juliet"); | |
| 194: | } | |
| 195: | else | |
| 196: | { | |
| 197: | cb.ShowText(act); | |
| 198: | } | |
| 199: | cb.EndText(); | |
| 200: | } | |
| 201: | ||
| 202: | // we override the onCloseDocument method | |
| 203: | public override void OnCloseDocument(PdfWriter writer, Document document) | |
| 204: | { | |
| 205: | template.BeginText(); | |
| 206: | template.SetFontAndSize(bf, 8); | |
| 207: | template.ShowText((writer.PageNumber - 1).ToString()); | |
| 208: | template.EndText(); | |
| 209: | } | |
| 210: | ||
| 211: | // we add a method to retrieve the glossary | |
| 212: | public SortedList Speakers | |
| 213: | { | |
| 214: | get | |
| 215: | { | |
| 216: | return speakers; | |
| 217: | } | |
| 218: | } | |
| 219: | } | |
| 220: | ||
| 221: | public class Chap1201 | |
| 222: | { | |
| 223: | ||
| 224: | public Chap1201() | |
| 225: | { | |
| 226: | ||
| 227: | Console.WriteLine("Chapter 12 example 1: page events"); | |
| 228: | ||
| 229: | // step 1: creation of a document-object | |
| 230: | Document document = new Document(PageSize.A4, 80, 50, 30, 65); | |
| 231: | ||
| 232: | try | |
| 233: | { | |
| 234: | // step 2: | |
| 235: | // we create a writer that listens to the document | |
| 236: | // and directs a XML-stream to a file | |
| 237: | PdfWriter writer = PdfWriter.GetInstance(document, new FileStream("Chap1201.pdf", FileMode.Create)); | |
| 238: | ||
| 239: | // create add the event handler | |
| 240: | MyPageEvents events = new MyPageEvents(); | |
| 241: | writer.PageEvent = events; | |
| 242: | ||
| 243: | // step 3: we create a parser and set the document handler | |
| 244: | MyHandler h = new MyHandler(document, new MyMap("tagmap0703.xml")); | |
| 245: | ||
| 246: | // step 4: we parse the document | |
| 247: | h.Parse("Chap0703.xml"); | |
| 248: | ||
| 249: | document.NewPage(); | |
| 250: | foreach (string key in events.Speakers.Keys) | |
| 251: | { | |
| 252: | Speaker speaker = (Speaker)events.Speakers[key]; | |
| 253: | document.Add(new Paragraph(speaker.Name + ": " + speaker.Occurrance + " speech blocks")); | |
| 254: | } | |
| 255: | document.Close(); | |
| 256: | ||
| 257: | } | |
| 258: | catch(Exception e) | |
| 259: | { | |
| 260: | Console.Error.WriteLine(e.Message); | |
| 261: | Console.Error.WriteLine(e.StackTrace); | |
| 262: | } | |
| 263: | } | |
| 264: | } | |
| 265: | } |