Hello Bruno!

I came across severe problems when cellspacing is used with tables and the
table has to be continued on next page. After some time of debugging I have
been able to correct those problems. Here is the corrected code, again in
PdfDocument.add(Element) for the TABLE case (please add to CVS and next
version). When you run the example sent in one of my previous mails now you
can use cellspacing or not, use header cells or not .. in all cases table
gets rendered properly!

One bug remains (as already stated in a previous mail):
If cells have to show multiple lines and get split onto 2 pages then those
cells which dont have text on second page wont draw the graphics also .. and
therefore missing borders/background.
Any hint on how that splitting is done? How can I debug that, where and what
should I watch?

       // new page
       newPage();
       // G.F.: if something added in page event i.e. currentHeight > 0
       float heightCorrection = 0;
       boolean somethingAdded = false;
       if (currentHeight > 0)
       {
        heightCorrection = 6;
        currentHeight += heightCorrection;
        somethingAdded = true;
        newLine();
        flushLines();
        indentTop = currentHeight - leading;
        currentHeight = 0;
       }
       else
       {
        flushLines();
       }

       // this part repeats the table headers (if any)
       headercells = table.getHeaderCells();
       int size = headercells.size();
       if (size > 0)
       {
        // G.F. below use top(0)/bottom(0) so cellspacing does not influence
        // this is the top of the headersection
        cell = (PdfCell) headercells.get(0);
        float oldTop = cell.top(0);
        // loop over all the cells of the table header
        for (int i = 0; i < size; i++)
        {
         cell = (PdfCell) headercells.get(i);
         // calculation of the new cellpositions
         cell.setTop(indentTop() - oldTop + cell.top(0));
         cell.setBottom(indentTop() - oldTop + cell.bottom(0));
         pagetop = cell.bottom();
         // we paint the borders of the cell
         cellGraphics.rectangle(cell.rectangle(indentTop(),
indentBottom()));
         // we write the text of the cell
         ArrayList images = cell.getImages(indentTop(), indentBottom());
         for (Iterator im = images.iterator(); im.hasNext();)
         {
          cellsShown = true;
          Image image = (Image) im.next();
          addImage(graphics, image, 0, 0, 0, 0, 0, 0);
         }
         lines = cell.getLines(indentTop(), indentBottom());
         float cellTop = cell.top(indentTop());
         text.moveText(0, cellTop-heightCorrection);
         cellDisplacement = flushLines() - cellTop+heightCorrection;
         text.moveText(0, cellDisplacement);
        }

        currentHeight = indentTop() - pagetop + table.cellspacing();
        text.moveText(0, pagetop - indentTop() - table.cellspacing() -
currentHeight);
       }
       // G.F.: in case no header files but something added in page event
       else
       {
        if (somethingAdded)
        {
         pagetop = indentTop();
         text.moveText(0, pagetop - indentTop() - table.cellspacing());
        }
       }
       // G.F. take care of cellspacing
       oldHeight = currentHeight+ table.cellspacing()-heightCorrection;

       // calculating the new positions of the table and the cells
       size = Math.min(cells.size(), table.columns());
       int i = 0;
       while (i < size)
       {
        cell = (PdfCell) cells.get(i);
        if (cell.top(-table.cellspacing()) > lostTableBottom)
        {
         float newBottom = pagetop - difference + cell.bottom();
         float neededHeight = cell.remainingHeight();
         if (newBottom > pagetop - neededHeight)
         {
          difference += newBottom - (pagetop - neededHeight);
         }
        }
        i++;
       }
       size = cells.size();
       table.setTop(indentTop());
       table.setBottom(pagetop - difference +
table.bottom(table.cellspacing()));
       for (i = 0; i < size; i++)
       {
        cell = (PdfCell) cells.get(i);
        float newBottom = pagetop - difference + cell.bottom();
        // G.F. take care of proper cellspacing
        float newTop = pagetop - difference +
cell.top(-table.cellspacing());
        if (newTop > indentTop() - currentHeight + table.cellspacing())
        {
         newTop = indentTop() - currentHeight + table.cellspacing();
        }
        cell.setTop(newTop - table.cellspacing());
        cell.setBottom(newBottom - table.cellspacing());
       }

Kind regards,
Gerald.

----- Original Message -----
From: "Gerald Fehringer" <[EMAIL PROTECTED]>
To: <[EMAIL PROTECTED]>
Sent: Wednesday, October 02, 2002 9:36 AM
Subject: Re: [iText-questions] Table as header ?


