Page break in a table column for large contents

View: New views
1 Messages — Rating Filter:   Alert me  

Page break in a table column for large contents

by souvik_datta :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi,

I have created a pdf using itext. I have used PDFPTable and PdfPCell.
In a column of a table the content is too huge (more than 3 pages). So it is showing abrupt behaviour. The cell itself is going to a new page and automatic page break is happening to it.

I want the cell to to be in the same page and in case when the contents exceeds page size a page break.

Can anybody help me?  
 i am attaching the code
import java.awt.Color;
import java.io.FileOutputStream;
import java.util.zip.DataFormatException;

import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.Element;
import com.lowagie.text.Font;
import com.lowagie.text.FontFactory;
import com.lowagie.text.Image;
import com.lowagie.text.PageSize;
import com.lowagie.text.Paragraph;
import com.lowagie.text.Rectangle;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.pdf.PdfPCell;
import com.lowagie.text.pdf.PdfPTable;
import com.lowagie.text.pdf.PdfWriter;

public class PDFGeneration{
// PdfWriter writer = null;
// public PDFGeneration(PdfWriter writer)
// {
// this.writer = writer;
// }
       
        public static void main(String[] args) {
                        // step 1: creation of a document-object
                        Document document = new Document(PageSize.A4, 5, 50, 70, 70);
                        PdfPCell cell = null;
                        try {
                                String pdfTitle = "Quality_Assurance_Assessment_Report-200700803.pdf";
                                PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(pdfTitle));
                               
                                 writer.setPageEvent(new PageEvents());
// PdfPageEventHelper  eventHelper = new PdfPageEventHelper();
// eventHelper.onEndPage(writer, document);
                               
                               
// HeaderFooter footer = new HeaderFooter(new Phrase("Page - "), true);
// footer.setAlignment(Element.ALIGN_CENTER);
// footer.setBorderWidth(0);
// document.setFooter(footer);
                                        document.open();
                                        BaseFont arial = BaseFont.createFont("C:\\WINDOWS\\FONTS\\ARIAL.TTF", BaseFont.CP1252, BaseFont.NOT_EMBEDDED);
                                        float overAllwidths[] = {10f, 85f, 5f};
                                        PdfPTable overAlltable = new PdfPTable(3);
                                        overAlltable.setWidths(overAllwidths);
                                        overAlltable.setWidthPercentage(100);
                                       
                                       
                                        //Set NWA Logo
                                  Image NWALogo = Image.getInstance("nwa_black&white.bmp");
                                  NWALogo.setBorderWidthLeft(0);
                                  NWALogo.setBorderWidthTop(0);
                                  NWALogo.setBorderWidthBottom(0);
                                  NWALogo.setBorderWidthRight(0);
                                  NWALogo.scalePercent(60);
                                  cell = new PdfPCell(NWALogo);
                                  cell.setColspan(1);
                                  cell.setBorderWidthLeft(0);
                                  cell.setBorderWidthTop(0);
                                  cell.setBorderWidthBottom(0);
                                  cell.setBorderWidthRight(0);
                                  cell.setHorizontalAlignment(Element.ALIGN_CENTER);
                                  overAlltable.addCell(cell);
                                       
                                       
                                       
                                        //Header Part
                                        float widths[] = {100f};
                                        PdfPTable table = new PdfPTable(1);
                                       
                                        table.setWidths(widths);
                                        table.setWidthPercentage(100);
                               
                                                                               
                                        cell = getCellWithContent("COMMERCIAL OR FINANCIAL INFORMATION SUBMITTED IN CONFIDENCE", 10, Font.BOLD);
                                        table.addCell(cell);
                                       
                                        cell = getCellWithContent("OPERATIONAL SAFETY & QUALITY DIVISION", 21, Font.UNDERLINE);
                                        cell.setHorizontalAlignment(Element.ALIGN_CENTER);
                                        table.addCell(cell);
                                       
                                        table.addCell(putBlankCell(0, 1));
                                       
                                        cell = getCellWithContent("Quality Assurance", 8, Font.BOLD);
                                        cell.setHorizontalAlignment(Element.ALIGN_RIGHT);
                                        table.addCell(cell);
                                       
                                        //table.addCell(putBlankCellAtRight());
                                       
                                        table.addCell(putBlankCell(0, 1));
                                       

                                        cell = new PdfPCell(table);
                                        cell.setColspan(1);
                                        cell.setBorderWidthBottom(0);
                                        cell.setBorderWidthRight(0);
                                        cell.setBorderWidthLeft(0);
                                        cell.setBorderWidthTop(0);
                                        overAlltable.addCell(cell);
                                       
                                        cell = putBlankCell(0, 1);
                                        overAlltable.addCell(cell);
                                        //End of Header Part
                                       
                                        cell = putBlankCell(0, 1);
                                        overAlltable.addCell(cell);
                                       
                                        float adrsTabWidths[] = {50f, 50f};
                                        PdfPTable adrTable = new PdfPTable(2);
                                        adrTable.setWidths(adrsTabWidths);
                                        adrTable.setWidthPercentage(100f);
                                       
                                       
                                        //Inner Table For To Address
                                        float adrInnerWidths[] = {20f, 80f};
                                        PdfPTable adrInnerTable = new PdfPTable(2);
                                        adrInnerTable.setWidths(adrInnerWidths);
                                        adrInnerTable.setWidthPercentage(100f);
                                       
                                       
                                        cell = getCellWithContent("To:", 8, Font.NORMAL);
                                        cell.setHorizontalAlignment(Element.ALIGN_LEFT);
                                        adrInnerTable.addCell(cell);
                                       
                                        //Loop for To Addresses
                                        for(int cnt = 0; cnt < 3; cnt++)
                                        {
                                                if(cnt > 0)
                                                {
                                                        adrInnerTable.addCell(putBlankCell(0, 1));
                                                }
                                                cell = getCellWithContent("getFstNme()", 8, Font.NORMAL);
                                                adrInnerTable.addCell(cell);
                                                adrInnerTable.addCell(putBlankCell(0, 1));
                                                cell = getCellWithContent("getTitle()", 8, Font.NORMAL);
                                                adrInnerTable.addCell(cell);
                                                adrInnerTable.addCell(putBlankCell(0, 1));
                                                cell = getCellWithContent("getEml()", 8, Font.NORMAL);
                                                adrInnerTable.addCell(cell);
                                               
                                                adrInnerTable.addCell(putBlankCell(0, 2));
                                        }
                                       
                                        cell = new PdfPCell(adrInnerTable);
                                        cell.setBorderWidth(0);
                                       
                                        adrTable.addCell(cell);
                                        //End of To Segment
                                       
                                        //Inner Table For From
                                       
                                        adrInnerTable = new PdfPTable(2);
                                        float innerWidthsFrom[] = {30f, 70f};
                                        adrInnerTable.setWidths(innerWidthsFrom);
                                        adrInnerTable.setWidthPercentage(100f);
               
                                        cell = getCellWithContent("From:", 8, Font.NORMAL);
                                        cell.setHorizontalAlignment(Element.ALIGN_LEFT);
                                        adrInnerTable.addCell(cell);
       
                                        //Loop for To Addresses
                                        for(int cnt = 0; cnt < 1; cnt++)
                                        {
                                                if(cnt > 0)
                                                {
                                                        adrInnerTable.addCell(putBlankCell(0, 1));
                                                }
                                                cell = getCellWithContent("getFstNme()", 8, Font.NORMAL);
               
                                                adrInnerTable.addCell(cell);
               
                                                adrInnerTable.addCell(putBlankCell(0, 2));
                                        }
       
                                        //Date
                                        adrInnerTable.addCell(putBlankCell(0, 2));
                                        adrInnerTable.addCell(getCellWithContent("Date:", 8, Font.NORMAL));
                                        adrInnerTable.addCell(getCellWithContent("getDate()", 8, Font.NORMAL));
       
                                        cell = new PdfPCell(adrInnerTable);
                                        cell.setBorderWidth(0);
       
                                        adrTable.addCell(cell);
                                        //End of From Segment
                                                                                                       
                                        cell = new PdfPCell(adrTable);
                                        cell.setBorder(0);
                                        overAlltable.addCell(cell);
                                                                       
                                        cell = putBlankCell(0, 1);
                                        overAlltable.addCell(cell);
                                       
                                        overAlltable.addCell(putBlankCell(0, 3));
                                       
//CC Fields
                                        overAlltable.addCell(putBlankCell(0, 1));
                                       
                                        int noOfCols = 3;
                                        float genWidths[] = new float [1+noOfCols];
                                        genWidths[0] = 10f;
                                        for(int i = 1; i < 1+noOfCols; i++)
                                        {
                                                genWidths[i] = 90f/noOfCols;
                                        }
                                        adrTable = new PdfPTable(1+noOfCols);
                                        adrTable.setWidths(genWidths);
                                       
                                        cell = getCellWithContent("CC:", 8, Font.NORMAL);
                                        cell.setHorizontalAlignment(Element.ALIGN_LEFT);
                                        adrTable.addCell(cell);
                                        int i = 0;
                                        for(i = 0; i < 15; i++)
                                        {
                                                if(i%noOfCols == 0 && (i != 0))
                                                {
                                                        adrTable.addCell(putBlankCell(0, 1));
                                                        System.out.println("blank ");
                                                }
                                                adrTable.addCell(getCellWithContent("getCCAddress()"+i, 8, Font.NORMAL));
                                                System.out.println(i);
                                        }
                                        if(i%noOfCols > 0)
                                        {
                                                adrTable.addCell(putBlankCell(0, i%noOfCols));
                                        }
                                       
                                       
                                        cell = new PdfPCell(adrTable);
                                        cell.setBorderWidth(0);
                                        overAlltable.addCell(cell);
                                       
                                        overAlltable.addCell(putBlankCell(0, 1));
                                        overAlltable.addCell(putBlankCell(0, 3));
//End of CC Segments
//Assessment Segment
                                        overAlltable.addCell(putBlankCell(0, 1));
                                        table = new PdfPTable(3);
                                        float asmtWidth[] = {50f, 15f, 35f};
                                        table.setWidthPercentage(100f);
                                        table.setWidths(asmtWidth);
                                       
                                        table.addCell(putBlankCell(0, 1));
                                        cell = getCellWithContent("Assessment:", 8, Font.NORMAL);
                                        table.addCell(cell);
                                       
                                        cell = getCellWithContent("getAssmtNum()", 8, Font.NORMAL);
                                        table.addCell(cell);
                                       
                                        cell = new PdfPCell(table);
                                        cell.setBorderWidth(0);
                                        overAlltable.addCell(cell);
                                       
                                        overAlltable.addCell(putBlankCell(0, 1));
                                        overAlltable.addCell(putBlankCell(0, 3));
//End of Assmt Segment
//Subject Segment
  overAlltable.addCell(putBlankCell(0, 1));
                                        table = new PdfPTable(2);
                                        table.setWidthPercentage(100f);
                                        float genInnrWidth[] = {10f, 90f};
                                        table.setWidths(genInnrWidth);
                                        cell = getCellWithContent("Subject:", 8, Font.NORMAL);
                                        table.addCell(cell);
                                       
                                        cell = getCellWithContent("getSubject()", 8, Font.NORMAL);
                                        table.addCell(cell);
                                       
                                        cell = new PdfPCell(table);
                                        cell.setBorderWidth(0);
                                        overAlltable.addCell(cell);
                                       
                                        overAlltable.addCell(putBlankCell(0, 1));
//End of subject segment
                                       
// //Page Break
// overAlltable.addCell(putBlankCell(0, 3));
// document.add(overAlltable);
// document.newPage();
//
// overAlltable.deleteBodyRows();
// overAlltable.addCell(putBlankCell(0, 3));
                                       
                                        //////
                                       
//Assessment Coverage Segment  
  overAlltable.addCell(putBlankCell(0, 1));
                                        table = new PdfPTable(2);
                                        table.setWidthPercentage(100f);
                                        //float genInnrWidth[] = {10f, 90f};
                                        table.setWidths(genInnrWidth);
                                       
                                        cell = getCellWithContent("A:", 8, Font.BOLD);
                                        table.addCell(cell);
                                       
                                        cell = getCellWithContent("ASSESSMENT COVERAGE and AUTHORIZATION ", 8, Font.UNDERLINE);
                                        table.addCell(cell);
                                       
                                        cell = putBlankCell(0, 1);
                                        table.addCell(cell);
                                       
                                        String coverageAndAuthStr = "A Northwest Airlines (NWA) "+ "getRcpntOrg()"+" assessment was performed at "+"getRespOrgAddress()"+"\n\nThis assessment was under the authority of NWA Quality Program.\n\n";
                                        String coverageDesc = "ADJFGXGXGFGGXH XBKBGXG bKXGGG gdc GGG dggd GKGKg";
          cell = getCellWithContent(coverageAndAuthStr+coverageDesc, 8, Font.NORMAL);
                                        table.addCell(cell);
                                       
                                        table.addCell(putBlankCell(0, 2));
                                       
                                        cell = new PdfPCell(table);
                                        cell.setBorderWidth(0);
                                        overAlltable.addCell(cell);
                                       
                                        overAlltable.addCell(putBlankCell(0, 1));
                                        overAlltable.addCell(putBlankCell(0, 3));
//End of assessment coverage section
//Time Span Segment
  overAlltable.addCell(putBlankCell(0, 1));
                                        table = new PdfPTable(2);
                                        table.setWidthPercentage(100f);
                                        //float genInnrWidth[] = {10f, 90f};
                                        table.setWidths(genInnrWidth);
                                       
                                        cell = getCellWithContent("B:", 8, Font.BOLD);
                                        table.addCell(cell);
                                       
                                        cell = getCellWithContent("TIME SPAN OF ASSESSMENT", 8, Font.UNDERLINE);
                                        table.addCell(cell);
                                       
                                        cell = putBlankCell(0, 1);
                                        table.addCell(cell);
                                       
                                        cell = getCellWithContent("getTimeSpan()", 8, Font.NORMAL);
                                        table.addCell(cell);
                                       
                                        table.addCell(putBlankCell(0, 2));
                                       
                                        cell = new PdfPCell(table);
                                        cell.setBorderWidth(0);
                                        overAlltable.addCell(cell);
                                       
                                        overAlltable.addCell(putBlankCell(0, 1));
                                        overAlltable.addCell(putBlankCell(0, 3));
//End of time span segment
//Background of Segment
                                        overAlltable.addCell(putBlankCell(0, 1));
                                        table = new PdfPTable(2);
                                        table.setWidthPercentage(100f);
                                        //float genInnrWidth[] = {10f, 90f};
                                        table.setWidths(genInnrWidth);
                                        cell = getCellWithContent("C:", 8, Font.BOLD);
                                        table.addCell(cell);
                                       
                                        cell = getCellWithContent("BACKGROUND OF ASSESSMENT", 8, Font.UNDERLINE);
                                        table.addCell(cell);
                                       
                                        String desc = "Cellheights, -alignment, -padding, -leading, -colors:The content of a cell is wrapped by default. You can change this default with setNoWrap(true), but the result can look rather ugly, so be careful with this option.If you are using the default (nowrap = false), the height of the Cell is adjusted dynamically. Each cell in the same row has the height of the 'highest' cell. iText calculates this height based on different parameters: the number of lines of the content, the leading, the padding,... In some cases you don't want to give that kind of control to iText: you want a cell to have a fixed height. This can be done with the method setFixedHeight. Of course, if the content doesn't fit into this height, you will loose part of it. Maybe you want to do the opposite: maybe you want a cell to have a minimum height, even if it hasn't that much content. In that case, you'll have to use setMinimumHeight. Finally, there is another useful method setExtendLastRow. It extends the last row of a Table to the bottom of the page. Example: java com.lowagie.examples.objects.tables.CellHeights Playing with heights of PdfPCells: see CellHeights.pdf The following example demonstrates the self explaining methods setHorizontalAlignment and setVerticalAlignment. For the horizontal alignment, you can choose: * Element.ALIGN_LEFT * Element.ALIGN_CENTER  * Element.ALIGN_RIGHT    * Element.ALIGN_JUSTIFIED or the vertical alignment:   * Element.ALIGN_TOP  * Element.ALIGN_MIDDLE  * Element.ALIGN_BOTTOM  * Element.ALIGN_BASELINE Example: java com.lowagie.examples.objects.tables.CellAlignment  hanging the alignment of the contents of a PdfPCell: see Alignment.pdf he padding is the space between the content and the outer cellborder. You can set the amount of this space with setPadding, setPaddingTop, setPaddingRight, setPaddingLeft and/or setPaddingBottom. The leading is the space between the lines of the content. This value can be set with setLeading(float,%20float). xample: java com.lowagie.examples.objects.tables.CellPaddingLeading hanging the padding and the leading of the contents of a PdfPCell: see PaddingLeading.pdf f you set the padding, the borderwidth isn't taken into account. You can force iText to calculate the borderwidth into the padding with setUseBorderPadding(true). In the next example, some method to set colors and grayfill of borders and cells are demonstrated. As you can see, the padding of the green cell doesn't take the width of the magenta border into account. In the blue cell, the word 'blue' doesn't overlap with the cyan border. All methods that were used to change the borders and their color, were methods of the class Rectangle. Example: java com.lowagie.examples.objects.tables.CellColors Using some Rectangle methods on PdfPCell: see CellColors.pdf A last set of methods that influence the position of the content within a cell, are the methods setUseAscender and setUseDescender. An example is the best way to demonstrate the difference: Example: java com.lowagie.examples.objects.tables.TableBorders Demonstrates different borderstyles: see TableBorders.pdf In the example shown above, you also learn how to change the width and the color of cellborders. o to top of the page plitting tables over several pages: f you have really large tables, you can't expect them to fit on one page. iText splits the table automatically between rows. If your table has a header that needs to be repeated on every page, you need to tell the table how many rows the header counts. This can be done with the method setHeaderRows. In the example, there is one headerrow: xample: java com.lowagie.examples.objects.tables.AddBigTable  very big table added with document.add(): see AddBigTable.pdf n the previous example every row just took one line. If you have rows that are much larger, you must know that tables are split between rows by default. So if only part of a row fits on the current page, the complete row is passed to the next page (see SplitRowsBetween.pdf). Only if the row doesn't fit on 1 page it is split in two.  You can change this by using setSplitLate(false). In this case, the row is split immediately. As you can see in SplitRowsWithin.pdf the maximum size of each page is used. f you really don't want the rows to be split, you'll have to use setSplitRows(false), but in that case, you will lose a lot of data: see OmitRows.pdf. Example: java com.lowagie.examples.objects.tables.SplitRows Add a table to a PDF with document.add() and if the cell doesn't fit, the row is split in two parts: see SplitRowsBetween.pdf SplitRowsWithin.pdf OmitRows.pdf Cellheights, -alignment, -padding, -leading, -colors:The content of a cell is wrapped by default. You can change this default with setNoWrap(true), but the result can look rather ugly, so be careful with this option.If you are using the default (nowrap = false), the height of the Cell is adjusted dynamically. Each cell in the same row has the height of the 'highest' cell. iText calculates this height based on different parameters: the number of lines of the content, the leading, the padding,... In some cases you don't want to give that kind of control to iText: you want a cell to have a fixed height. This can be done with the method setFixedHeight. Of course, if the content doesn't fit into this height, you will loose part of it. Maybe you want to do the opposite: maybe you want a cell to have a minimum height, even if it hasn't that much content. In that case, you'll have to use setMinimumHeight. Finally, there is another useful method setExtendLastRow. It extends the last row of a Table to the bottom of the page. Example: java com.lowagie.examples.objects.tables.CellHeights Playing with heights of PdfPCells: see CellHeights.pdf The following example demonstrates the self explaining methods setHorizontalAlignment and setVerticalAlignment. For the horizontal alignment, you can choose: * Element.ALIGN_LEFT * Element.ALIGN_CENTER  * Element.ALIGN_RIGHT    * Element.ALIGN_JUSTIFIED or the vertical alignment:   * Element.ALIGN_TOP  * Element.ALIGN_MIDDLE  * Element.ALIGN_BOTTOM  * Element.ALIGN_BASELINE Example: java com.lowagie.examples.objects.tables.CellAlignment  hanging the alignment of the contents of a PdfPCell: see Alignment.pdf he padding is the space between the content and the outer cellborder. You can set the amount of this space with setPadding, setPaddingTop, setPaddingRight, setPaddingLeft and/or setPaddingBottom. The leading is the space between the lines of the content. This value can be set with setLeading(float,%20float). xample: java com.lowagie.examples.objects.tables.CellPaddingLeading hanging the padding and the leading of the contents of a PdfPCell: see PaddingLeading.pdf f you set the padding, the borderwidth isn't taken into account. You can force iText to calculate the borderwidth into the padding with setUseBorderPadding(true). In the next example, some method to set colors and grayfill of borders and cells are demonstrated. As you can see, the padding of the green cell doesn't take the width of the magenta border into account. In the blue cell, the word 'blue' doesn't overlap with the cyan border. All methods that were used to change the borders and their color, were methods of the class Rectangle. Example: java com.lowagie.examples.objects.tables.CellColors Using some Rectangle methods on PdfPCell: see CellColors.pdf A last set of methods that influence the position of the content within a cell, are the methods setUseAscender and setUseDescender. An example is the best way to demonstrate the difference: Example: java com.lowagie.examples.objects.tables.TableBorders Demonstrates different borderstyles: see TableBorders.pdf In the example shown above, you also learn how to change the width and the color of cellborders. o to top of the page plitting tables over several pages: f you have really large tables, you can't expect them to fit on one page. iText splits the table automatically between rows. If your table has a header that needs to be repeated on every page, you need to tell the table how many rows the header counts. This can be done with the method setHeaderRows. In the example, there is one headerrow: xample: java com.lowagie.examples.objects.tables.AddBigTable  very big table added with document.add(): see AddBigTable.pdf n the previous example every row just took one line. If you have rows that are much larger, you must know that tables are split between rows by default. So if only part of a row fits on the current page, the complete row is passed to the next page (see SplitRowsBetween.pdf). Only if the row doesn't fit on 1 page it is split in two.  You can change this by using setSplitLate(false). In this case, the row is split immediately. As you can see in SplitRowsWithin.pdf the maximum size of each page is used. f you really don't want the rows to be split, you'll have to use setSplitRows(false), but in that case, you will lose a lot of data: see OmitRows.pdf. Example: java com.lowagie.examples.objects.tables.SplitRows Add a table to a PDF with document.add() and if the cell doesn't fit, the row is split in two parts: see SplitRowsBetween.pdf SplitRowsWithin.pdf OmitRows.pdf Cellheights, -alignment, -padding, -leading, -colors:The content of a cell is wrapped by default. You can change this default with setNoWrap(true), but the result can look rather ugly, so be careful with this option.If you are using the default (nowrap = false), the height of the Cell is adjusted dynamically. Each cell in the same row has the height of the 'highest' cell. iText calculates this height based on different parameters: the number of lines of the content, the leading, the padding,... In some cases you don't want to give that kind of control to iText: you want a cell to have a fixed height. This can be done with the method setFixedHeight. Of course, if the content doesn't fit into this height, you will loose part of it. Maybe you want to do the opposite: maybe you want a cell to have a minimum height, even if it hasn't that much content. In that case, you'll have to use setMinimumHeight. Finally, there is another useful method setExtendLastRow. It extends the last row of a Table to the bottom of the page. Example: java com.lowagie.examples.objects.tables.CellHeights Playing with heights of PdfPCells: see CellHeights.pdf The following example demonstrates the self explaining methods setHorizontalAlignment and setVerticalAlignment. For the horizontal alignment, you can choose: * Element.ALIGN_LEFT * Element.ALIGN_CENTER  * Element.ALIGN_RIGHT    * Element.ALIGN_JUSTIFIED or the vertical alignment:   * Element.ALIGN_TOP  * Element.ALIGN_MIDDLE  * Element.ALIGN_BOTTOM  * Element.ALIGN_BASELINE Example: java com.lowagie.examples.objects.tables.CellAlignment  hanging the alignment of the contents of a PdfPCell: see Alignment.pdf he padding is the space between the content and the outer cellborder. You can set the amount of this space with setPadding, setPaddingTop, setPaddingRight, setPaddingLeft and/or setPaddingBottom. The leading is the space between the lines of the content. This value can be set with setLeading(float,%20float). xample: java com.lowagie.examples.objects.tables.CellPaddingLeading hanging the padding and the leading of the contents of a PdfPCell: see PaddingLeading.pdf f you set the padding, the borderwidth isn't taken into account. You can force iText to calculate the borderwidth into the padding with setUseBorderPadding(true). In the next example, some method to set colors and grayfill of borders and cells are demonstrated. As you can see, the padding of the green cell doesn't take the width of the magenta border into account. In the blue cell, the word 'blue' doesn't overlap with the cyan border. All methods that were used to change the borders and their color, were methods of the class Rectangle. Example: java com.lowagie.examples.objects.tables.CellColors Using some Rectangle methods on PdfPCell: see CellColors.pdf A last set of methods that influence the position of the content within a cell, are the methods setUseAscender and setUseDescender. An example is the best way to demonstrate the difference: Example: java com.lowagie.examples.objects.tables.TableBorders Demonstrates different borderstyles: see TableBorders.pdf In the example shown above, you also learn how to change the width and the color of cellborders. o to top of the page plitting tables over several pages: f you have really large tables, you can't expect them to fit on one page. iText splits the table automatically between rows. If your table has a header that needs to be repeated on every page, you need to tell the table how many rows the header counts. This can be done with the method setHeaderRows. In the example, there is one headerrow: xample: java com.lowagie.examples.objects.tables.AddBigTable  very big table added with document.add(): see AddBigTable.pdf n the previous example every row just took one line. If you have rows that are much larger, you must know that tables are split between rows by default. So if only part of a row fits on the current page, the complete row is passed to the next page (see SplitRowsBetween.pdf). Only if the row doesn't fit on 1 page it is split in two.  You can change this by using setSplitLate(false). In this case, the row is split immediately. As you can see in SplitRowsWithin.pdf the maximum size of each page is used. f you really don't want the rows to be split, you'll have to use setSplitRows(false), but in that case, you will lose a lot of data: see OmitRows.pdf. Example: java com.lowagie.examples.objects.tables.SplitRows Add a table to a PDF with document.add() and if the cell doesn't fit, the row is split in two parts: see SplitRowsBetween.pdf SplitRowsWithin.pdf OmitRows.pdf Cellheights, -alignment, -padding, -leading, -colors:The content of a cell is wrapped by default. You can change this default with setNoWrap(true), but the result can look rather ugly, so be careful with this option.If you are using the default (nowrap = false), the height of the Cell is adjusted dynamically. Each cell in the same row has the height of the 'highest' cell. iText calculates this height based on different parameters: the number of lines of the content, the leading, the padding,... In some cases you don't want to give that kind of control to iText: you want a cell to have a fixed height. This can be done with the method setFixedHeight. Of course, if the content doesn't fit into this height, you will loose part of it. Maybe you want to do the opposite: maybe you want a cell to have a minimum height, even if it hasn't that much content. In that case, you'll have to use setMinimumHeight. Finally, there is another useful method setExtendLastRow. It extends the last row of a Table to the bottom of the page. Example: java com.lowagie.examples.objects.tables.CellHeights Playing with heights of PdfPCells: see CellHeights.pdf The following example demonstrates the self explaining methods setHorizontalAlignment and setVerticalAlignment. For the horizontal alignment, you can choose: * Element.ALIGN_LEFT * Element.ALIGN_CENTER  * Element.ALIGN_RIGHT    * Element.ALIGN_JUSTIFIED or the vertical alignment:   * Element.ALIGN_TOP  * Element.ALIGN_MIDDLE  * Element.ALIGN_BOTTOM  * Element.ALIGN_BASELINE Example: java com.lowagie.examples.objects.tables.CellAlignment  hanging the alignment of the contents of a PdfPCell: see Alignment.pdf he padding is the space between the content and the outer cellborder. You can set the amount of this space with setPadding, setPaddingTop, setPaddingRight, setPaddingLeft and/or setPaddingBottom. The leading is the space between the lines of the content. This value can be set with setLeading(float,%20float). xample: java com.lowagie.examples.objects.tables.CellPaddingLeading hanging the padding and the leading of the contents of a PdfPCell: see PaddingLeading.pdf f you set the padding, the borderwidth isn't taken into account. You can force iText to calculate the borderwidth into the padding with setUseBorderPadding(true). In the next example, some method to set colors and grayfill of borders and cells are demonstrated. As you can see, the padding of the green cell doesn't take the width of the magenta border into account. In the blue cell, the word 'blue' doesn't overlap with the cyan border. All methods that were used to change the borders and their color, were methods of the class Rectangle. Example: java com.lowagie.examples.objects.tables.CellColors Using some Rectangle methods on PdfPCell: see CellColors.pdf A last set of methods that influence the position of the content within a cell, are the methods setUseAscender and setUseDescender. An example is the best way to demonstrate the difference: Example: java com.lowagie.examples.objects.tables.TableBorders Demonstrates different borderstyles: see TableBorders.pdf In the example shown above, you also learn how to change the width and the color of cellborders. o to top of the page plitting tables over several pages: f you have really large tables, you can't expect them to fit on one page. iText splits the table automatically between rows. If your table has a header that needs to be repeated on every page, you need to tell the table how many rows the header counts. This can be done with the method setHeaderRows. In the example, there is one headerrow: xample: java com.lowagie.examples.objects.tables.AddBigTable  very big table added with document.add(): see AddBigTable.pdf n the previous example every row just took one line. If you have rows that are much larger, you must know that tables are split between rows by default. So if only part of a row fits on the current page, the complete row is passed to the next page (see SplitRowsBetween.pdf). Only if the row doesn't fit on 1 page it is split in two.  You can change this by using setSplitLate(false). In this case, the row is split immediately. As you can see in SplitRowsWithin.pdf the maximum size of each page is used. f you really don't want the rows to be split, you'll have to use setSplitRows(false), but in that case, you will lose a lot of data: see OmitRows.pdf. Example: java com.lowagie.examples.objects.tables.SplitRows Add a table to a PDF with document.add() and if the cell doesn't fit, the row is split in two parts: see SplitRowsBetween.pdf SplitRowsWithin.pdf OmitRows.pdf Cellheights, -alignment, -padding, -leading, -colors:The content of a cell is wrapped by default. You can change this default with setNoWrap(true), but the result can look rather ugly, so be careful with this option.If you are using the default (nowrap = false), the height of the Cell is adjusted dynamically. Each cell in the same row has the height of the 'highest' cell. iText calculates this height based on different parameters: the number of lines of the content, the leading, the padding,... In some cases you don't want to give that kind of control to iText: you want a cell to have a fixed height. This can be done with the method setFixedHeight. Of course, if the content doesn't fit into this height, you will loose part of it. Maybe you want to do the opposite: maybe you want a cell to have a minimum height, even if it hasn't that much content. In that case, you'll have to use setMinimumHeight. Finally, there is another useful method setExtendLastRow. It extends the last row of a Table to the bottom of the page. Example: java com.lowagie.examples.objects.tables.CellHeights Playing with heights of PdfPCells: see CellHeights.pdf The following example demonstrates the self explaining methods setHorizontalAlignment and setVerticalAlignment. For the horizontal alignment, you can choose: * Element.ALIGN_LEFT * Element.ALIGN_CENTER  * Element.ALIGN_RIGHT    * Element.ALIGN_JUSTIFIED or the vertical alignment:   * Element.ALIGN_TOP  * Element.ALIGN_MIDDLE  * Element.ALIGN_BOTTOM  * Element.ALIGN_BASELINE Example: java com.lowagie.examples.objects.tables.CellAlignment  hanging the alignment of the contents of a PdfPCell: see Alignment.pdf he padding is the space between the content and the outer cellborder. You can set the amount of this space with setPadding, setPaddingTop, setPaddingRight, setPaddingLeft and/or setPaddingBottom. The leading is the space between the lines of the content. This value can be set with setLeading(float,%20float). xample: java com.lowagie.examples.objects.tables.CellPaddingLeading hanging the padding and the leading of the contents of a PdfPCell: see PaddingLeading.pdf f you set the padding, the borderwidth isn't taken into account. You can force iText to calculate the borderwidth into the padding with setUseBorderPadding(true). In the next example, some method to set colors and grayfill of borders and cells are demonstrated. As you can see, the padding of the green cell doesn't take the width of the magenta border into account. In the blue cell, the word 'blue' doesn't overlap with the cyan border. All methods that were used to change the borders and their color, were methods of the class Rectangle. Example: java com.lowagie.examples.objects.tables.CellColors Using some Rectangle methods on PdfPCell: see CellColors.pdf A last set of methods that influence the position of the content within a cell, are the methods setUseAscender and setUseDescender. An example is the best way to demonstrate the difference: Example: java com.lowagie.examples.objects.tables.TableBorders Demonstrates different borderstyles: see TableBorders.pdf In the example shown above, you also learn how to change the width and the color of cellborders. o to top of the page plitting tables over several pages: f you have really large tables, you can't expect them to fit on one page. iText splits the table automatically between rows. If your table has a header that needs to be repeated on every page, you need to tell the table how many rows the header counts. This can be done with the method setHeaderRows. In the example, there is one headerrow: xample: java com.lowagie.examples.objects.tables.AddBigTable  very big table added with document.add(): see AddBigTable.pdf n the previous example every row just took one line. If you have rows that are much larger, you must know that tables are split between rows by default. So if only part of a row fits on the current page, the complete row is passed to the next page (see SplitRowsBetween.pdf). Only if the row doesn't fit on 1 page it is split in two.  You can change this by using setSplitLate(false). In this case, the row is split immediately. As you can see in SplitRowsWithin.pdf the maximum size of each page is used. f you really don't want the rows to be split, you'll have to use setSplitRows(false), but in that case, you will lose a lot of data: see OmitRows.pdf. Example: java com.lowagie.examples.objects.tables.SplitRows Add a table to a PDF with document.add() and if the cell doesn't fit, the row is split in two parts: see SplitRowsBetween.pdf SplitRowsWithin.pdf OmitRows.pdf";
                                       
                                        cell = putBlankCell(0, 1);
                                        table.addCell(cell);
                                       
                                        cell = getCellWithContent(desc, 8, Font.NORMAL);
                                        table.addCell(cell);
                                       
                                        table.addCell(putBlankCell(0, 2));
                                       
                                        cell = new PdfPCell(table);
                                        cell.setBorderWidth(0);
                                        overAlltable.addCell(cell);
                                       
                                        overAlltable.addCell(putBlankCell(0, 1));
                                        overAlltable.addCell(putBlankCell(0, 3));
//End of assessment background segment
//Start of CAR Segment
                                        overAlltable.addCell(putBlankCell(0, 1));
                                        table = new PdfPTable(2);
                                        table.setWidthPercentage(100f);
                                        //float genInnrWidth[] = {10f, 90f};
                                        table.setWidths(genInnrWidth);
                                       
                                        cell = getCellWithContent("D:", 8, Font.BOLD);
                                        table.addCell(cell);
                                       
                                        cell = getCellWithContent("CORRECTIVE ACTION RESPONSE", 8, Font.UNDERLINE);
                                        table.addCell(cell);
                                       
                                        cell = putBlankCell(0, 1);
                                        table.addCell(cell);
                                       
                                        String CARDesc = "The non-compliance findings noted during this Assessment require a written response to include actions planned or taken. Timelines are required for planned actions.";
                                        String NoCARDesc = "A corrective action response is not required, as there were no findings noted during this assessment.";

          cell = getCellWithContent(CARDesc, 8, Font.NORMAL);
                                        table.addCell(cell);
                                       
                                        table.addCell(putBlankCell(0, 2));
                                       
                                        cell = new PdfPCell(table);
                                        cell.setBorderWidth(0);
                                        overAlltable.addCell(cell);
                                       
                                        overAlltable.addCell(putBlankCell(0, 1));
                                        overAlltable.addCell(putBlankCell(0, 3));
//End of CAR segment
//Start of ASSESSMENT FINDINGS Segment
                                        overAlltable.addCell(putBlankCell(0, 1));
                                        table = new PdfPTable(2);
                                        table.setWidthPercentage(100f);
                                        //float genInnrWidth[] = {10f, 90f};
                                        table.setWidths(genInnrWidth);
                                       
                                        cell = getCellWithContent("E:", 8, Font.BOLD);
                                        table.addCell(cell);
                                       
                                        cell = getCellWithContent("ASSESSMENT FINDINGS", 8, Font.UNDERLINE);
                                        table.addCell(cell);
                                       
                                        cell = putBlankCell(0, 1);
                                        table.addCell(cell);
                                       
                                        String FNDGDesc = "The non-compliance findings noted during this Assessment require a written response to include actions planned or taken. Timelines are required for planned actions.";
                                       
                                        cell = getCellWithContent(FNDGDesc, 8, Font.NORMAL);
                                        table.addCell(cell);
                                       
                                        table.addCell(putBlankCell(0, 2));
                                       
                                        cell = new PdfPCell(table);
                                        cell.setBorderWidth(0);
                                        overAlltable.addCell(cell);
                                       
                                        overAlltable.addCell(putBlankCell(0, 1));
                                        overAlltable.addCell(putBlankCell(0, 3));
                                        overAlltable.addCell(putBlankCell(0, 3));
                                        for(i = 0; i < 5; i++)
                                        {
                                                overAlltable.addCell(putBlankCell(0, 1));
                                                table = new PdfPTable(2);
                                                table.setWidthPercentage(100f);
                                                float innrRsltWidth[] = {12f, 88f};
                                                table.setWidths(innrRsltWidth);

                                                cell = getCellWithContent(String.valueOf(i+1)+".  ", 8, Font.BOLD);
                                                cell.setHorizontalAlignment(Element.ALIGN_RIGHT);
                                                table.addCell(cell);

                                                cell = getCellWithContent("getRsltDesc(FNDG)", 8, Font.NORMAL);
                                                table.addCell(cell);

                                                cell = putBlankCell(0, 1);
                                                table.addCell(cell);



                                                table.addCell(putBlankCell(00, 2));

                                                cell = new PdfPCell(table);
                                                cell.setBorderWidth(0);
                                                overAlltable.addCell(cell);

                                                overAlltable.addCell(putBlankCell(0, 1));
                                                overAlltable.addCell(putBlankCell(0, 3));
                                                if(i == 3)
                                                {
// Page Break
                                                        overAlltable.addCell(putBlankCell(0, 3));
                                                        document.add(overAlltable);
                                                        document.newPage();
                                                        overAlltable.deleteBodyRows();
                                                        overAlltable.addCell(putBlankCell( 0, 3));
                                                       
                                                        //////
                                                }
                                        }
//End of ASSESSMENT FINDINGS segment
                                       
                                       
                                       
                                       
                                       
                                       
                                       
                                       
                                                                       

                               
                                document.add(overAlltable);
                                document.newPage();
                               
                                                               
                } catch (Exception de) {
                                System.err.println(de.getMessage());
                                de.printStackTrace();
                        }
                        // step 5: we close the document
                        document.close();
        }
        private static PdfPCell getCellWithContent(String content, int fontSize, int fontEmp) throws DataFormatException
        {
                PdfPCell cell = null;
                try
                {
                       
                        Paragraph p;
                       
                       
                       
                        BaseFont arial = BaseFont.createFont("c:\\windows\\fonts\\arial.ttf", BaseFont.CP1252, BaseFont.NOT_EMBEDDED);
                        Font font = new Font(arial, fontSize);
                        p = new Paragraph(content, font);
                        cell = new PdfPCell(p);
                        cell.setColspan(1);
                        cell.setBorderWidthBottom(0);
                        cell.setBorderWidthRight(0);
                        cell.setBorderWidthLeft(0);
                        cell.setBorderWidthTop(0);
                }
                catch (Exception ex)
                {
                        ex.printStackTrace();
                }
                return cell;
        }
// private static PdfPCell putBlankCell(int bRight, int bTop, int bLeft, int bBottom, int colspan)
// {
// PdfPCell cell;
// Paragraph p;
// p = new Paragraph();
// cell = new PdfPCell(p);
// cell.setColspan(colspan);
// cell.setBorderWidthBottom(bBottom);
// cell.setBorderWidthRight(bRight);
// cell.setBorderWidthLeft(bLeft);
// cell.setBorderWidthTop(bTop);
// return cell;
// }
        private static PdfPCell putBlankCell(int border, int colspan)
        {
                PdfPCell cell;
                Paragraph p;
                p = new Paragraph();
                cell = new PdfPCell(p);
                cell.setColspan(colspan);
                cell.setBorderWidth(border);
                return cell;
        }

}


LightInTheBox - Buy quality products at wholesale price