Skip to main content
Skip table of contents

Colspan and rowspan

Some table examples involving rowspan and/or colspan.


simplerowcolspan

JAVA

JAVA
/**
 * Example written by Bruno Lowagie in answer to:
 * http://stackoverflow.com/questions/20016630/how-to-create-a-table-in-a-generated-pdf-using-itextsharp
 * 
 * We create a table with five columns, combining rowspan and colspan
 */
package sandbox.tables;

import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

import sandbox.WrapToTest;

@WrapToTest
public class SimpleRowColspan {
    public static final String DEST = "results/tables/simple_rowspan_colspan.pdf";

    public static void main(String[] args) throws IOException,
            DocumentException {
        File file = new File(DEST);
        file.getParentFile().mkdirs();
        new SimpleRowColspan().createPdf(DEST);
    }
    
    public void createPdf(String dest) throws IOException, DocumentException {
        Document document = new Document();
        PdfWriter.getInstance(document, new FileOutputStream(dest));
        document.open();
        PdfPTable table = new PdfPTable(5);
        table.setWidths(new int[]{ 1, 2, 2, 2, 1});
        PdfPCell cell;
        cell = new PdfPCell(new Phrase("S/N"));
        cell.setRowspan(2);
        table.addCell(cell);
        cell = new PdfPCell(new Phrase("Name"));
        cell.setColspan(3);
        table.addCell(cell);
        cell = new PdfPCell(new Phrase("Age"));
        cell.setRowspan(2);
        table.addCell(cell);
        table.addCell("SURNAME");
        table.addCell("FIRST NAME");
        table.addCell("MIDDLE NAME");
        table.addCell("1");
        table.addCell("James");
        table.addCell("Fish");
        table.addCell("Stone");
        table.addCell("17");
        document.add(table);
        document.close();
    }
}


colspanrowspan

JAVA

JAVA
/**
 * Example written by Bruno Lowagie in answer to the following question:
 * http://stackoverflow.com/questions/23989852/itext-combining-rowspan-and-colspan-pdfptable
 */
package sandbox.tables;

import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import sandbox.WrapToTest;

@WrapToTest
public class ColspanRowspan {

    public static final String DEST = "results/tables/colspan_rowspan.pdf";
    
    public static void main(String[] args) throws IOException, DocumentException {
        File file = new File(DEST);
        file.getParentFile().mkdirs();
        new ColspanRowspan().createPdf(DEST);
    }
    
    public void createPdf(String dest) throws IOException, DocumentException {
        Document document = new Document();
        PdfWriter.getInstance(document, new FileOutputStream(dest));
        document.open();
        PdfPTable table = new PdfPTable(4);
        PdfPCell cell = new PdfPCell(new Phrase(" 1,1 "));
        table.addCell(cell);
        cell = new PdfPCell(new Phrase(" 1,2 "));
        table.addCell(cell);
        PdfPCell cell23 = new PdfPCell(new Phrase("multi 1,3 and 1,4"));
        cell23.setColspan(2);
        cell23.setRowspan(2);
        table.addCell(cell23);
        cell = new PdfPCell(new Phrase(" 2,1 "));
        table.addCell(cell);
        cell = new PdfPCell(new Phrase(" 2,2 "));
        table.addCell(cell);
        document.add(table);
        document.close();
    }
}


simpletable2

JAVA

JAVA
/**
 * Example written by Bruno Lowagie and Nishanthi Grashia in answer to the following question:
 * http://stackoverflow.com/questions/24359321/adding-row-to-a-table-in-pdf-using-itext
 */
package sandbox.tables;

import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import sandbox.WrapToTest;

@WrapToTest
public class SimpleTable2 {
    public static final String DEST = "results/tables/simple_table2.pdf";

