I have used and adapted the onEndPAge() example and everything works
nicely.  I have tried adding a a big fat table as my header and it doesn't
display right, it seems like it is reacting to my margins that I have set
for the ColumnText but when I change them it exhibits the same behavior.

Here is the code:


App Code (Clip)
---------------------------------------
PdfPTable head = new PdfPTable (2);
        try {

            head.addCell(Image.getInstance("images/DVLogo.jpg"));


        head.addCell(company);
        head.addCell(custTextArea.getText());
        head.addCell("Date: "+ dateField.getText()+ " Invoice # "+ num);

        PdfPTable itemHeader = new PdfPTable (7);
       // int headerwidths[] = { 10, 20, 40, 10, 5, 5, 10 };

        itemHeader.setWidthPercentage(100);
        //itemHeader.setWidths(headerwidths);

        itemHeader.addCell("Qty");
        itemHeader.addCell("Code");
        itemHeader.addCell("Description");
        itemHeader.addCell("Unit$");
        itemHeader.addCell("Tax1");
        itemHeader.addCell("Tax2");
        itemHeader.addCell("Ext. Total$");

        PdfPCell cell = new PdfPCell(itemHeader);
        cell.setColspan(1);

        head.addCell(cell);

        inv.setHeader(head);

------------------------------------------------
public class PDFReport extends PdfPageEventHelper{

    /** Creates a new instance of PDFReport */
    public PDFReport(String filename) {
        try {

            writer = PdfWriter.getInstance(document, new
FileOutputStream(filename));
            writer.setPageEvent(this);


            document.addAuthor("Data Virtue");
            document.addCreationDate();
            document.addSubject("Nevitium Report");
            document.open();

            cb = writer.getDirectContent();
            ct = new ColumnText(cb);
            ct.setSimpleColumn(36, 36, PageSize.A4.width() - 36,
PageSize.A4.height() - 36, 18, Element.ALIGN_LEFT);

        } catch (FileNotFoundException ex) {
            ex.printStackTrace();
        } catch (DocumentException ex) {
            ex.printStackTrace();
        }
    }


    public void addImage (String fileURL) {
        try {

            Image img = Image.getInstance(fileURL) ;

            try {

                document.add (img);


            } catch (DocumentException ex) {
                ex.printStackTrace();
            }

        } catch (BadElementException ex) {
            ex.printStackTrace();
        } catch (MalformedURLException ex) {
            ex.printStackTrace();
        } catch (IOException ex) {
            ex.printStackTrace();
        }


    }

    public void addVLine (float inches) {

        float length = inches * 72;




    }

    public void addParagraph (Paragraph p, boolean keepTogether) {

        //not in use
        try {

            p.setKeepTogether(keepTogether);
            document.add(p);

        } catch (DocumentException ex) {
            ex.printStackTrace();
        }


    }

    public void addParagraph (String text, boolean keepTogether) {

        if (store_text) raw_text += text;


        try {

            System.out.println (text);

            Paragraph p = new Paragraph(text, new Font(doc_font,
font_size, font_style) );

            //p.setKeepTogether(keepTogether);

            ColumnText tst = ColumnText.duplicate(ct);


            tst.addElement(p);

            pos = ct.getYLine();
            //System.out.println (pos);




            status = tst.go(true);


            if (!ColumnText.hasMoreText(status)) {
                ct.addElement(p);
                ct.setYLine(pos);
                ct.go(false);
            }
            else {
                document.newPage();
                ct.addElement(p);
                ct.setYLine(PageSize.A4.height() - 36);
                ct.go();
            }



        } catch (DocumentException ex) {
            ex.printStackTrace();
        }



    }


    public void newPage () {

        try {

            document.newPage();

        } catch (DocumentException ex) {
            ex.printStackTrace();
        }

    }

    public void finish () {

        document.close();
        //document = null;
    }

    /**
     *Convenience method to return all the text passed into the report
     *
     */


    public Chunk getChunk (String text, int size_adj){
        int fs = font_size;
        int fstyle = font_style;

        if (size_adj > 10 ) fstyle = Font.BOLD;
        else if (size_adj > 0 || 2 * size_adj < 0) fs += size_adj;

        return new Chunk (text, new Font (doc_font, fs, fstyle));


    }


    public Phrase getPhrase (String text){

        return new Phrase (0, text, new Font (doc_font, font_size,
font_style) );



    }


    public String getRawText () {


        return raw_text;

    }

   public float getPageWidth () {

       Rectangle r = document.getPageSize();
       return r.width();


   }


   public void setFont (String font) {

        if (font.equalsIgnoreCase("roman")) doc_font = Font.TIMES_ROMAN;
        if (font.equalsIgnoreCase("courier")) doc_font = Font.COURIER;
        if (font.equalsIgnoreCase("helv")) doc_font = Font.HELVETICA;
        if (font.equalsIgnoreCase("zapf")) doc_font = Font.ZAPFDINGBATS;
        if (font.equalsIgnoreCase("symbol")) doc_font = Font.SYMBOL;


    }

    public void setFontStyle (String style) {

        if (style.equalsIgnoreCase("normal")) font_style = Font.NORMAL;
        if (style.equalsIgnoreCase("bold")) font_style = Font.BOLD;
        if (style.equalsIgnoreCase("italic")) font_style = Font.ITALIC;
        if (style.equalsIgnoreCase("underline")) font_style = Font.UNDERLINE;
        if (style.equalsIgnoreCase("strike")) font_style = Font.STRIKETHRU;

    }

    public void setFontSize (int a) {

        font_size = a;


    }


    public void setReportName (String name){

        report_name = name;


    }


    /**
     * Use this to toggle text storage so that all the
     * paragraphs passed in will not be compiled into one string
     */
    public void setStoreText (boolean s){

        store_text = s;

    }

    public void onEndPage(PdfWriter writer, Document document) {
       // I left this empty because it is overridden below
    }

    public void setWatermark (String file){

        watermark_file = file;
        setWatermarkEnabled(true);

    }

    public void setWatermarkEnabled (boolean tf){

        water_mark = tf;

    }

    public void setReportTitle (String title){

        report_name = title;

    }

    public void blastText () {


        System.out.println(raw_text);

    }

    protected Document document = new Document(PageSize.A4);
    protected ColumnText ct;
    protected PdfContentByte cb;
    protected PdfWriter writer;

    protected float pos;

    protected int status = ColumnText.START_COLUMN;


    protected String report_name = "Data Virtue Report ";

    protected String raw_text = "";

    protected int doc_font = Font.COURIER;
    protected int font_size = 10;
    protected int font_style = Font.NORMAL;

    protected boolean store_text = true;
    protected boolean water_mark = false;

    protected String watermark_file;

}

----------------------------------------------

public class PDFInvoice extends PDFReport {
    /** Creates a new instance of PDFReport */

    PDFInvoice ( String file) {
        super (file);

    }


    public void setHeader (PdfPTable pdft){

        head = pdft;

    }

    public void setSummary (PdfPTable pdft){

        summary = pdft;

    }


    /** Call close() after using this method!*/
    public void writeSummary () {


        summary_written = true;

    }


    public void onEndPage(PdfWriter writer, Document doc) {
        try {

            Rectangle page = doc.getPageSize();

             PdfPTable h = new PdfPTable(head);

            h.setTotalWidth(page.width() - 72);

            h.writeSelectedRows(0, -1, doc.leftMargin(), page.height() -
doc.topMargin() + h.getTotalHeight(),
             writer.getDirectContent());


                       PdfPTable foot = new PdfPTable(1);

            if (!summary_written){
            PdfPCell footerCell = new PdfPCell (new Phrase ("Page: " +
document.getPageNumber()+ "  Continued...", new Font
(doc_font, font_size+2, font_style)));
            footerCell.setBorder(Rectangle.BOX);

            footerCell.setHorizontalAlignment(Element.ALIGN_RIGHT);
            foot.addCell(footerCell);

            }else foot = summary;


            foot.setTotalWidth(page.width() - document.leftMargin() -
document.rightMargin());
            foot.writeSelectedRows(0, -1, document.leftMargin(),
document.bottomMargin(),
                writer.getDirectContent());




        }
        catch (Exception e) {
            throw new ExceptionConverter(e);
        }
    }

   private PdfPTable head;
   private PdfPTable summary = new PdfPTable (1);
   private boolean summary_written = false;

}


Thanks in advance.




-------------------------------------------------------------------------
Take Surveys. Earn Cash. Influence the Future of IT
Join SourceForge.net's Techsay panel and you'll get the chance to share your
opinions on IT & business topics through brief surveys - and earn cash
http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV
_______________________________________________
iText-questions mailing list
iText-questions@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/itext-questions
Buy the iText book: http://itext.ugent.be/itext-in-action/

Reply via email to