> Is there some way to keep a PdfPTable on the same page as long as it fits?
>
> I see the keepTogether property but it seems to have no effect.
>
> I don't want to split the table unless it will not fit on one page.
>
> thanks,
>
> Rick
>
>
> -------------------------------------------------------------------------
> 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
> [email protected]
> https://lists.sourceforge.net/lists/listinfo/itext-questions
> Buy the iText book: http://itext.ugent.be/itext-in-action/
>
Look at the writeTable() method

import com.lowagie.text.BadElementException;
import com.lowagie.text.DocumentException;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import com.lowagie.text.Cell;
import com.lowagie.text.Document;
import com.lowagie.text.ExceptionConverter;
import com.lowagie.text.PageSize;
import com.lowagie.text.Paragraph;
import com.lowagie.text.Rectangle;
import com.lowagie.text.pdf.PdfPTable;
import com.lowagie.text.pdf.PdfPCell;
import com.lowagie.text.Phrase;
import com.lowagie.text.Table;
import com.lowagie.text.pdf.PdfPageEventHelper;
import com.lowagie.text.pdf.PdfWriter;
import com.lowagie.text.Chunk;
import com.lowagie.text.Font;
//import com.lowagie.text.FontFactory;
import com.lowagie.text.Image;
import com.lowagie.text.Element;
import com.lowagie.text.pdf.PdfContentByte;

import com.lowagie.text.pdf.ColumnText;
import java.io.IOException;
import java.net.MalformedURLException;

/**
 *
 * @author Sean K Anderson - Data Virtue 2006
 */
public class PDFInvoice extends PDFReport {
    /** Creates a new instance of PDFReport */

    PDFInvoice ( String file, PdfPTable header) {
        super (file);

        System.out.println( header.getTotalHeight());

        ct.setSimpleColumn(36, 36, PageSize.LETTER.width() - 36,
PageSize.LETTER.height() - header.getTotalHeight()-16, 18,
Element.ALIGN_LEFT);
        this.setHeader(header);
    }



    public PdfPCell getCell (String text){

        return new PdfPCell ();


    }


    public void setHeader (PdfPTable pdft){

        head = pdft;
        head.setTotalWidth(PageSize.LETTER.width() - 72);
    }

    public void setBody (PdfPTable b){

        b.setTotalWidth(PageSize.LETTER.width()-72);
        body = b;


    }


    public void writeTable (PdfPTable b){


        //b.setSplitLate(true);
        //b.setSplitRows(true);
        float rowh = b.getRowHeight(1);

        float hf = head.getTotalHeight()+foot.getTotalHeight()+32;
        float bs = PageSize.LETTER.height() - hf;
        float chink = bs / rowh - 1;
        float to = chink;
        float rows = b.getTotalHeight() / rowh;

        float a = 0;


        do {

            b.writeSelectedRows((int)a,  (int)to, 36,
PageSize.LETTER.height()-head.getTotalHeight()-16,
             writer.getDirectContent());
            try {
                document.newPage();
            } catch (DocumentException ex) {
                ex.printStackTrace();
            }
            a += chink;
            to += chink;


        }while (a < rows);
        //
        //b.writeSelectedRows(0, -1, 36,
PageSize.LETTER.height()-head.getTotalHeight()-16,
           //  writer.getDirectContent());

    }


    public void setFooter (PdfPTable pdft){

        foot = pdft;
        foot.setTotalWidth(PageSize.LETTER.width() - 72);
    }


    public float howManyPages (PdfPTable pdfpt) {


        //pdfpt.setTotalWidth(PageSize.LETTER.width()-72);
        float th = pdfpt.getTotalHeight();
        System.out.println("Body Size "+ th);

        float hf = head.getTotalHeight()+foot.getTotalHeight();
        float bs = PageSize.LETTER.height() - hf;

        System.out.println(bs / th);

        return th / bs;

    }

    public void build () {

            pages = this.howManyPages(body);

            //float t = 1 - a;
            ///System.out.println("Remainder of pages % 1.0 = " + a);

            //if (a != 0  ) pages += a;
            if (pages <= 1) pages = 1;
            if (pages > 1){

                float a = pages % 1.0f;
                float r = 1 - a;
                pages += r;

            }

            System.out.println("PAGES: "+pages);


        writeTable (body);


    }


    /**
     *
     *
     *
     *
     **/

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

            if (water_mark){

                writeWatermark();

            }

            Rectangle page = doc.getPageSize();

             PdfPTable h = new PdfPTable(head);



            //System.out.println(h.getTotalHeight());



            h.writeSelectedRows(0, -1, 36, PageSize.LETTER.height()-16 ,
             writer.getDirectContent());

             //ct.setYLine(842 - 200);

            //page.height() - 36 + h.getTotalHeight()
            //ct.setYLine(page.height() - (h.getTotalHeight() + 36));
            //document.add(head);
            //PdfPTable foot = new PdfPTable(1);
           // for (int k = 1; k <= 6; ++k)

           /* 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.deleteLastRow();

            PdfPCell cell = new PdfPCell (new Phrase("Page: " +
writer.getPageNumber()+ " of " + (int)pages,
getDocFont(0,0)));

            cell.setHorizontalAlignment(Element.ALIGN_RIGHT);
                cell.setColspan(2);

            cell.setGrayFill(.8f);



            foot.addCell(cell);

            foot.writeSelectedRows(0, -1, document.leftMargin(),
document.bottomMargin()+foot.getTotalHeight(),
                writer.getDirectContent());


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


    }

   private float pages;
   private PdfPTable head;
   private PdfPTable body;
   private PdfPTable foot;
   //private boolean summary_written = false;


}



Sean - Data Virtue




-------------------------------------------------------------------------
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
[email protected]
https://lists.sourceforge.net/lists/listinfo/itext-questions
Buy the iText book: http://itext.ugent.be/itext-in-action/

Reply via email to