Package com.lowagie.examples.directcontent.pageevents

Source Code of com.lowagie.examples.directcontent.pageevents.EndPage

/*
* $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.directcontent.pageevents;

import java.io.FileOutputStream;

import com.lowagie.text.LwgDocument;
import com.lowagie.text.ExceptionConverter;
import com.lowagie.text.LwgPageSize;
import com.lowagie.text.Paragraph;
import com.lowagie.text.LwgRectangle;
import com.lowagie.text.pdf.LwgPdfPTable;
import com.lowagie.text.pdf.PdfPageEventHelper;
import com.lowagie.text.pdf.PdfWriter;

/**
* Demonstrates the use of PageEvents.
*/
public class EndPage extends PdfPageEventHelper {
   
    /**
     * Demonstrates the use of PageEvents.
     * @param args no arguments needed
     */
    public static void main(String[] args)
    {
        LwgDocument document = new LwgDocument(LwgPageSize.A4, 50, 50, 70, 70);
        try {
            PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("endpage.pdf"));
            writer.setPageEvent(new EndPage());
            document.open();
            String text = "Lots of text. ";
            for (int k = 0; k < 10; ++k)
                text += text;
            document.add(new Paragraph(text));
            document.close();
        }
        catch (Exception de) {
            de.printStackTrace();
        }
    }
   
    /**
     * @see com.lowagie.text.pdf.PdfPageEventHelper#onEndPage(com.lowagie.text.pdf.PdfWriter, com.lowagie.text.Document)
     */
    public void onEndPage(PdfWriter writer, LwgDocument document) {
        try {
            LwgRectangle page = document.getPageSize();
            LwgPdfPTable head = new LwgPdfPTable(3);
            for (int k = 1; k <= 6; ++k)
                head.add("head " + k);
            head.setTotalWidth(page.getWidth() - document.leftMargin() - document.rightMargin());
            head.writeSelectedRows(0, -1, document.leftMargin(), page.getHeight() - document.topMargin() + head.getTotalHeight(),
                writer.getDirectContent());
            LwgPdfPTable foot = new LwgPdfPTable(3);
            for (int k = 1; k <= 6; ++k)
                foot.add("foot " + k);
            foot.setTotalWidth(page.getWidth() - document.leftMargin() - document.rightMargin());
            foot.writeSelectedRows(0, -1, document.leftMargin(), document.bottomMargin(),
                writer.getDirectContent());
        }
        catch (Exception e) {
            throw new ExceptionConverter(e);
        }
    }

}
TOP

Related Classes of com.lowagie.examples.directcontent.pageevents.EndPage

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.