/* * This code is free software. It may only be copied or modified * if you include the following copyright notice: * * --> Copyright 2001 by Bruno Lowagie <-- * --> Copyright 2004 by Kazuya Ujihara <-- * * This code is example code of 'iText .NET'. * See http://www.ujihara.jp/iTextdotNET/examples.html * * This code is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. */ using System; using com.lowagie.text; using com.lowagie.text.pdf; using System.IO; using System.Collections; class Glossary : PdfPageEventHelper { // we will keep a glossary of words and the pages they appear on in a SortedList // keys will be sorted according to Thread.CurrentCulture. SortedList glossary = new SortedList(); // we override only the onGenericTag method public override void onGenericTag(PdfWriter writer, Document document, Rectangle rect, String text) { glossary[text] = writer.getPageNumber(); } // we add a method to retrieve the glossary public SortedList getGlossary() { return glossary; } } public class Chap0209 { public static void Main(String[] args) { Console.WriteLine("Chapter 2 example 9: generic tags"); // step 1: creation of a document-object Document document = new Document(); // step 2: // we create a writer that listens to the document // and directs a PDF-stream to a file PdfWriter writer = PdfWriter.getInstance(document, new FileStream("Chap0209.pdf", FileMode.Create)); // step 3: we open the document document.open(); // step 4: // we create an Event and add it to the writer Glossary pageEvent = new Glossary(); writer.setPageEvent(pageEvent); int i; // we add some content String[] f = new String[14]; i = 0; f[i++] = "Courier"; f[i++] = "Courier Bold"; f[i++] = "Courier Italic"; f[i++] = "Courier Bold Italic"; f[i++] = "Helvetica"; f[i++] = "Helvetica bold"; f[i++] = "Helvetica italic"; f[i++] = "Helvetica bold italic"; f[i++] = "Times New Roman"; f[i++] = "Times New Roman bold"; f[i++] = "Times New Roman italic"; f[i++] = "Times New Roman bold italic"; f[i++] = "Symbol"; f[i++] = "Zapfdingbats"; Font[] fonts = new Font[f.Length]; i = 0; fonts[i++] = FontFactory.getFont(FontFactory.COURIER, 12, Font.NORMAL); fonts[i++] = FontFactory.getFont(FontFactory.COURIER, 12, Font.BOLD); fonts[i++] = FontFactory.getFont(FontFactory.COURIER, 12, Font.ITALIC); fonts[i++] = FontFactory.getFont(FontFactory.COURIER, 12, Font.BOLD | Font.ITALIC); fonts[i++] = FontFactory.getFont(FontFactory.HELVETICA, 12, Font.NORMAL); fonts[i++] = FontFactory.getFont(FontFactory.HELVETICA, 12, Font.BOLD); fonts[i++] = FontFactory.getFont(FontFactory.HELVETICA, 12, Font.ITALIC); fonts[i++] = FontFactory.getFont(FontFactory.HELVETICA, 12, Font.BOLD | Font.ITALIC); fonts[i++] = FontFactory.getFont(FontFactory.TIMES_ROMAN, 12, Font.NORMAL); fonts[i++] = FontFactory.getFont(FontFactory.TIMES_ROMAN, 12, Font.BOLD); fonts[i++] = FontFactory.getFont(FontFactory.TIMES_ROMAN, 12, Font.ITALIC); fonts[i++] = FontFactory.getFont(FontFactory.TIMES_ROMAN, 12, Font.BOLD | Font.ITALIC); fonts[i++] = FontFactory.getFont(FontFactory.SYMBOL, 12, Font.NORMAL); fonts[i++] = FontFactory.getFont(FontFactory.ZAPFDINGBATS, 12, Font.NORMAL); for (i = 0; i < f.Length; i++) { Chunk chunk = new Chunk("This is font ", fonts[i]); Paragraph p = new Paragraph(chunk); p.add(new Phrase(new Chunk(f[i], fonts[i]).setGenericTag(f[i]))); document.add(p); if (i % 4 == 3) { document.newPage(); } } // we add the glossary document.newPage(); SortedList glossary = pageEvent.getGlossary(); for (i = 0; i < glossary.Count; i++) { string key = (string)glossary.GetKey(i); int page = (int)glossary.GetByIndex(i); Paragraph g = new Paragraph(key); g.add(" : page "); g.add(page.ToString()); document.add(g); } // step 5: we close the document document.close(); } }