0

I have table with 30 rows and array list full of strings.Table has 2 columns, in first is like company name , in second i need to write Strings from list, each row of that second column should have 10 elements, How do I do that ?

PDF Output should be something like this :

Company name |||| DESTINATION      
AirFrance |||| Tokio,London,Istanbul,New Nork,Paris,Vienna,OSLO,Belgrade,Budapest,Amsterdam.

Each company should read 10 destination from list.

It's probably something easy that I just can't see I tried to count list elements and somehow partition it but it didn't work.

By the way i use itextpdf library and i read those data from excel.

Here is code :

public class ReadExcel {
    private String inputfile;
    private List<Avio> avioni;

    public ReadExcel() {

        this.avioni = new ArrayList<Avio>();
    }


public List<Avio> getAvioni() {
    return avioni;
}

public void setAvioni(List<Avio> avioni) {
    this.avioni = avioni;
}

public String getInputfile() {
    return inputfile;
}

public void setInputfile(String inputfile) {
    this.inputfile = inputfile;
}

public void citaj(String naziv) {
    Avio a = new Avio();

    try {
        Workbook w = Workbook.getWorkbook(new File(naziv));
        Sheet sheet = w.getSheet(0);
        // System.out.println(sheet.getRows());
        for (int i = 1; i < sheet.getRows(); i++) {
            Avio tmpAvio = new Avio();

            tmpAvio.setDrzava(sheet.getCell(0, i).getContents());
            System.out.println();
            tmpAvio.setNazivKomp(sheet.getCell(1, i).getContents());
            tmpAvio.setVlasnik(sheet.getCell(2, i).getContents());
            tmpAvio.setBrAviona(Integer.parseInt(sheet.getCell(3, i)
                    .getContents()));
            tmpAvio.setGod(Integer.parseInt(sheet.getCell(4, i)
                    .getContents()));

            tmpAvio.setDatum(sheet.getCell(6,i).getContents());
            tmpAvio.setModelAviona(sheet.getCell(7,i).getContents());
            tmpAvio.setKapetan(sheet.getCell(8,i).getContents());
            tmpAvio.setJMBG(sheet.getCell(9,i).getContents());
            tmpAvio.setIskustvo(sheet.getCell(10,i).getContents());
            tmpAvio.setBrPutnika(sheet.getCell(11,i).getContents());
            tmpAvio.setLet(sheet.getCell(12,i).getContents());

            avioni.add(tmpAvio);


        }

        Sheet sheet1 = w.getSheet(1);

        List<String> destinacije = new ArrayList<String>();
        for (int i = 0; i < sheet1.getRows(); i++) {
            for (int j = 0; j < sheet1.getColumns(); j++) {

                destinacije.add(sheet1.getCell(j, i).getContents());
                System.out.println(sheet1.getCell(j,i).getContents()+i+","+j);
            }
            avioni.get(i).setDestinacije(destinacije);
            System.out.println(avioni.get(i));
        }


    }

    catch (BiffException e1) {

        e1.printStackTrace();
    } catch (IOException e) {

        e.printStackTrace();
    }


}

public void kreirajPDF(String naziv) {

    Document d = new Document();
    try {
        PdfWriter.getInstance(d, new FileOutputStream(new File(naziv)));
        d.open();

        File fontFile = new File("swansea.ttf");
        BaseFont unicode = BaseFont.createFont(fontFile.getAbsolutePath(),
                BaseFont.IDENTITY_H, BaseFont.EMBEDDED);

        Font titleFont = new Font(unicode, 16, Font.BOLD);
        titleFont.setColor(BaseColor.WHITE);
        Font textFont = new Font(unicode, 11, Font.NORMAL);
        Font headerFont = new Font(unicode, 14, Font.BOLD);

        Paragraph p1 = new Paragraph("Evropske Avio Kompanije", titleFont);
        p1.setAlignment(com.itextpdf.text.Element.ALIGN_CENTER);
        Image img = Image.getInstance("lufthansa2.jpg");
        img.setAlignment(Image.MIDDLE |Image.TEXTWRAP);
        p1.add(img);
        d.add(p1);
        //d.add(new Paragraph("DESTINACIJE"));


        Paragraph p2 = new Paragraph("Destinacije", titleFont);
        p2.setAlignment(com.itextpdf.text.Element.ALIGN_CENTER);            
        d.add(p2);
        d.add(new Paragraph(" "));

        Paragraph p3 = new Paragraph("Piloti", titleFont);
        p3.setAlignment(com.itextpdf.text.Element.ALIGN_CENTER);            
        d.add(p3);

        kreiranjeTabele(d, textFont, headerFont, "Pilot");
        d.close();
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (DocumentException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}

private void kreiranjeTabele(Document d, Font textFont, Font headerFont,
        String smjer) throws DocumentException {
    PdfPTable table = new PdfPTable(5);

    PdfPCell c1 = new PdfPCell(new Phrase("Drzava", headerFont));
    table.addCell(c1);

    PdfPCell c2 = new PdfPCell(new Phrase("Kompanija", headerFont));
    table.addCell(c2);

    PdfPCell c3 = new PdfPCell(new Phrase("Vlasnik", headerFont));
    table.addCell(c3);

    PdfPCell c4 = new PdfPCell(new Phrase("Broj Aviona", headerFont));
    table.addCell(c4);

    PdfPCell c5 = new PdfPCell(new Phrase("Godina", headerFont));
    table.addCell(c5);

    for (Avio s : avioni) {
        table.setWidthPercentage(110);

        // Paragraph p1 = new Paragraph(s.toString(),
        // textFont);Integer.toString(i)
        table.addCell(new Phrase(s.getDrzava(), textFont));
        table.addCell(new Phrase(s.getNazivKomp(), textFont));
        table.addCell(new Phrase(s.getVlasnik(), textFont));
        table.addCell(new Phrase(Integer.toString(s.getBrAviona()),
                textFont));
        table.addCell(new Phrase(Integer.toString(s.getGod()), textFont));

    }




    PdfPTable table1 = new PdfPTable(5);
    PdfPCell c11 = new PdfPCell(new Phrase("Kompanija", headerFont));
    table1.addCell(c11);

    PdfPCell c22 = new PdfPCell(new Phrase("Avion", headerFont));
    table1.addCell(c22);

    PdfPCell c33 = new PdfPCell(new Phrase("Datum", headerFont));
    table1.addCell(c33);

    PdfPCell c44 = new PdfPCell(new Phrase("Destinacija", headerFont));
    table1.addCell(c44);

    PdfPCell c55 = new PdfPCell(new Phrase("Trajanje leta", headerFont));
    table1.addCell(c55);
    for (Avio s1 : avioni) {
        table1.setWidthPercentage(110);
        table1.addCell(new Phrase(s1.getNazivKomp(), textFont));
        table1.addCell(new Phrase(s1.getModelAviona(), textFont));
        table1.addCell(new Phrase(s1.getDatum(), textFont));

    //  table1.addCell(new Phrase(s1.getDestinacije(), textFont));
        table1.addCell(new Phrase(s1.getLet(), textFont));

    }
    PdfPTable table2 = new PdfPTable(5);
    PdfPCell c111 = new PdfPCell(new Phrase("Pilot", headerFont));
    table2.addCell(c111);

    PdfPCell c222 = new PdfPCell(new Phrase("Drzava", headerFont));
    table2.addCell(c222);

    PdfPCell c333 = new PdfPCell(new Phrase("JMBG", headerFont));
    table2.addCell(c333);

    PdfPCell c444 = new PdfPCell(new Phrase("Iskustvo", headerFont));
    table2.addCell(c444);

    PdfPCell c555 = new PdfPCell(new Phrase("Avion", headerFont));
    table2.addCell(c555);
    for (Avio s2 : avioni) {
        table2.setWidthPercentage(110);
        table2.addCell(new Phrase(s2.getKapetan(), textFont));
        table2.addCell(new Phrase(s2.getDrzava(), textFont));
        table2.addCell(new Phrase(s2.getJMBG(), textFont));
        table2.addCell(new Phrase(s2.getIskustvo(), textFont));
        table2.addCell(new Phrase(s2.getModelAviona(), textFont));

    }

    d.add(table);
    d.newPage();        
    d.add(table1);
    d.newPage();        
    d.add(table2);
}

Hope someone can help. Thanks in advance.

  • i posted code ...Its not in english ... But at PDFPTable table1 is that problem with destinations. – thebluetrol Jun 25 '14 at 16:33
  • i need 10 strings from list in each row of column Destinacije as you see in code.But not the same 10 . – thebluetrol Jun 25 '14 at 16:40
  • The question still isn't clear. Why is this an iText question? If I understand it correctly, it's a 101 programmer's question. Unless there's some hidden problem that isn't mentioned. – Bruno Lowagie Jun 25 '14 at 16:47
  • Because output should be PDF table , no other problems only this if you saw above how output should look... I have like 30 companies and Destinacije column which is empty and should be filled with Strings from list and for each company i need 10 String ( 10 destinations). – thebluetrol Jun 25 '14 at 16:56
  • "if you saw above how output should look." Maybe that's the problem: it doesn't look like anything. You need to rephrase your question. In its current state, I doubt any one besides you understands what you're asking. I for one don't have a clue. I'm still wondering: what is the question? – Bruno Lowagie Jun 26 '14 at 05:35
  • Okey i have string list of those cities, i read those from excel file and need them to write into PDF TABEL. But problem is this table has 30 rows and each row should have 10 cities from list . I made it work but i always get same 10 cities in each row . It should go through list take ten write in pdf go to next ten write in next row. This is my output : http://www.dodaj.rs/f/2k/dr/2Qze6ZLb/screen-shot-2014-06-26-a.png You see every row has same city list and thats those first ten cities from list. – thebluetrol Jun 26 '14 at 06:09

0 Answers0