Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 840247
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T05:35:35+00:00 2026-05-15T05:35:35+00:00

I am having trouble keeping my first and second rows of my main PdfPTable

  • 0

I am having trouble keeping my first and second rows of my main PdfPTable together using iText. My first row consists of a PdfPTable with some basic information. My second row consists of a PdfPTable that contains all of the tabulated info. Every time the tabulated info becomes too big and spans multiple pages, it is kicked to the second page automatically rather than showing up directly below the basic info and then paging to the next page. How can I avoid this problem? I have tried using setSplitRows(false), but I simply get a blank document (see commented lines 117 and 170). How can I keep my tabulated data (second row) up on the first page? An example of my code is shown below (you should be able to just copy/paste).

public class TestHelper{
    private TestEventHelper helper;

    public TestHelper(){
        super();
        helper  = new TestEventHelper();
    }



    public TestEventHelper getHelper() {
        return helper;
    }



    public void setHelper(TestEventHelper helper) {
        this.helper = helper;
    }



    public static void main(String[] args){
        TestHelper test = new TestHelper();
        TestEventHelper helper = test.getHelper();

        FileOutputStream file = null;
        Document document = null;
        PdfWriter writer = null;
        try {
            file = new FileOutputStream(new File("C://Documents and Settings//All Users//Desktop//pdffile2.pdf"));
            document = new Document(PageSize.A4.rotate(), 36, 36, 36, 36);

            writer = PdfWriter.getInstance(document, file);
//          writer.setPageEvent(templateHelper);
            writer.setPdfVersion(PdfWriter.PDF_VERSION_1_7);
            writer.setUserunit(1f);
            document.open();

            List<Element> pages = null;
            try {
                pages = helper.createTemplate();
            } catch (Exception e) {
                e.printStackTrace();
            }

            Iterator<Element> iterator = pages.iterator();
            while (iterator.hasNext()) {
                Element element = iterator.next();
                if (element instanceof Phrase) {
                    document.newPage();
                } else {
                    document.add(element);
                }
            }

        } catch (Exception de) {
            de.printStackTrace();
//          log.debug("Exception " + de + " " + de.getMessage());
        } finally {
            if (document != null) {
                document.close();
            }
            if (writer != null) {
                writer.close();
            }
        }
        System.out.println("Done!");
    }


private class TestEventHelper extends PdfPageEventHelper{

    // The PdfTemplate that contains the total number of pages.
    protected PdfTemplate total;
    protected BaseFont helv;

    private static final float SMALL_MARGIN = 20f;
    private static final float MARGIN = 36f;
    private final Font font = new Font(Font.HELVETICA, 12, Font.BOLD);
    private final Font font2 = new Font(Font.HELVETICA, 10, Font.BOLD);
    private final Font smallFont = new Font(Font.HELVETICA, 10, Font.NORMAL);

    private String[] datatableHeaderFields = new String[]{"Header1", "Header2", "Header3", "Header4", "Header5", "Header6", "Header7", "Header8", "Header9"};
    public TestEventHelper(){
        super();
    }

    public List<Element> createTemplate() throws Exception {
        List<Element> elementList = new ArrayList<Element>();
        float[] tableWidths = new float[]{1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.25f, 1.25f, 1.25f, 1.25f};
//      logger.debug("entering create reports template...");

        PdfPTable splitTable = new PdfPTable(1);
        splitTable.setSplitRows(false);
        splitTable.setWidthPercentage(100f);

        PdfPTable pageTable = new PdfPTable(1);
        pageTable.setKeepTogether(true);
        pageTable.setWidthPercentage(100f);

        PdfPTable searchTable = generateSearchFields();
        if(searchTable != null){
            searchTable.setSpacingAfter(25f);
        }

        PdfPTable outlineTable = new PdfPTable(1);
        outlineTable.setKeepTogether(true);
        outlineTable.setWidthPercentage(100f);

        PdfPTable datatable = new PdfPTable(datatableHeaderFields.length);
        datatable.setKeepTogether(false);
        datatable.setWidths(tableWidths);


        generateDatatableHeader(datatable);

        for(int i = 0; i < 100; i++){
            addCell(datatable, String.valueOf(i), 1, Rectangle.NO_BORDER, Element.ALIGN_CENTER, smallFont, true);
            addCell(datatable, String.valueOf(i+1), 1, Rectangle.NO_BORDER, Element.ALIGN_CENTER, smallFont, true);
            addCell(datatable, String.valueOf(i+2), 1, Rectangle.NO_BORDER, Element.ALIGN_CENTER, smallFont, true);
            addCell(datatable, String.valueOf(i+3), 1, Rectangle.NO_BORDER, Element.ALIGN_CENTER, smallFont, true);
            addCell(datatable, String.valueOf(i+4), 1, Rectangle.NO_BORDER, Element.ALIGN_CENTER, smallFont, true);
            addCell(datatable, String.valueOf(i+5), 1, Rectangle.NO_BORDER, Element.ALIGN_CENTER, smallFont, true);
            addCell(datatable, String.valueOf(i+6), 1, Rectangle.NO_BORDER, Element.ALIGN_CENTER, smallFont, true);
            addCell(datatable, String.valueOf(i+7), 1, Rectangle.NO_BORDER, Element.ALIGN_CENTER, smallFont, true);
            addCell(datatable, String.valueOf(i+8), 1, Rectangle.NO_BORDER, Element.ALIGN_RIGHT, smallFont, true);
        }

        PdfPCell dataCell = new PdfPCell(datatable);
        dataCell.setBorder(Rectangle.BOX);
        outlineTable.addCell(dataCell);

        PdfPCell searchCell = new PdfPCell(searchTable);
        searchCell.setVerticalAlignment(Element.ALIGN_TOP);

        PdfPCell outlineCell = new PdfPCell(outlineTable);
        outlineCell.setVerticalAlignment(Element.ALIGN_TOP);

        addCell(pageTable, searchCell, 1, Rectangle.NO_BORDER, Element.ALIGN_LEFT, null, null);
        addCell(pageTable, outlineCell, 1, Rectangle.NO_BORDER, Element.ALIGN_CENTER, null, null);

        PdfPCell pageCell = new PdfPCell(pageTable);
        pageCell.setVerticalAlignment(Element.ALIGN_TOP);
        addCell(splitTable, pageCell, 1, Rectangle.NO_BORDER, Element.ALIGN_CENTER, null, null);

        elementList.add(pageTable);
//      elementList.add(splitTable);

        return elementList;
    }

