/*
 * $Id: Chap1107.java,v 1.4 2002/02/28 09:08:58 blowagie Exp $
 * $Name:  $
 *
 * This code is free software. It may only be copied or modified
 * if you include the following copyright notice:
 *
 * --> Copyright 2001 by Bruno Lowagie <--
 *
 * This code is part of the 'iText Tutorial'.
 * You can find the complete tutorial at the following address:
 * http://www.lowagie.com/iText/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@lowagie.com
 */

import java.io.FileOutputStream;
import java.io.IOException;

import com.lowagie.text.*;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.pdf.PdfWriter;
import com.lowagie.text.pdf.PdfContentByte;
import com.lowagie.text.pdf.PdfDestination;
import com.lowagie.text.pdf.PdfOutline;
import com.lowagie.text.pdf.PdfTemplate;

public class TestOutLines {

    public static void main(String[] args) {

        System.out.println("Chapter 11 example 7: Outlines and Destinations");

        // step 1: creation of a document-object
        Document document = new Document();

        try {

            // 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 FileOutputStream("TestOutLines.pdf"));

            // step 3: we open the document
            document.open();
            document.newPage();

            // step 4: we grab the ContentByte and do some stuff with it
            PdfContentByte cb = writer.getDirectContent();

			            // we create a PdfTemplate
			            PdfTemplate template = cb.createTemplate(25, 25);

			            // we add some crosses to visualize the destinations
			            template.moveTo(13, 0);
			            template.lineTo(13, 25);
			            template.moveTo(0, 13);
			            template.lineTo(50, 13);
			            template.stroke();

			            // we add the template on different positions
			            cb.addTemplate(template, 287, 787);
			            cb.addTemplate(template, 187, 487);
			            cb.addTemplate(template, 487, 287);
			            cb.addTemplate(template, 87, 87);



            // we define the destinations
            PdfDestination d0 = new PdfDestination(PdfDestination.FIT);
            PdfDestination d1 = new PdfDestination(PdfDestination.FIT);
            PdfDestination d2 = new PdfDestination(PdfDestination.FIT);
            PdfDestination d3 = new PdfDestination(PdfDestination.FIT);
            PdfDestination d4 = new PdfDestination(PdfDestination.FIT);
            PdfDestination d5 = new PdfDestination(PdfDestination.FIT);
            PdfDestination dummyD = new PdfDestination(PdfDestination.FIT);

            // we define the outlines
            PdfOutline outRoot = new PdfOutline(cb.getRootOutline(), d0, "root");
            PdfOutline out1 = new PdfOutline(outRoot, d1, "sub 1");
            PdfOutline out2 = new PdfOutline(outRoot, d2, "sub 2");
            PdfOutline out3 = new PdfOutline(outRoot, d3, "sub 3");
            PdfOutline out4 = new PdfOutline(outRoot, d4, "sub 4");

            cb.addOutline(outRoot);
            cb.addOutline(out1);
            cb.addOutline(out2);
            cb.addOutline(out3);
            cb.addOutline(out4);


            for ( int i = 0; i<20; i++) {
				int which = i % 4;

				if ( which == 0 )
					cb.addOutline(new PdfOutline( out1, dummyD, "" + i ));
			    else if ( which == 1 )
					cb.addOutline(new PdfOutline( out2, dummyD, "" + i ));
			    else if ( which == 2 )
					cb.addOutline(new PdfOutline( out3, dummyD, "" + i ));
			    else if ( which == 3 )
					cb.addOutline(new PdfOutline( out4, dummyD, "" + i ));

			}



        }
        catch(DocumentException de) {
            System.err.println(de.getMessage());
        }
        catch(IOException ioe) {
            System.err.println(ioe.getMessage());
        }

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

