Multiple datetime from datagrid itextsharp PDF c#

  • 155 Views
  • Last Post 21 September 2018
Stylus STYLUS posted this 20 September 2018

I have solution and work fine  I need in line ,if (c == 2), to add multiple columns with date and time...some help?    

//Adding DataRow  
            foreach (DataGridViewRow row in mp_racun_listaDataGridView.Rows)  
            {  
                int c = 0;  
                foreach (DataGridViewCell cell in row.Cells)  
                {  
                    if (c == 2)  
                    {  
                        PdfPCell cell2 = new PdfPCell(new Phrase(cell.Value.ToString().Split(' ')[0], calibri));  
                        pdfTable.AddCell(cell2);  
                    }  
                    else  
                    {  
                        PdfPCell cell2 = new PdfPCell(new Phrase(cell.Value.ToString(), calibri));  
                        pdfTable.AddCell(cell2);  
                    }  
                    c++;  
                }  
            }  
Order By: Standard | Newest | Votes
admin posted this 20 September 2018

You can try

// a table with three columns
PdfPTable table = new PdfPTable(3);
// the cell object
PdfPCell cell;
// we add a cell with colspan 3
cell = new PdfPCell(new Phrase("Cell with colspan 3"));
cell.setColspan(3);
table.addCell(cell);
// now we add a cell with rowspan 2
cell = new PdfPCell(new Phrase("Cell with rowspan 2"));
cell.setRowspan(2);
table.addCell(cell);
// we add the four remaining cells with addCell()
table.addCell("row 1; cell 1");
table.addCell("row 1; cell 2");
table.addCell("row 2; cell 1");
table.addCell("row 2; cell 2");

I've created a table with 3 columns, then add cell into row

You can also get complicated and use a sub-table if you need more control

var subTable = new PdfPTable(new float[] { 10, 100 });                        
subTable.AddCell(new PdfPCell(new Phrase("First line")) { Colspan = 2, Border = 0 });
subTable.AddCell(new PdfPCell() { Border = 0 });
subTable.AddCell(new PdfPCell(new Phrase("Second line")) {  Border = 0 });
subTable.AddCell(new PdfPCell() { Border = 0 });
subTable.AddCell(new PdfPCell(new Phrase("Third line")) { Border = 0 });
subTable.AddCell(new PdfPCell(new Phrase("Fourth line")) { Colspan = 2, Border = 0 });
table.AddCell(subTable);
Stylus STYLUS posted this 20 September 2018