    public static void main(String[] args) throws IOException,
            DocumentException {
        File file = new File(DEST);
        file.getParentFile().mkdirs();
        new SimpleTable2().createPdf(DEST);
    }
    public void createPdf(String dest) throws IOException, DocumentException {
        Document document = new Document();
        PdfWriter.getInstance(document, new FileOutputStream(dest));
        document.open();
        PdfPTable table = new PdfPTable(8);
        PdfPCell cell = new PdfPCell(new Phrase("hi"));
        cell.setRowspan(2);
        table.addCell(cell);
        for(int aw = 0; aw < 14; aw++){
            table.addCell("hi");
        }
        document.add(table);
        document.close();
    }

}


simpletable9

JAVA

JAVA
/**
 * Example written by Bruno Lowagie in answer to the following question:
 * http://stackoverflow.com/questions/30032558/how-to-show-a-cellcolumn-with-its-row-values-of-a-pdftableitextsharp-in-next
 */
package sandbox.tables;

import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import sandbox.WrapToTest;

@WrapToTest
public class SimpleTable9 {
    public static final String DEST = "results/tables/simple_table9.pdf";

    public static void main(String[] args) throws IOException,
            DocumentException {
        File file = new File(DEST);
        file.getParentFile().mkdirs();
        new SimpleTable9().createPdf(DEST);
    }
    public void createPdf(String dest) throws IOException, DocumentException {
        Document document = new Document();
        PdfWriter.getInstance(document, new FileOutputStream(dest));
        document.open();
        document.add(new Paragraph("With 3 columns:"));
        PdfPTable table = new PdfPTable(3);
        table.setSpacingBefore(5);
        table.setWidths(new int[]{1, 1, 8});
        table.setWidthPercentage(100);
        table.addCell("Col a");
        table.addCell("Col b");
        table.addCell("Col c");
        table.addCell("Value a");
        table.addCell("Value b");
        table.addCell("This is a long description for column c. It needs much more space hence we made sure that the third column is wider.");
        document.add(table);
        document.add(new Paragraph("With 2 columns:"));
        table = new PdfPTable(2);
        table.setSpacingBefore(5);
        table.setWidthPercentage(100);
        table.getDefaultCell().setColspan(1);
        table.addCell("Col a");
        table.addCell("Col b");
        table.addCell("Value a");
        table.addCell("Value b");
        table.getDefaultCell().setColspan(2);
        table.addCell("Value b");
        table.addCell("This is a long description for column c. It needs much more space hence we made sure that the third column is wider.");
        table.getDefaultCell().setColspan(1);
        table.addCell("Col a");
        table.addCell("Col b");
        table.addCell("Value a");
        table.addCell("Value b");
        table.getDefaultCell().setColspan(2);
        table.addCell("Value b");
        table.addCell("This is a long description for column c. It needs much more space hence we made sure that the third column is wider.");
        document.add(table);
        document.close();
    }

}


simpletable11

JAVA

JAVA
/**
 * Example written by Bruno Lowagie in answer to the following question:
 * http://stackoverflow.com/questions/31146249/how-to-set-up-a-table-display-in-itextpdf
 */
package sandbox.tables;

import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Element;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import sandbox.WrapToTest;

@WrapToTest
public class SimpleTable11 {
    public static final String DEST = "results/tables/simple_table11.pdf";

