Package com.lowagie.examples.objects.chunk

Source Code of com.lowagie.examples.objects.chunk.Glossary

/*
* $Id$
*
* This code is part of the 'iText Tutorial'.
* You can find the complete tutorial at the following address:
* http://itextdocs.lowagie.com/tutorial/
*
* 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.
*
* itext-questions@lists.sourceforge.net
*/

package com.lowagie.examples.objects.chunk;

import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Iterator;
import java.util.TreeMap;

import com.lowagie.text.Chunk;
import com.lowagie.text.LwgDocument;
import com.lowagie.text.DocumentException;
import com.lowagie.text.LwgFont;
import com.lowagie.text.FontFactory;
import com.lowagie.text.Paragraph;
import com.lowagie.text.LwgPhrase;
import com.lowagie.text.LwgRectangle;
import com.lowagie.text.pdf.PdfPageEventHelper;
import com.lowagie.text.pdf.PdfWriter;

/**
* Demonstrates the use of the Generic PageEvent.
*
* @author blowagie
*/

public class Glossary extends PdfPageEventHelper {
   
    /** keeps a glossary of words and the pages they appear on */
    public TreeMap glossary = new TreeMap();
   
    /**
     * All the text that is passed to this event, gets registered in the glossary.
     *
   * @see com.lowagie.text.pdf.PdfPageEventHelper#onGenericTag(com.lowagie.text.pdf.PdfWriter, com.lowagie.text.Document, com.lowagie.text.LwgRectangle, java.lang.String)
     */
    public void onGenericTag(PdfWriter writer, LwgDocument document, LwgRectangle rect, String text) {
        glossary.put(text, new Integer(writer.getPageNumber()));
    }
   
  /**
   * Generic page event.
   *
   * @param args no arguments needed here
   */
  public static void main(String[] args) {

    System.out.println("Glossary");

    // step 1: creation of a document-object
    LwgDocument document = new LwgDocument();
    try {
      // step 2:
      // we create a writer that listens to the document
      PdfWriter writer = PdfWriter.getInstance(document,
          new FileOutputStream("Glossary.pdf"));
      Glossary generic = new Glossary();
      writer.setPageEvent(generic);
     
      // step 3: we open the document
      document.open();
      // step 4:
            String[] f = new String[14];
            f[0] = "Courier";
            f[1] = "Courier Bold";
            f[2] = "Courier Italic";
            f[3] = "Courier Bold Italic";
            f[4] = "Helvetica";
            f[5] = "Helvetica bold";
            f[6] = "Helvetica italic";
            f[7] = "Helvetica bold italic";
            f[8] = "Times New Roman";
            f[9] = "Times New Roman bold";
            f[10] = "Times New Roman italic";
            f[11] = "Times New Roman bold italic";
            f[12] = "Symbol";
            f[13] = "Zapfdingbats";
            LwgFont[] fonts = new LwgFont[14];
            fonts[0] = FontFactory.getFont(FontFactory.COURIER, 12, LwgFont.NORMAL);
            fonts[1] = FontFactory.getFont(FontFactory.COURIER, 12, LwgFont.BOLD);
            fonts[2] = FontFactory.getFont(FontFactory.COURIER, 12, LwgFont.ITALIC);
            fonts[3] = FontFactory.getFont(FontFactory.COURIER, 12, LwgFont.BOLD | LwgFont.ITALIC);
            fonts[4] = FontFactory.getFont(FontFactory.HELVETICA, 12, LwgFont.NORMAL);
            fonts[5] = FontFactory.getFont(FontFactory.HELVETICA, 12, LwgFont.BOLD);
            fonts[6] = FontFactory.getFont(FontFactory.HELVETICA, 12, LwgFont.ITALIC);
            fonts[7] = FontFactory.getFont(FontFactory.HELVETICA, 12, LwgFont.BOLD | LwgFont.ITALIC);
            fonts[8] = FontFactory.getFont(FontFactory.TIMES_ROMAN, 12, LwgFont.NORMAL);
            fonts[9] = FontFactory.getFont(FontFactory.TIMES_ROMAN, 12, LwgFont.BOLD);
            fonts[10] = FontFactory.getFont(FontFactory.TIMES_ROMAN, 12, LwgFont.ITALIC);
            fonts[11] = FontFactory.getFont(FontFactory.TIMES_ROMAN, 12, LwgFont.BOLD | LwgFont.ITALIC);
            fonts[12] = FontFactory.getFont(FontFactory.SYMBOL, 12, LwgFont.NORMAL);
            fonts[13] = FontFactory.getFont(FontFactory.ZAPFDINGBATS, 12, LwgFont.NORMAL);
            for (int i = 0; i < 14; i++) {
                Chunk chunk = new Chunk("This is font ", fonts[i]);
                Paragraph p = new Paragraph(chunk);
                p.add(new LwgPhrase(new Chunk(f[i], fonts[i]).setGenericTag(f[i])));
                document.add(p);
                if (i % 4 == 3) {
                    document.newPage();
                }
            }
           
            // we add the glossary
            document.newPage();
            for (Iterator i = generic.glossary.keySet().iterator(); i.hasNext(); ) {
                String key = (String) i.next();
                int page = ((Integer) generic.glossary.get(key)).intValue();
                Paragraph g = new Paragraph(key);
                g.add(" : page ");
                g.add(String.valueOf(page));
                document.add(g);
            }
    } catch (DocumentException de) {
      System.err.println(de.getMessage());
    } catch (IOException ioe) {
      System.err.println(ioe.getMessage());
    }

    // step 5: we close the document
    document.close();
  }
}
TOP

Related Classes of com.lowagie.examples.objects.chunk.Glossary

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.