Search code examples
itextpdfptable

Is there any easy way to create table elements in itextsharp


I get some values from database and set into itextsharp PdfPCell with title. Here is my some piece of code.

    var a = _db.persons.SingleOrDefault(m => m.id == Person.Id);

    var table = new PdfPTable(2) {WidthPercentage = 100};
    table.DefaultCell.Border = Rectangle.NO_BORDER;
    var columns = new[] { 50f, 50f };
    table.SetWidths(columns);


    var bt1a = new PdfPCell(new Phrase("Name, surname:", fontGeneralText));
    var bt2a = new PdfPCell(new Phrase("Personal Id:", fontGeneralText));
    var bt3a = new PdfPCell(new Phrase("Phone number:", fontGeneralText));
    var bt4a = new PdfPCell(new Phrase("Department:", fontGeneralText));
    var bt5a = new PdfPCell(new Phrase("University:", fontGeneralText));
    var bt6a = new PdfPCell(new Phrase("Faculty:", fontGeneralText));

    var bt1b = new PdfPCell(new Phrase(a.name_surname, fontBoldText));
    var bt2b = new PdfPCell(new Phrase(a.personal_id, fontGeneralText));
    var bt3b = new PdfPCell(new Phrase(a.phne_number, fontGeneralText));
    var bt4b = new PdfPCell(new Phrase(a.department, fontGeneralText));
    var bt5b = new PdfPCell(new Phrase(a.university, fontGeneralText));
    var bt6b = new PdfPCell(new Phrase(a.faculty, fontGeneralText));

    table.AddCell(bt1a);
    table.AddCell(bt1b);

    table.AddCell(bt2a);
    table.AddCell(bt2b);

    table.AddCell(bt3a);
    table.AddCell(bt3b);

    table.AddCell(bt4a);
    table.AddCell(bt4b);

    table.AddCell(bt5a);
    table.AddCell(bt5b);

    table.AddCell(bt6a);
    table.AddCell(bt6b);

    doc.Add(table);

But it is ver amateur coding style. Because I write same structure and change only parameters. Is there any way to automated method like using foreach etc.

I think it can be done by the way but I don't know after here:

    List<string> titles = new List<string>() {"Name, surname:", "Personal Id:", "Phone number:", "Department:", "University:", "Faculty:"};

    List<Phrase> datas = new List<Phrase>() { new Phrase(a.name_surname, fontGeneralText), new Phrase(a.personal_id, fontGeneralText), new Phrase(a.phone_number, fontGeneralText), new Phrase(a.department, fontGeneralText), new Phrase(a.university, fontGeneralText), new Phrase(a.faculty, fontGeneralText)};


    foreach (var titles in title)
    {
        MessageBox.Show(title);
    }

Solution

  • From the code example above, you already have titles and datas. So just create the correct font needed for those two collections - something like this:

    List<string> titles = new List<string>() 
    { 
        "Name, surname:", "Personal Id:", "Phone number:", 
        "Department:", "University:", "Faculty:" 
    };
    List<string> datas = new List<string>() 
    { 
        "0", "1", "2", "3", "4", "5"
    };
    Font timesBold = FontFactory.GetFont("Times-Roman", 8, Font.BOLD);
    Font timesNormal = FontFactory.GetFont("Times-Roman", 8, Font.NORMAL);
    

    Then pass those parameters to a method similar to this:

    // you should also verify titles and datas have same 'Count'
    public void FillTable(List<string> titles, List<string> datas, 
        Font titleFont, Font dataFont)
    {
        var table = new PdfPTable(2) { WidthPercentage = 100 };
        table.DefaultCell.Border = Rectangle.NO_BORDER;
        var columns = new[] { 50f, 50f };
        table.SetWidths(columns);
    
        using (FileStream stream = new FileStream(
            OUT_FILE,
            FileMode.Create,
            FileAccess.Write))
        {
            using (var document = new Document())
            {
                PdfWriter.GetInstance(document, stream);
                document.Open();
                for (int i = 0; i < titles.Count; ++i)
                {
                    table.AddCell(new PdfPCell(new Phrase(titles[i], titleFont)));
                    table.AddCell(new PdfPCell(new Phrase(datas[i], dataFont)));
                }
                document.Add(table);
            }
        }
    }