    public static void main(String[] args) throws IOException,
            DocumentException {
        File file = new File(DEST);
        file.getParentFile().mkdirs();
        new SimpleTable11().createPdf(DEST);
    }
    public void createPdf(String dest) throws IOException, DocumentException {
        Document document = new Document();
        PdfWriter.getInstance(document, new FileOutputStream(dest));
        document.open();
        PdfPTable table = new PdfPTable(5);
        table.setWidths(new int[]{1, 2, 1, 1, 1});
        table.addCell(createCell("SKU", 2, 1, Element.ALIGN_LEFT));
        table.addCell(createCell("Description", 2, 1, Element.ALIGN_LEFT));
        table.addCell(createCell("Unit Price", 2, 1, Element.ALIGN_LEFT));
        table.addCell(createCell("Quantity", 2, 1, Element.ALIGN_LEFT));
        table.addCell(createCell("Extension", 2, 1, Element.ALIGN_LEFT));
        String[][] data = {
            {"ABC123", "The descriptive text may be more than one line and the text should wrap automatically", "$5.00", "10", "$50.00"},
            {"QRS557", "Another description", "$100.00", "15", "$1,500.00"},
            {"XYZ999", "Some stuff", "$1.00", "2", "$2.00"}
        };
        for (String[] row : data) {
            table.addCell(createCell(row[0], 1, 1, Element.ALIGN_LEFT));
            table.addCell(createCell(row[1], 1, 1, Element.ALIGN_LEFT));
            table.addCell(createCell(row[2], 1, 1, Element.ALIGN_RIGHT));
            table.addCell(createCell(row[3], 1, 1, Element.ALIGN_RIGHT));
            table.addCell(createCell(row[4], 1, 1, Element.ALIGN_RIGHT));
        }
        table.addCell(createCell("Totals", 2, 4, Element.ALIGN_LEFT));
        table.addCell(createCell("$1,552.00", 2, 1, Element.ALIGN_RIGHT));
        document.add(table);
        document.close();
    }
    
    public PdfPCell createCell(String content, float borderWidth, int colspan, int alignment) {
        PdfPCell cell = new PdfPCell(new Phrase(content));
        cell.setBorderWidth(borderWidth);
        cell.setColspan(colspan);
        cell.setHorizontalAlignment(alignment);
        return cell;
    }

}


simpletable12

JAVA

JAVA
/**
 * Example written by Bruno Lowagie in answer to the following question:
 * http://stackoverflow.com/questions/31263533/how-to-create-nested-column-using-itextsharp
 */
package sandbox.tables;

import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Element;
import com.itextpdf.text.Font;
import com.itextpdf.text.Font.FontFamily;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import sandbox.WrapToTest;

@WrapToTest
public class SimpleTable12 {
    public static final String DEST = "results/tables/simple_table12.pdf";
    
    protected Font font;

    public static void main(String[] args) throws IOException,
            DocumentException {
        File file = new File(DEST);
        file.getParentFile().mkdirs();
        new SimpleTable12().createPdf(DEST);
    }
    public void createPdf(String dest) throws IOException, DocumentException {
        font = new Font(FontFamily.HELVETICA, 10);
        Document document = new Document();
        PdfWriter.getInstance(document, new FileOutputStream(dest));
        document.open();
        PdfPTable table = new PdfPTable(8);
        table.setWidthPercentage(100);
        table.addCell(createCell("Examination", 1, 2, PdfPCell.BOX));
        table.addCell(createCell("Board", 1, 2, PdfPCell.BOX));
        table.addCell(createCell("Month and Year of Passing", 1, 2, PdfPCell.BOX));
        table.addCell(createCell("", 1, 1, PdfPCell.TOP));
        table.addCell(createCell("Marks", 2, 1, PdfPCell.TOP));
        table.addCell(createCell("Percentage", 1, 2, PdfPCell.BOX));
        table.addCell(createCell("Class / Grade", 1, 2, PdfPCell.BOX));
        table.addCell(createCell("", 1, 1, PdfPCell.BOX));
        table.addCell(createCell("Obtained", 1, 1, PdfPCell.BOX));
        table.addCell(createCell("Out of", 1, 1, PdfPCell.BOX));
        table.addCell(createCell("12th / I.B. Diploma", 1, 1, PdfPCell.BOX));
        table.addCell(createCell("", 1, 1, PdfPCell.BOX));
        table.addCell(createCell("", 1, 1, PdfPCell.BOX));
        table.addCell(createCell("Aggregate (all subjects)", 1, 1, PdfPCell.BOX));
        table.addCell(createCell("", 1, 1, PdfPCell.BOX));
        table.addCell(createCell("", 1, 1, PdfPCell.BOX));
        table.addCell(createCell("", 1, 1, PdfPCell.BOX));
        table.addCell(createCell("", 1, 1, PdfPCell.BOX));
        document.add(table);
        document.close();
    }
    
