Here is a code sample.  Uncomment the static NUM_FILLER_ROWS at the top
of the class to see the different cases.  Also, notice my bookmark for
case 3 seems wrong.  It looks like getVerticalPosition is returning the
Y of page 1 where the table _would_ have been added if it could fit,
instead of the end of the table on page 2.

I need to create a table that has 2 "parts" (let's call them A and B).
"A" is a number of rows up to and including the first transaction on the
account (there may be 0 or more transactions).  "B" is the remaining
transactions.  So for example:

(A*)|SAVINGS ACCOUNT 12345  |
(A) |SAVINGS ACCOUNT SUMMARY|
(A*)|COLUMN HEADERS         |
(A) |BEGINNING BALANCE      |
(A) |FIRST TRANSACTION      |
(B) |TRAN 2                 |
(B) |TRAN 3                 |
(B) |TRAN N                 |
(B) |ENDING BALANCE         |

The business rules are:
1) all rows "A" have to appear on the same page
2) a bookmark (destination) needs to be created so users can "jump"
straight to "A"
3) TRAN rows should have an alternating background
4) if rows from "B" span multiple pages, rows A* should be printed as
headers rows

I've tried to build this a few different ways and haven't been totally
successful.  My best attempt was to use two tables (A and B).  Table A
is setKeepTogether(true) with no header rows and setSpaceAfter(0).
Table B has two header rows, setKeepTogether(false), and
skipFirstHeader(true).  The issue is when Table A takes you to the very
bottom of the current page, when you add Table B, it can't add the first
row, so it's forwarded to the next page, and the header is still
skipped.

I guess what I really want is a way to say "skip first header if added
to current page" concept.  

Can you provide some guidance on how to accomplish this?  Is there a way
to modify header rows on the fly in an on page start/end event?  

Does this even make sense.

Jason Berk
jb...@purduefed.com

This is a transmission from Purdue Federal Credit Union (Purdue Federal) and is 
intended solely for its authorized recipient(s), and may contain information 
that is confidential and or legally privileged. If you are not an addressee, or 
the employee or agent responsible for delivering it to an addressee, you are 
hereby notified that any use, dissemination, distribution, publication or 
copying of the information contained in this email is strictly prohibited. If 
you have received this transmission in error, please notify us by telephoning 
(765)497-3328 or returning the email. You are then instructed to delete the 
information from your computer. Thank you for your cooperation.

package test.pdf.common;

import com.itextpdf.text.Font;
import com.itextpdf.text.FontFactory;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;

public class Table extends PdfPTable {

        public static final Font BASE_FONT = FontFactory.getFont(
                        BaseFont.HELVETICA, 8);

        public Table(float[] relativeWidths) {
                super(relativeWidths);
                setDefaults();
        }

        public Table(int columns) {
                super(columns);
                setDefaults();
        }

        private void setDefaults() {
                setWidthPercentage(100);
                getDefaultCell().setPadding(3);
                getDefaultCell().setUseAscender(true);
                getDefaultCell().setBorder(Rectangle.BOX);
                getDefaultCell().setVerticalAlignment(ALIGN_BOTTOM);
        }

        public void addText(String text, Font font) {
                addCell(new Phrase(text, font));
        }

        public void addText(String text) {
                addText(text, BASE_FONT);
        }

        public void addNestedTable(PdfPTable table, int colSpan) {
                table.setWidthPercentage(100);
                PdfPCell cell = new PdfPCell(getDefaultCell());
                cell.setPadding(0);
                cell.setColspan(colSpan);
                cell.addElement(table);
                addCell(cell);
        }

        public void addNestedTable(PdfPTable table) {
                addNestedTable(table, getNumberOfColumns());
        }

}

package test.table;

import java.io.FileOutputStream;

import test.pdf.common.Table;

import com.itextpdf.text.BaseColor;
import com.itextpdf.text.Document;
import com.itextpdf.text.Element;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.PdfContentByte;
import com.itextpdf.text.pdf.PdfDestination;
import com.itextpdf.text.pdf.PdfOutline;
import com.itextpdf.text.pdf.PdfWriter;

public class Test {

        private static final String RESULTS = "/home/jberk/desktop/test.pdf";

        /*
         * this works - transaction #2 fit on page 1
         */
        // private static final int NUM_FILLER_ROWS = 16;

        /*
         * this is my issue - summary page fit on page 1, but 0 rows of the tran
         * table fit so it was forwarded to page 2, and the headers were NOT 
printed
         * because of skipFirstHeader(true)
         */
        // private static final int NUM_FILLER_ROWS = 17;

        /*
         * this works - the entire summary table is moved to page 2. This is 
pretty
         * much the same as the first case, just on page 2 instead of 1.  The 
bookmark
     * seems wrong, but I can live with that (as there would be no content 
between
     * the destination and the table...just white space
         */
         private static final int NUM_FILLER_ROWS = 18;

        private static final int NUM_TRANSACTION_ROWS = 150;

        private Document doc;
        private PdfWriter writer;
        private PdfContentByte canvas;

        public static void main(String[] args) {
                Test test = new Test();
                try {
                        test.go();
                        System.out.println("finished");
                } catch (Exception e) {
                        e.printStackTrace();
                }
        }

