0

I need an easy method that converts the excel to pdf automatically taking account of merged cells and empty cells. Is there any available method from itext that does this? I know this example below but it doesn't work with a complex excel:

public static void main(String[] args) throws Exception{
                //First we read the Excel file in binary format into FileInputStream
                FileInputStream input_document = new FileInputStream(new File("C:\\excel_to_pdf.xls"));
                // Read workbook into HSSFWorkbook
                HSSFWorkbook my_xls_workbook = new HSSFWorkbook(input_document); 
                // Read worksheet into HSSFSheet
                HSSFSheet my_worksheet = my_xls_workbook.getSheetAt(0); 
                // To iterate over the rows
                Iterator<Row> rowIterator = my_worksheet.iterator();
                //We will create output PDF document objects at this point
                Document iText_xls_2_pdf = new Document();
                PdfWriter.getInstance(iText_xls_2_pdf, new FileOutputStream("Excel2PDF_Output.pdf"));
                iText_xls_2_pdf.open();
                //Suppose we have two columns in the Excel sheet, so we create a PDF table with two columns
                //Note: There are ways to make this dynamic in nature, if you want to.
                PdfPTable my_table = new PdfPTable(2);
                //We will use the object below to dynamically add new data to the table
                PdfPCell table_cell;
                //Loop through rows.
                while(rowIterator.hasNext()) {
                        Row row = rowIterator.next(); 
                        Iterator<Cell> cellIterator = row.cellIterator();
                                while(cellIterator.hasNext()) {
                                        Cell cell = cellIterator.next(); //Fetch CELL
                                        switch(cell.getCellType()) { //Identify CELL type
                                                //you need to add more code here based on
                                                //your requirement / transformations
                                        case Cell.CELL_TYPE_STRING:
                                                //Push the data from Excel to PDF Cell
                                                 table_cell=new PdfPCell(new Phrase(cell.getStringCellValue()));
                                                 //feel free to move the code below to suit to your needs
                                                 my_table.addCell(table_cell);
                                                break;
                                        }
                                        //next line
                                }

                }
                //Finally add the table to PDF document
                iText_xls_2_pdf.add(my_table);                       
                iText_xls_2_pdf.close();                
                //we created our pdf file..
                input_document.close(); //close xls
        }
manuel_k
  • 575
  • 1
  • 8
  • 22
  • you should checkout the Apache POI project: https://poi.apache.org/ And also this stackoverflow thread: https://stackoverflow.com/questions/17826401/convert-doc-to-pdf-using-apache-poi – Axel Podehl Jan 15 '18 at 09:24
  • Or this one: https://www.docx4java.org/trac/docx4j. Haven't tried it myself, but Apache POI isn't exactly easy to use. – Axel Podehl Jan 15 '18 at 09:26
  • @AxelPodehl not working. I tried the options you provided – manuel_k Jan 15 '18 at 15:57
  • When you only use cell.getStringValue(), I think the information about your first/last column is lost. You probably also need to use the worksheet's merged regions to compute the table_cell's first/last column: for( int m=0; m – Axel Podehl Jan 16 '18 at 08:12
  • @AxelPodehl i am already using this method but it gives me a bad table when merging 2 cells column (example: C2:D2). it works fine if the merge is C2:C4 on the same column – manuel_k Jan 16 '18 at 11:53

0 Answers0