Convert CSV to PDF– Java Program Example

In this series, we are providing examples for converting a CSV file to PDF using Java. In this part of the series, we will provide a standalone program in Java that will do the following:
  • Read input CSV file
  • Create PDF document
  • Populate a PDF table from the contents of the CSV file
  • Writes PDF file to output
Each of these steps is described briefly now. The full Java program is provided at the end of this post.

Read Input CSV File

In this step, we will use opencsv-2.3.jar file, and read the input CSV file data into the program memory. The program assumes the CSV file has two columns, even though you can make that dynamic depending on your needs. A code fragment that reads a CSV file is given below:
                /* Step -1 : Read input CSV file in Java */
                String inputCSVFile = "csv_to_pdf.csv";
                CSVReader reader = new CSVReader(new FileReader(inputCSVFile));




Create PDF document

We will use itextpdf-5.3.4.jar and create a Document object, and attach it to a PdfWriter object at this step. This will create a PDF file for us and we can start writing the contents of the CSV file to the PDF following this. We will also create iText table objects that maps to a physical PDF table and will hold CSV file data. The code fragment is provided below:


                /* Step-2: Initialize PDF documents - logical objects */
                Document my_pdf_data = new Document();
                PdfWriter.getInstance(my_pdf_data, new FileOutputStream("converted_PDF_File.pdf"));
                my_pdf_data.open();            
                PdfPTable my_first_table = new PdfPTable(2);
                PdfPCell table_cell;



Populate PDF table from CSV data

In this step, we will loop through the CSV file line by line and dump the contents of this file into a PDF table. We will use Pdfptable object to capture CSV file data and attach this table to the PDF document created earlier.


                /* Step -3: Loop through CSV file and populate data to PDF table */
                while ((nextLine = reader.readNext()) != null) {
                        lnNum++;        
                        table_cell=new PdfPCell(new Phrase(nextLine[0]));
                        my_first_table.addCell(table_cell);
                        table_cell=new PdfPCell(new Phrase(nextLine[1]));
                        my_first_table.addCell(table_cell);                                             
                }


Write PDF file to output

Once all the lines in the CSV file are read, we close the PDF document and the write the output to a file. The CSV file opened for reading can also be closed at this point. We also attach the logical PDF table to Document object at this step. This ensures the table is written to PDF.


                /* Step -4: Attach table to PDF and close the document */
                my_pdf_data.add(my_first_table);                       
                my_pdf_data.close();            


CSV to PDF – Complete Java Program

The complete Java program that converts a CSV file to a table in PDF document is provided below:

import java.io.FileOutputStream;
import java.io.*;
import au.com.bytecode.opencsv.CSVReader;
import java.io.FileReader;
import java.util.*;
import com.itextpdf.text.*;
import com.itextpdf.text.pdf.*;
public class csv2pdf {  
        public static void main(String[] args) throws Exception{
                
                /* Step -1 : Read input CSV file in Java */
                String inputCSVFile = "csv_to_pdf.csv";
                CSVReader reader = new CSVReader(new FileReader(inputCSVFile));
                /* Variables to loop through the CSV File */
                String [] nextLine; /* for every line in the file */            
                int lnNum = 0; /* line number */
                /* Step-2: Initialize PDF documents - logical objects */
                Document my_pdf_data = new Document();
                PdfWriter.getInstance(my_pdf_data, new FileOutputStream("converted_PDF_File.pdf"));
                my_pdf_data.open();            
                PdfPTable my_first_table = new PdfPTable(2);
                PdfPCell table_cell;
                /* Step -3: Loop through CSV file and populate data to PDF table */
                while ((nextLine = reader.readNext()) != null) {
                        lnNum++;        
                        table_cell=new PdfPCell(new Phrase(nextLine[0]));
                        my_first_table.addCell(table_cell);
                        table_cell=new PdfPCell(new Phrase(nextLine[1]));
                        my_first_table.addCell(table_cell);                                             
                }
                /* Step -4: Attach table to PDF and close the document */
                my_pdf_data.add(my_first_table);                       
                my_pdf_data.close();            
        }
}


CSV to PDF – Output Snapshot


A snapshot of the output created by the program above is given below:

CSV to PDF Conversion in Java - Example Output
CSV to PDF - Java Program Output - Example



That completes the standalone Java program example for the tutorial. In the next part, we will examine how to create a servlet for this conversion. Related links to this series are available below:

This series: Convert CSV to PDF using Java
Keywords: Convert CSV to PDF, Java Example Program, OpenCSV, iText, Java Servlet Example
All Parts:
1. Convert CSV to PDF– Standard Java Program Example
2. CSV to PDF Servlet Conversion – HTML Form Design
3. CSV to PDF Converter – Full Java Servlet Program

No comments:

Post a Comment