        private void go() throws Exception {
                createPdf();
                PdfDestination stmtTop = new PdfDestination(PdfDestination.XYZ, 
0,
                                doc.top(), 0);
                new PdfOutline(canvas.getRootOutline(), stmtTop, "START", 
false);
                addStatementSummary();
                addFiller();
                addSummaryTable();
                addTranTable();
                PdfDestination stmtBottom = new 
PdfDestination(PdfDestination.XYZ, 0,
                                doc.bottom(), 0);
                new PdfOutline(canvas.getRootOutline(), stmtBottom, "END", 
false);
                doc.close();
        }

        private void addFiller() throws Exception {
                Table t = new Table(new float[] { 1 });
                t.setSpacingAfter(9);
                for (int i = 1; i <= NUM_FILLER_ROWS; i++) {
                        t.addText("filler row " + i);
                }
                doc.add(t);
        }

        private void addTranTable() throws Exception {
                Table t = new Table(new float[] { 1, 5, 1 });
                t.setKeepTogether(false);
                t.setHeaderRows(2);
                t.setSkipFirstHeader(true);
                t.getDefaultCell().setBackgroundColor(BaseColor.LIGHT_GRAY);
                t.getDefaultCell().setColspan(t.getNumberOfColumns());
                t.addText("SAVINGS ACCOUNT 123456 CONTINUED");
                t.getDefaultCell().setColspan(1);
                // COLUMN HEADERS
                t.addText("DATE");
                t.addText("DESCRIPTION");
                t.addText("AMOUNT");
                t.getDefaultCell().setBackgroundColor(null);
                for (int numTrans = 2; numTrans <= NUM_TRANSACTION_ROWS; 
numTrans++) {
                        t.addText("MM-DD");
                        t.addText("TRANSACTION #" + numTrans);
                        t.addText("$100.00");
                }
                doc.add(t);
        }

        private void addSummaryTable() throws Exception {
                String tableName = "SAVINGS ACCOUNT 123456";
                Table t = new Table(new float[] { 1, 5, 1 });
                t.getDefaultCell().setColspan(t.getNumberOfColumns());
                t.getDefaultCell().setHorizontalAlignment(Element.ALIGN_CENTER);
                t.getDefaultCell().setVerticalAlignment(Element.ALIGN_MIDDLE);
                t.addText(tableName);
                // float minHeight = t.getDefaultCell().getMinimumHeight();
                t.getDefaultCell().setMinimumHeight(72);
                t.addText("ACCOUNT SUMMARY");
                t.getDefaultCell().setMinimumHeight(0);
                t.getDefaultCell().setColspan(1);
                t.getDefaultCell().setHorizontalAlignment(Element.ALIGN_LEFT);
                // COLUMN HEADERS
                t.addText("DATE");
                t.addText("DESCRIPTION");
                t.addText("AMOUNT");
                // BEGINNING BALANCE ROW
                t.addText("");
                t.addText("BEGINNING BALANCE");
                t.addText("$100.00");
                // FIRST REAL TRANSACTION
                t.addText("MM-DD");
                t.addText("TRANSACTION #1");
                t.addText("$100.00");
                t.setKeepTogether(true);
                PdfDestination destination = new 
PdfDestination(PdfDestination.XYZ, 0,
                                writer.getVerticalPosition(true) - 
t.getTotalHeight(), 0);
                new PdfOutline(canvas.getRootOutline(), destination, tableName, 
false);
                doc.add(t);
        }

        private void addStatementSummary() throws Exception {
                Table table = new Table(new float[] { 1, 1 });
                table.setWidthPercentage(100);
                table.setSpacingAfter(9);
                table.getDefaultCell().setBackgroundColor(BaseColor.LIGHT_GRAY);
                table.getDefaultCell().setBorder(Rectangle.BOX);
                table.getDefaultCell().setMinimumHeight(150);
                
table.getDefaultCell().setHorizontalAlignment(Element.ALIGN_CENTER);
                
table.getDefaultCell().setVerticalAlignment(Element.ALIGN_MIDDLE);
                table.addText("SHARES SUMMARY");
                table.addText("LOANS SUMMARY");
                doc.add(table);
        }

        private void createPdf() throws Exception {
                doc = new Document(PageSize.LETTER, 36, 36, 260, 45);
                writer = PdfWriter.getInstance(doc, new 
FileOutputStream(RESULTS));
                writer.setPdfVersion(PdfWriter.VERSION_1_6);
                writer.setViewerPreferences(PdfWriter.FitWindow
                                | PdfWriter.PageModeUseOutlines | 
PdfWriter.PrintScalingNone);
                doc.open();
                canvas = writer.getDirectContent();
                doc.setMargins(36, 36, 92, 36);
        }

}

------------------------------------------------------------------------------
Live Security Virtual Conference
Exclusive live event will cover all the ways today's security and 
threat landscape has changed and how IT managers can respond. Discussions 
will include endpoint security, mobile security and the latest in malware 
threats. http://www.accelacomm.com/jaw/sfrnl04242012/114/50122263/
_______________________________________________
iText-questions mailing list
iText-questions@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/itext-questions

iText(R) is a registered trademark of 1T3XT BVBA.
Many questions posted to this list can (and will) be answered with a reference 
to the iText book: http://www.itextpdf.com/book/
Please check the keywords list before you ask for examples: 
http://itextpdf.com/themes/keywords.php

Reply via email to