This is full code

 private void button9_Click(object sender, EventArgs e)
        {
            //Creating iTextSharp Table from the DataTable data
            //Console.WriteLine(drzaveDataGridView.ColumnCount); test za itext sharp
            PdfPTable pdfTable = new PdfPTable(mp_racun_listaDataGridView.ColumnCount);
            PdfPTable table = new PdfPTable(3);
            pdfTable.DefaultCell.Padding = 3;
            pdfTable.WidthPercentage = 100;
            pdfTable.HorizontalAlignment = Element.ALIGN_LEFT;
            pdfTable.DefaultCell.BorderWidth = 1;
            pdfTable.DefaultCell.BorderWidth = 1;
            float[] sirina = new float[] { 0f, 15f, 25f, 70f, 25f, 0f, 0f, 0f, 0f, 0f, 0f, 0f, 0f, 0f, 0f, 0f, 0f, 0f, 0f, 0f, 0f, 0f, 0f, 0f, 0f, 0f, 0f, 0f, 0f, 0f, 0f, 0f, 0f, 0f };
            pdfTable.SetWidths(sirina);


            BaseFont bfCalibri = BaseFont.CreateFont("c:\\windows\\fonts\\calibri.ttf", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
            iTextSharp.text.Font calibri = new iTextSharp.text.Font(bfCalibri, 9);
            iTextSharp.text.Font calibri10bold3 = new iTextSharp.text.Font(bfCalibri, 9, iTextSharp.text.Font.BOLD, iTextSharp.text.BaseColor.WHITE);
            //iTextSharp.text.Font calibriSubTitle2 = new iTextSharp.text.Font(bfCalibri, 12);

            //Adding Header row
            foreach (DataGridViewColumn column in mp_racun_listaDataGridView.Columns)
            {
                PdfPCell cell = new PdfPCell(new Phrase(column.HeaderText, calibri10bold3));
                cell.HorizontalAlignment = Element.ALIGN_CENTER;
                cell.VerticalAlignment = Element.ALIGN_CENTER;
                cell.BackgroundColor = new iTextSharp.text.BaseColor(243, 70, 5);
                pdfTable.AddCell(cell);
            }

            //Adding DataRow
            foreach (DataGridViewRow row in mp_racun_listaDataGridView.Rows)
            {
                int c = 0;
                foreach (DataGridViewCell cell in row.Cells)
                {
                    if (c == 2)
                    {
                        PdfPCell cell2 = new PdfPCell(new Phrase(cell.Value.ToString().Split(' ')[0], calibri));
                        pdfTable.AddCell(cell2);
                    }
                    else
                    {
                        PdfPCell cell2 = new PdfPCell(new Phrase(cell.Value.ToString(), calibri));
                        pdfTable.AddCell(cell2);
                    }
                    c++;
                }
            }


  string folderPath = "C:\\PDFs\\";
                            if (!Directory.Exists(folderPath))
                            {
                                Directory.CreateDirectory(folderPath);
                            }
                            using (FileStream stream = new FileStream(folderPath + "Lista MP računa.pdf", FileMode.Create))
                            {
                                iTextSharp.text.Font calibriTitle = new iTextSharp.text.Font(bfCalibri, 14, iTextSharp.text.Font.BOLD);
                                iTextSharp.text.Font calibriSubTitle = new iTextSharp.text.Font(bfCalibri, 12);
                                iTextSharp.text.Font calibriSubTitlebold = new iTextSharp.text.Font(bfCalibri, 14, iTextSharp.text.Font.BOLD);
                                iTextSharp.text.Font calibri10bold = new iTextSharp.text.Font(bfCalibri, 10, iTextSharp.text.Font.BOLD);
                                iTextSharp.text.Font calibri22bold = new iTextSharp.text.Font(bfCalibri, 22, iTextSharp.text.Font.BOLDITALIC);
                                iTextSharp.text.Font calibrinaslov = new iTextSharp.text.Font(bfCalibri, 16, iTextSharp.text.Font.BOLD);
                                Document pdfDoc = new Document(PageSize.A4, 27f, 10f, 10f, 30f);
                                // PdfWriter.GetInstance(pdfDoc, stream);
                                PdfWriter writer = PdfWriter.GetInstance(pdfDoc, stream);
                                pdfDoc.Open();
                                pdfDoc.Add(new ListItem(korisnik_programa, calibriTitle));
                                pdfDoc.Add(new ListItem(adresa, calibriSubTitle));
                                pdfDoc.Add(new ListItem(mesto, calibriSubTitle));
                                pdfDoc.Add(new ListItem("JIB:" + " " + jib2, calibriSubTitle));
                                pdfDoc.Add(new ListItem("PIB:" + " " + pib2, calibriSubTitle));
                                pdfDoc.Add(new ListItem("Žiro-racun:" + " " + ziro_racun2, calibriSubTitle));
                                pdfDoc.Add(new ListItem("E-mail:" + " " + e_mail, calibriSubTitle));
                                pdfDoc.Add(new ListItem(" "));
                                // pdfDoc.Add(new ListItem(" "));
                                pdfDoc.Add(new ListItem("                                                    Lista maloprodajnih računa ", calibrinaslov));
                                pdfDoc.Add(new ListItem(" "));
                                // pdfDoc.Add(new ListItem(" "));
                                pdfDoc.Add(new ListItem(" "));




                                PdfContentByte cb = writer.DirectContent;
                                PdfContentByte cb1 = writer.DirectContent;
                                cb.SetColorStroke(new BaseColor(41, 128, 185));// boja linija


                                iTextSharp.text.Rectangle fakturisao = new iTextSharp.text.Rectangle(pdfDoc.PageSize.Width - 140f, 120f, pdfDoc.PageSize.Width - 520f, 50f);
                                iTextSharp.text.Rectangle primio = new iTextSharp.text.Rectangle(pdfDoc.PageSize.Width - 140f, 120f, pdfDoc.PageSize.Width - 60f, 50f);
                                iTextSharp.text.Rectangle otpremio = new iTextSharp.text.Rectangle(pdfDoc.PageSize.Width - 140f, 120f, pdfDoc.PageSize.Width - 330f, 50f);
                                iTextSharp.text.Rectangle ukupno_za_uplatu = new iTextSharp.text.Rectangle(pdfDoc.PageSize.Width - 140f, 120f, pdfDoc.PageSize.Width - 462f, 590f);
                                //     iTextSharp.text.Rectangle slogan_korisnika2 = new iTextSharp.text.Rectangle(pdfDoc.PageSize.Width - 340f, 10f, pdfDoc.PageSize.Width - 20f, 10f);



                                //cb.SetColorStroke(new CMYKColor(1f, 0f, 0f, 0f));// boja linija
                                // cb.SetColorStroke(new BaseColor(41,128,185));// boja linija
                                // cb.SetColorFill(new CMYKColor(0f, 0f, 1f, 0f));

                                cb1.SetLineWidth(2);
                                cb1.RoundRectangle(20f, 691f, 560f, 140f, 10f); // zaglavlje rectangle
                                cb1.Stroke();







                                cb.SetLineWidth(1);
                                cb.MoveTo(20f, 681f);  //linija gornja zaglavlje
                                cb.LineTo(580f, 681f);
                                cb.Stroke();

                                cb.SetLineWidth(1);
                                cb.MoveTo(20f, 652f); // linija donja zaglavlje
                                cb.LineTo(580f, 652f);
                                cb.Stroke();




                                cb.SetLineWidth(0.5);
                                cb.MoveTo(20f, 20f); // linija donja slogan firme
                                cb.LineTo(585f, 20f);
                                cb.Stroke();


                                pdfDoc.Add(pdfTable);
                                pdfDoc.Add(table);



                                cb.SetLineWidth(0.5);
                                cb.MoveTo(20f, 20f); // linija donja slogan firme
                                cb.LineTo(585f, 20f);
                                cb.Stroke();
                                //     cb.SetLineWidth(1.5);
                                //   cb.MoveTo(5f, 5f);   //linija korisnik programa
                                //   cb.LineTo(5f, 820f);
                                //   cb.Stroke();

                                pdfDoc.Close();
                                stream.Close();
                                System.Diagnostics.Process.Start(folderPath + "Lista MP računa.pdf");
                            }
                        }
                    }
                }
            }
        }