    public void onOpenDocument(PdfWriter writer, Document document) {
        total = writer.getDirectContent().createTemplate(100, 100);
        total.setBoundingBox(new Rectangle(-20, -20, 100, 100));
        try {
            helv = BaseFont.createFont(BaseFont.HELVETICA, BaseFont.WINANSI, BaseFont.NOT_EMBEDDED);
        } catch (Exception e) {
            throw new ExceptionConverter(e);
        }
    }

    public void onEndPage(PdfWriter writer, Document document) {
        //TODO
    }

    public void onCloseDocument(PdfWriter writer, Document document) {
        total.beginText();
        total.setFontAndSize(helv, 10);
        total.setTextMatrix(0, 0);
        total.showText(String.valueOf(writer.getPageNumber() - 1));
        total.endText();
    }

    private PdfPTable generateSearchFields(){
        PdfPTable searchTable = new PdfPTable(2);

        for(int i = 0; i < 6; i++){
            addCell(searchTable, "Search Key" +i, 1, Rectangle.NO_BORDER, Element.ALIGN_RIGHT, font2, MARGIN, true);
            addCell(searchTable, "Search Value +i", 1, Rectangle.NO_BORDER, Element.ALIGN_LEFT, smallFont, null, true);
        }
        return searchTable;
    }

    private void generateDatatableHeader(PdfPTable datatable) {
        if (datatableHeaderFields != null && datatableHeaderFields.length != 0) {
            for (int i = 0; i < datatableHeaderFields.length; i++) {
                addCell(datatable, datatableHeaderFields[i], 1, Rectangle.BOX, Element.ALIGN_CENTER, font2);
            }
        }
    }

    private PdfPCell addCell(PdfPTable table, String cellContent, int colspan, int cellBorder, int horizontalAlignment, Font font) {
        return addCell(table, cellContent, colspan, cellBorder, horizontalAlignment, font, null, null);
    }

    private PdfPCell addCell(PdfPTable table, String cellContent, int colspan, int cellBorder, int horizontalAlignment, Font font, Boolean noWrap) {
        return addCell(table, cellContent, colspan, cellBorder, horizontalAlignment, font, null, noWrap);
    }

    private PdfPCell addCell(PdfPTable table, String cellContent, Integer colspan, Integer cellBorder, Integer horizontalAlignment, Font font, Float paddingLeft, Boolean noWrap) {
        PdfPCell cell = new PdfPCell(new Phrase(cellContent, font));
        return addCell(table, cell, colspan, cellBorder, horizontalAlignment, paddingLeft, noWrap);
    }

    private PdfPCell addCell(PdfPTable table, PdfPCell cell, int colspan, int cellBorder, int horizontalAlignment, Float paddingLeft, Boolean noWrap) {
        cell.setColspan(colspan);
        cell.setBorder(cellBorder);
        cell.setHorizontalAlignment(horizontalAlignment);
        if(paddingLeft != null){
            cell.setPaddingLeft(paddingLeft);
        }
        if(noWrap != null){
            cell.setNoWrap(noWrap);
        }
        table.addCell(cell);
        return cell;
    }

}
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T05:35:36+00:00Added an answer on May 15, 2026 at 5:35 am