    public PdfPCell createCell(String content, int colspan, int rowspan, int border) {
        PdfPCell cell = new PdfPCell(new Phrase(content, font));
        cell.setColspan(colspan);
        cell.setRowspan(rowspan);
        cell.setBorder(border);
        cell.setHorizontalAlignment(Element.ALIGN_CENTER);
        return cell;
    }

}


rowspanabsoluteposition

JAVA

JAVA
/**
 * Example written by Bruno Lowagie in answer to:
 * http://stackoverflow.com/questions/23730886/row-span-not-working-for-writeselectedrows-method
 * 
 * We create a table with five columns, combining rowspan and colspan
 */
package sandbox.tables;

import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Element;
import com.itextpdf.text.Image;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import sandbox.WrapToTest;

@WrapToTest
public class RowspanAbsolutePosition {
    public static final String DEST = "results/tables/write_selected_colspan.pdf";
    public static final String IMG = "resources/images/berlin2013.jpg";

    public static void main(String[] args) throws IOException,
            DocumentException {
        File file = new File(DEST);
        file.getParentFile().mkdirs();
        new RowspanAbsolutePosition().createPdf(DEST);
    }
    
    public void createPdf(String dest) throws IOException, DocumentException {
        Document document = new Document();
        PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(dest));
        document.open();
        PdfPTable table1 = new PdfPTable(3);
        table1.setWidths(new int[]{15,20,20});
        table1.setTotalWidth(555);
        PdfPCell cell = new PdfPCell(new Phrase("{Month}"));
        cell.setColspan(2);
        cell.setHorizontalAlignment(Element.ALIGN_LEFT);
        Image img = Image.getInstance(IMG);
        img.scaleToFit(555f * 20f / 55f, 10000);
        PdfPCell cell2 = new PdfPCell(img, false);
        cell2.setRowspan(2);
        PdfPCell cell3 = new PdfPCell(new Phrase("Mr Fname Lname"));
        cell3.setColspan(2);
        cell3.setHorizontalAlignment(Element.ALIGN_LEFT);
        table1.addCell(cell);
        table1.addCell(cell2);
        table1.addCell(cell3);
        table1.writeSelectedRows(0, -1, 20 , 820, writer.getDirectContent());
        document.close();
    }
}


tablemeasurements

JAVA

JAVA
/*
 * Example written in answer to:
 * http://stackoverflow.com/questions/34539028
 */
package sandbox.tables;

import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Element;
import com.itextpdf.text.Font;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.Utilities;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

/**
 *
 * @author Bruno Lowagie (iText Software)
 */
public class TableMeasurements {
    public static final String DEST = "results/tables/table_measurements.pdf";

    public static void main(String[] args) throws IOException,
            DocumentException {
        File file = new File(DEST);
        file.getParentFile().mkdirs();
        new TableMeasurements().createPdf(DEST);
    }
    public void createPdf(String dest) throws IOException, DocumentException {
        Document document = new Document();
        PdfWriter.getInstance(document, new FileOutputStream(dest));
        document.open();
        PdfPTable table = new PdfPTable(10);
        table.setTotalWidth(Utilities.millimetersToPoints(100));
        table.setLockedWidth(true);
        table.getDefaultCell().setBorder(PdfPCell.NO_BORDER);
        table.addCell(getCell(10));
        table.addCell(getCell(5));
        table.addCell(getCell(3));
        table.addCell(getCell(2));
        table.addCell(getCell(3));
        table.addCell(getCell(5));
        table.addCell(getCell(1));
        table.completeRow();
        document.add(table);
        document.close();
    }
    
    private PdfPCell getCell(int cm) {
        PdfPCell cell = new PdfPCell();
        cell.setColspan(cm);
        cell.setUseAscender(true);
        cell.setUseDescender(true);
        Paragraph p = new Paragraph(
                String.format("%smm", 10 * cm),
                new Font(Font.FontFamily.HELVETICA, 8));
        p.setAlignment(Element.ALIGN_CENTER);
        cell.addElement(p);
        return cell;
    }
}

Results

JavaScript errors detected

Please note, these errors can depend on your browser setup.

If this problem persists, please contact our support.