Don,

I have created a TOC using templates. The sample that demonstrates it
follows. It has a few other unrelated inclusions that I was testing. Hope
that helps.

Regards
GS
------------ cut ------------------
import java.io.FileOutputStream;
import java.io.IOException;
import java.awt.Color;

import com.lowagie.text.*;
import com.lowagie.text.pdf.*;

class Test extends PdfPageEventHelper {
    
    private static String blah = "blah";
    static {
        for (int i=0; i<30; i++)
            blah += " blah";
    }

    private static final Paragraph blahblah = new Paragraph(blah + blah);
    private static final Paragraph blahblahblah = new Paragraph(blah + blah
+ blah);
    static {
        blahblahblah.setAlignment(Element.ALIGN_JUSTIFIED);
        blahblah.setAlignment(Element.ALIGN_JUSTIFIED);
    }

    // creation of the document with a certain size and certain margins
    private final Document document;
    // This is the contentbyte object of the writer
    private final PdfContentByte cb;

    //
    private final PdfWriter writer;

    // we will put the final number of pages in a template
    private final PdfTemplate totalPagesTemplate;

    // table to store templates for all chapters
    private final java.util.Hashtable toc = new java.util.Hashtable();

    // this is the BaseFont we are going to use for the header / footer
    private final BaseFont bf = BaseFont.createFont(BaseFont.HELVETICA,
BaseFont.CP1252, BaseFont.NOT_EMBEDDED);
    private final Font chapterFont =
FontFactory.getFont(FontFactory.HELVETICA, 24, Font.NORMAL, new Color(255,
0, 0));

    // Holds current position. Updated at the end of each paragraph
    private float currentY = 0;

    public Test() throws Exception {
        // creation of a document-object
        this.document = new Document(PageSize.A4, 50, 50, 50, 50);
        // create a writer that listens to the document
        this.writer = PdfWriter.getInstance(document, new
FileOutputStream("text.pdf"));
        // create add the event handler
        writer.setPageEvent(this);
        this.cb = writer.getDirectContent();
        this.totalPagesTemplate = cb.createTemplate(50, 50);
    }

    public static void main(String[] args) throws Exception {
        new Test().run();
    }

    // override the onEndPage method
    public void onEndPage(PdfWriter writer, Document document) {
        String text = "Page " + writer.getPageNumber() + " of ";
        float len = bf.getWidthPoint(text, 8);
        cb.beginText();
        cb.setFontAndSize(bf, 8);
        cb.setTextMatrix(280, 30);
        cb.showText(text);
        cb.endText();
        cb.addTemplate(totalPagesTemplate, 280 + len, 30);
    }

    // override the onParagraph method
    public void onParagraphEnd(PdfWriter writer, Document document, float
position) {
        currentY = position;
    }

    public void run() throws Exception {

           document.addAuthor("Powertel Limited");
           document.addSubject("This is the result of a Test.");

           // define a header
           HeaderFooter header = new HeaderFooter(new Phrase("This is a
header ", FontFactory.getFont(FontFactory.COURIER, 8, Font.NORMAL)), false);
           header.setBorder(Rectangle.NO_BORDER);
           header.setAlignment(Element.ALIGN_RIGHT);
           document.setHeader(header);

           // open the document for writing
           document.open();

           for (int i=0; i<10; i++) {
               document.add(blahblahblah);
               document.add(blahblah);
           }

           // Add TOC
           document.setPageSize(PageSize.A4.rotate());
           Paragraph cTitle = new Paragraph("This is TOC ", chapterFont);
           Chapter chapter = new Chapter(cTitle, 0);
           chapter.setNumberDepth(0);
           document.add(chapter);
            // this loop will create TOC for 7 chapters
           for (int i = 1; i < 8; i++) {
               String text = "Chapter " + i + " - page ";
               Chunk chunk = new Chunk(text).setLocalGoto("Chapter " + i);
               document.add(new Paragraph(chunk));
               float len = bf.getWidthPoint(text, 12);
               PdfTemplate template = cb.createTemplate(50, 50);
               toc.put(new Integer(i), template);
               cb.addTemplate(template, 50 + len, currentY);
           }

            // this loop will create 7 chapters
            document.setPageSize(PageSize.A4);
            for (int i = 1; i < 8; i++) {
                Chunk chunk = new Chunk("This is chapter " + i,
chapterFont).setLocalDestination("Chapter " + i); 
                cTitle = new Paragraph(chunk);
                chapter = new Chapter(cTitle, i);
                chapter.setNumberDepth(0);
                document.add(chapter);
                
                // Add page numbers to TOC
                PdfTemplate template = (PdfTemplate)(toc.get(new
Integer(i)));
                if (null != template) {
                    template.beginText();
                    template.setFontAndSize(bf, 12);
 
template.showText(String.valueOf(writer.getPageNumber()));
                    template.endText();
                }

                for (int j=0; j<i+1; j++) {
                    document.add(blahblahblah);
                    document.add(blahblah);
                }
            }

            // Add total pages
            totalPagesTemplate.beginText();
            totalPagesTemplate.setFontAndSize(bf, 8);
 
totalPagesTemplate.showText(String.valueOf(writer.getPageNumber()));
            totalPagesTemplate.endText();

            // close the document
            document.close();
    }
}
------------ cut ------------------

----- Original Message -----
From: Doak, Don <[EMAIL PROTECTED]> 
creating a Table of Contents   
2002-12-12 14:13

> Has anyone created a Table of Contents for a document they were writing
and
>  have it use the outline and page numbers?
>  
>  Thanks
>  DD



-------------------------------------------------------
This sf.net email is sponsored by:ThinkGeek
Welcome to geek heaven.
http://thinkgeek.com/sf
_______________________________________________
iText-questions mailing list
[EMAIL PROTECTED]
https://lists.sourceforge.net/lists/listinfo/itext-questions

Reply via email to