iText pdf library
Website search

How can I convert a CSV file to a table with a repeating header row?

I am generating a PDF file from CSV using iText. I need to format the file such that the header row (which occurs in the beginning of every page) should be in a different font and color. Just to be clear, I know how to set the font style/size/color. I'm having a tough time finding out how to do that for the header rows of my table.

Posted on StackOverflow on Nov 10, 2014 by harsha

Your requirement is explained in large detail in our tutorial video, more specifically in the UnitedStates example. In this example, we take a CSV file containing the different states of the US: united_states.csv

name;abbr;capital;most populous city;population;square miles;time zone 1;time zone 2;dst
ALABAMA;AL;Montgomery;Birmingham;4,708,708;52,423;CST (UTC-6);EST (UTC-5);YES
ALASKA;AK;Juneau;Anchorage;698,473;656,425;AKST (UTC-09) ;HST (UTC-10) ;YES
ARIZONA;AZ;Phoenix;Phoenix;6,595,778;114,006;MT (UTC-07); ;NO
ARKANSAS;AR;Little Rock;Little Rock;2,889,450;53,182;CST (UTC-6); ;YES
CALIFORNIA;CA;Sacramento;Los Angeles;36,961,664;163,707;PT (UTC-8); ;YES

And we parse these into a PDF with a repeating header: united_states.pdf

Here is the code:

protected void manipulatePdf(String dest) throws Exception {
        PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));
        Document doc = new Document(pdfDoc, PageSize.A4.rotate());

        PdfFont font = PdfFontFactory.createFont(StandardFonts.HELVETICA);
        PdfFont bold = PdfFontFactory.createFont(StandardFonts.HELVETICA_BOLD);

        Table table = new Table(UnitValue.createPercentArray(new float[] {14, 6, 12, 16, 12, 12, 12, 12, 6}));
        try (BufferedReader br = new BufferedReader(new FileReader(DATA))) {
            String line = br.readLine();

            // The last argument defines which cell will be added: a header or the usual one
            addRowToTable(table, line, bold, true);
            while ((line = br.readLine()) != null) {
                addRowToTable(table, line, font, false);



    public void addRowToTable(Table table, String line, PdfFont font, boolean isHeader) {

        // Parses string line with specified delimiter
        StringTokenizer tokenizer = new StringTokenizer(line, ";");

        // Creates cells according to parsed csv line
        while (tokenizer.hasMoreTokens()) {
            Cell cell = new Cell().add(new Paragraph(tokenizer.nextToken()).setFont(font));

            if (isHeader) {
            } else {

Take a close look at the process() method: it accepts a font parameter so that we can define a bigger, bolder font for the header, and isHeader parameter to define the moment of usage of addHeaderCell() method.

Click this link if you want to see how to answer this question in iText 5.

Ready to use iText?

Try our iText 7 Library and add-ons FREE for 30 days. Test your proof of concept, and see if our solution is right for you.

Get my FREE trial

Still have questions? 

We're happy to answer your questions. Reach out to us and we'll get back to you shortly.

Contact us
Stay updated

Join 11,000+ subscribers and become an iText PDF expert by staying up to date with our new products, updates, tips, technical solutions and happenings.

Subscribe Now