> Hello Bruno!
>
> My modification was only working with tables having header cells .. here
is
> a bug fix to have it working for tables without header cells as well
(again
> in the PdfDocument.add(Element) method):
>
>        // new page
>        newPage();
>        // Bug fix start Gerald Fehringer: if something added through page
> event
>        boolean somethingAdded = false;
>        if( currentHeight > 0 )
>        {
>         somethingAdded = true;
>         newLine();
>         flushLines();
>         indentTop = currentHeight - leading + 6;
>         currentHeight = 0;
>        }
>        else
>        {
>         flushLines();
>        }
>        // Bug fix end
>
>        // this part repeats the table headers (if any)
>        headercells = table.getHeaderCells();
>        int size = headercells.size();
>        if (size > 0) {
>         // this is the top of the headersection
>         cell = (PdfCell) headercells.get(0);
>         float oldTop = cell.top();
>         // loop over all the cells of the table header
>         for (int i = 0; i < size; i++) {
>          cell = (PdfCell) headercells.get(i);
>          // calculation of the new cellpositions
>          cell.setTop(indentTop()- oldTop + cell.top());
>          cell.setBottom(indentTop()- oldTop + cell.bottom() - 2f *
> table.cellspacing());
>          pagetop = cell.bottom();
>          // we paint the borders of the cell
>          cellGraphics.rectangle(cell.rectangle(indentTop(),
> indentBottom()));
>          // we write the text of the cell
>          ArrayList images = cell.getImages(indentTop(), indentBottom());
>          for (Iterator im = images.iterator(); im.hasNext(); ) {
>           cellsShown = true;
>           Image image = (Image) im.next();
>           addImage(graphics, image, 0, 0, 0, 0, 0, 0);
>          }
>          lines = cell.getLines(indentTop(), indentBottom());
>          float cellTop = cell.top(indentTop());
>          text.moveText(0, cellTop);
>          cellDisplacement = flushLines() - cellTop;
>          text.moveText(0, cellDisplacement);
>         }
>
>         currentHeight =  indentTop() - pagetop - table.cellspacing();
>         text.moveText(0, pagetop - indentTop() + table.cellspacing() -
> currentHeight);
>        }
>        // Bug fix start Gerald Fehringer: if no header cells and something
> added through page event
>        else
>        {
>         if( somethingAdded )
>         {
>          pagetop = indentTop();
>          text.moveText(0, pagetop - indentTop() + table.cellspacing());
>         }
>        }
>        // Bug fix end
>        oldHeight = currentHeight;
>
>
> Kind regards,
> Gerald.
>
> ----- Original Message -----
> From: "Gerald Fehringer" <[EMAIL PROTECTED]>
> To: <[EMAIL PROTECTED]>
> Sent: Tuesday, October 01, 2002 3:10 PM
> Subject: Re: [iText-questions] Table as header ?
>
>
> > I have managed to make my example working now :-)
> >
> > Have modified PdfDocument.add(Element) method for the case of TABLE ..
in
> > case a new page is triggered now I adjust the indentTop and
currentHeight
> > (if required i.e. currentHeight > 0, this is somewhat similar to when a
> > header object is added to the document):
> >
> >        // new page
> >        newPage();
> >        if( currentHeight > 0 )
> >        {
> >         newLine();
> >         flushLines();
> >         indentTop = currentHeight - leading + 6;
> >         currentHeight = 0;
> >        }
> >        else
> >        {
> >         flushLines();
> >        }
> >
> > Hope this change will be included in the next version, should help lots
of
> > people trying to add headers.
> >
> > Kind regards,
> > Gerald.
> >
> > PS: If you run the example sent in my previous mail you will notice some
> bug
> > with cell borders not being drawn if a cell contains multiple lines and
is
> > split onto 2 pages (see attached PDF).
> >
> > ----- Original Message -----
> > From: "Paulo Soares" <[EMAIL PROTECTED]>
> > To: "'Gerald Fehringer'" <[EMAIL PROTECTED]>;
> > <[EMAIL PROTECTED]>
> > Sent: Tuesday, October 01, 2002 12:29 PM
> > Subject: RE: [iText-questions] Table as header ?
> >
> >
> > > Page events only work with absolute positioned objects. You'll have to
> use
> > a
> > > PdfPTable.
> > >
> > > Best Regards,
> > > Paulo Soares
> > >
> > > > -----Original Message-----
> > > > From: Gerald Fehringer [SMTP:[EMAIL PROTECTED]]
> > > > Sent: Tuesday, October 01, 2002 11:17
> > > > To: [EMAIL PROTECTED]
> > > > Subject: Re: [iText-questions] Table as header ?
> > > >
> > > > I have done a small example which shows what I'd like to achieve ..
> > > > attached
> > > > you will find that example ..
> > > >
> > > > Using the MyPageEvent I'm adding a table on each onStartPage() to
the
> > > > document, this table represents the header of my document.
> > > > The main content of the document is another table which spans
multiple
> > > > pages.
> > > > The problem I have is the following: when starting the second page
my
> > > > header
> > > > table gets added and then the main data table continues to be drawn
> but
> > > > with
> > > > wrong coordinates .. (see attached PDF).
> > > >
> > > > What can I do?
> > > >
> > > > Kind regards,
> > > > Gerald.
> > > >
> > > > ----- Original Message -----
> > > > From: "Paulo Soares" <[EMAIL PROTECTED]>
> > > > To: "'Gerald Fehringer'" <[EMAIL PROTECTED]>;
> > > > <[EMAIL PROTECTED]>
> > > > Sent: Tuesday, October 01, 2002 11:58 AM
> > > > Subject: RE: [iText-questions] Table as header ?
> > > >
> > > >
> > > > > Unless you are using rowspan changing a Table into a PdfPTable is
> > > > trivial.
> > > > > If you have rowspan then it has to be converted to nested tables
and
> > the
> > > > > difficulty changes from trivial to easy. What's really your
problem?
> > > > >
> > > > > Best Regards,
> > > > > Paulo Soares
> > > > >
> > > > > > -----Original Message-----
> > > > > > From: Gerald Fehringer [SMTP:[EMAIL PROTECTED]]
> > > > > > Sent: Tuesday, October 01, 2002 8:54
> > > > > > To: [EMAIL PROTECTED]
> > > > > > Subject: Re: [iText-questions] Table as header ?
> > > > > >
> > > > > > Seems to be a difficult question :-)
> > > > > >
> > > > > > Any thoughts as yet?
> > > > > > Any hint on what I could do?
> > > > > >
> > > > > > Regards,
> > > > > > Gerald.
> > > > > >
> > > > > > ----- Original Message -----
> > > > > > From: "Gerald Fehringer" <[EMAIL PROTECTED]>
> > > > > > To: <[EMAIL PROTECTED]>
> > > > > > Sent: Monday, September 30, 2002 8:06 AM
> > > > > > Subject: [iText-questions] Table as header ?
> > > > > >
> > > > > >
> > > > > > > Hello!
> > > > > > >
> > > > > > >  I have an application which makes use of Tables
> > > > > > (com.lowagie.text.Table).
> > > > > > > It
> > > > > > >  get's filled with Chunks, Phrases, Images, .. dynamically.
All
> > this
> > > > > > works
> > > > > > >  fine.
> > > > > > >
> > > > > > >  Now there is need to add headers/footers to the documents,
but
> > > > these
> > > > > > can
> > > > > > be
> > > > > > >  quite complex. So my idea is to reuse the code already
written
> > for
> > > > > > creating
> > > > > > >  Tables dynamically and then add such a table as
header/footer.
> > > > > > >
> > > > > > >  From what I know I can use PageEvents to add headers/footers
to
> > the
> > > > > > >  individual pages, there adding PdfPTables (or text or
> templates)
> > at
> > > > > > > absolute
> > > > > > >  positions. But the problem I have is that I dont have a
> PdfPTable
> > > > but
> > > > a
> > > > > > >  Table ..
> > > > > > >
> > > > > > >  Therefore here my question:
> > > > > > >  Is it possible to somehow take my Table and transform it to a
> > > > > > PdfPTable?
> > > > > > Or
> > > > > > >  can I write the Table to a template? Or to an image? Or
> anything
> > > > else
> > > > > > that
> > > > > > >  allows me to use Table for header/footer?
> > > > > > >
> > > > > > >
> > > > > > >  Thanks for all your help,
> > > > > > >  Gerald.
> > > > > > >
> > > > > > >
> > > > > > >
> > > > > > > -------------------------------------------------------
> > > > > > > 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
> > > > > >
> > > > > >
> > > > > >
> > > > > > -------------------------------------------------------
> > > > > > This sf.net email is sponsored by: DEDICATED SERVERS only $89!
> > > > > > Linux or FreeBSD, FREE setup, FAST network. Get your own server
> > > > > > today at http://www.ServePath.com/indexfm.htm
> > > > > > _______________________________________________
> > > > > > iText-questions mailing list
> > > > > > [EMAIL PROTECTED]
> > > > > > https://lists.sourceforge.net/lists/listinfo/itext-questions <<
> > File:
> > > > tables.pdf >>  << File: com.zip >>
> >
>
>
>
> -------------------------------------------------------
> 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



-------------------------------------------------------
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