Stylus STYLUS posted this 20 September 2018

This code working fine, just is proble if I have two date columns (sqlbd type (date)), need to show in pdf have this:

18.08.2018  00:00:00

When put this code

foreach (DataGridViewRow row in mp_racun_listaDataGridView.Rows)
            {
                int c = 0;
                foreach (DataGridViewCell cell in row.Cells)
                {
                    if (c == 2)
                    {
                        PdfPCell cell2 = new PdfPCell(new Phrase(cell.Value.ToString().Split(' ')[0], calibri));
                        pdfTable.AddCell(cell2);
                    }
                    else
                    {
                        PdfPCell cell2 = new PdfPCell(new Phrase(cell.Value.ToString(), calibri));
                        pdfTable.AddCell(cell2);
                    }
                    c++;
                }
            }

I have good solution   18.08.2018.

Problem is when have three date columns, I try this

if (c == 2 && c==8 && c=10)

but don't work...just work for one column...some help?

admin posted this 21 September 2018

I think you can find cell index as the following

for (int i = 0; i < dataGridView1.Rows.Count; i++)
{
    for (int c = 0; c < dataGridView1.Columns.Count; c++)
    {
        PdfPCell cell2 = new PdfPCell(new Phrase(dataGridView1.Rows[i].Cells[c].Value.ToString(), calibri));
        pdfTable.AddCell(cell2);
    }
}
Stylus STYLUS posted this 21 September 2018

Thank you for help...my solution work...(full code), for 1 column.

If I have multiple column dont work. That I need help

i try this and app duplicate some columns

foreach (DataGridViewRow row in mp_racun_listaDataGridView.Rows)
            {
                int c = 0;
                foreach (DataGridViewCell cell in row.Cells)
                {
                    if (c == 2)
                    {
                        PdfPCell cell2 = new PdfPCell(new Phrase(cell.Value.ToString().Split(' ')[0], calibri));
                        pdfTable.AddCell(cell2);
                    }
                    else
                    {
                        PdfPCell cell2 = new PdfPCell(new Phrase(cell.Value.ToString(), calibri));
                        pdfTable.AddCell(cell2);
                    }
                    c++;
                }


int d = 0;
                foreach (DataGridViewCell cell in row.Cells)
                {
                    if (d == 3)
                    {
                        PdfPCell cell2 = new PdfPCell(new Phrase(cell.Value.ToString().Split(' ')[0], calibri));
                        pdfTable.AddCell(cell2);
                    }
                    else
                    {
                        PdfPCell cell2 = new PdfPCell(new Phrase(cell.Value.ToString(), calibri));
                        pdfTable.AddCell(cell2);
                    }
                    d++;
                }
            }

and try this

Nothing happened

int c = 0;
int d = 0;
                foreach (DataGridViewCell cell in row.Cells)
                {
                    if (c == 2 && d==3)
                    {
                        PdfPCell cell2 = new PdfPCell(new Phrase(cell.Value.ToString().Split(' ')[0], calibri));
                        pdfTable.AddCell(cell2);
                    }
                    else
                    {
                        PdfPCell cell2 = new PdfPCell(new Phrase(cell.Value.ToString(), calibri));
                        pdfTable.AddCell(cell2);
                    }
                    c++;
                    d++;
                }
admin posted this 21 September 2018

You should get the index cell instead of increasing c++; d++; variables

Close