    The problem is caused by having nested PdfPTables. iText cannot split a single cell containing a large table in the way you want. The best option is to use 2 separate tables, 1 for your basic information at the top, and a second one for the data.

    I got your code to work by commenting out some the nesting code in createTemplate()

        public List<Element> createTemplate() throws Exception {
        List<Element> elementList = new ArrayList<Element>();
        float[] tableWidths = new float[]{1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.25f, 1.25f, 1.25f, 1.25f};
        //      logger.debug("entering create reports template...");
    
        PdfPTable splitTable = new PdfPTable(1);
        splitTable.setSplitRows(false);
        splitTable.setWidthPercentage(100f);
    
        PdfPTable pageTable = new PdfPTable(1);
        pageTable.setKeepTogether(true);
        pageTable.setWidthPercentage(100f);
    
        PdfPTable searchTable = generateSearchFields();
        if(searchTable != null){
            searchTable.setSpacingAfter(25f);
        }
    
        PdfPTable outlineTable = new PdfPTable(1);
        outlineTable.setKeepTogether(true);
        outlineTable.setWidthPercentage(100f);
    
        PdfPTable datatable = new PdfPTable(datatableHeaderFields.length);
        //        datatable.setKeepTogether(false);
        datatable.setWidths(tableWidths);
    
    
        generateDatatableHeader(datatable);
    
        for(int i = 0; i < 100; i++){
            addCell(datatable, String.valueOf(i), 1, Rectangle.NO_BORDER, Element.ALIGN_CENTER, smallFont, true);
            addCell(datatable, String.valueOf(i+1), 1, Rectangle.NO_BORDER, Element.ALIGN_CENTER, smallFont, true);
            addCell(datatable, String.valueOf(i+2), 1, Rectangle.NO_BORDER, Element.ALIGN_CENTER, smallFont, true);
            addCell(datatable, String.valueOf(i+3), 1, Rectangle.NO_BORDER, Element.ALIGN_CENTER, smallFont, true);
            addCell(datatable, String.valueOf(i+4), 1, Rectangle.NO_BORDER, Element.ALIGN_CENTER, smallFont, true);
            addCell(datatable, String.valueOf(i+5), 1, Rectangle.NO_BORDER, Element.ALIGN_CENTER, smallFont, true);
            addCell(datatable, String.valueOf(i+6), 1, Rectangle.NO_BORDER, Element.ALIGN_CENTER, smallFont, true);
            addCell(datatable, String.valueOf(i+7), 1, Rectangle.NO_BORDER, Element.ALIGN_CENTER, smallFont, true);
            addCell(datatable, String.valueOf(i+8), 1, Rectangle.NO_BORDER, Element.ALIGN_RIGHT, smallFont, true);
        }
    
        //        PdfPCell dataCell = new PdfPCell(datatable);
        //        dataCell.setBorder(Rectangle.BOX);
        //        outlineTable.addCell(dataCell);
        //
        //        PdfPCell searchCell = new PdfPCell(searchTable);
        //        searchCell.setVerticalAlignment(Element.ALIGN_TOP);
        //
        //        PdfPCell outlineCell = new PdfPCell(outlineTable);
        //        outlineCell.setVerticalAlignment(Element.ALIGN_TOP);
        //
        //        addCell(pageTable, searchCell, 1, Rectangle.NO_BORDER, Element.ALIGN_LEFT, null, null);
        //        addCell(pageTable, outlineCell, 1, Rectangle.NO_BORDER, Element.ALIGN_CENTER, null, null);
        //
        //        PdfPCell pageCell = new PdfPCell(pageTable);
        //        pageCell.setVerticalAlignment(Element.ALIGN_TOP);
        //        addCell(splitTable, pageCell, 1, Rectangle.NO_BORDER, Element.ALIGN_CENTER, null, null);
    
        elementList.add(searchTable);
        elementList.add(datatable);
        //      elementList.add(pageTable);
        //      elementList.add(splitTable);
    
        return elementList;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using JQuery Mobile 1.1.0 and I'm having trouble keeping the underlying list selections
I'm having trouble using filesystemobject in a vba function I am writing. I keeping
I am having trouble keeping a ListBox updated with child objects. I'm using two
I'm having some trouble keeping objects in a vector, and I think I need
I'm having some trouble keeping apart the terms class, object, variable and datatype. can
I am having some trouble with using a parallel version of map ( ppmap
Keeping these in mind - HttpContext.Current - Foreach I'm having trouble wrapping my head
Having trouble understanding. With the following css : .bloc .field:nth-last-child(2){ ...some values... } and
I am just starting out with network programming and I am having trouble keeping
I am having trouble getting my pointers to work correctly. In my main file

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.