Goran Bibic

Goran Bibic

  • 482
  • 2.9k
  • 197.8k

Two Column Value Datagrid In One Column Table Itextsharp C#

Jun 15 2021 11:28 AM

My datagrid

Result:

MY part code need to modify:

//Adding Header row
foreach (DataGridViewColumn column in ItemsDataGridView.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); //255, 165, 61 //243, 70, 5
    pdfTable.AddCell(cell);
}

//Adding DataRow
foreach (DataGridViewRow row in ItemsDataGridView.Rows)
{
    foreach (DataGridViewCell cell in row.Cells)
    {
        PdfPCell cell2 = new PdfPCell(new Phrase(cell.Value.ToString(), calibri2));
        cell2.HorizontalAlignment = Element.ALIGN_RIGHT;
        cell2.VerticalAlignment = Element.ALIGN_MIDDLE;
        pdfTable.AddCell(cell2);
    }
}

Complete code:

//Creating iTextSharp Table from the DataTable data
//Console.WriteLine(drzaveDataGridView.ColumnCount); test za itext sharp
PdfPTable pdfTable = new PdfPTable(ItemsDataGridView.ColumnCount);

pdfTable.DefaultCell.Padding = 2;   //odstojanje od linija unutar tabele
pdfTable.WidthPercentage = 100;
pdfTable.HorizontalAlignment = Element.ALIGN_LEFT;  //datagreed tabela
pdfTable.DefaultCell.BorderWidth = 1;

float[] sirina = new float[] { 0f, 20f, 35f, 0f, 0f, 135f, 30f, 40f, 0f, 50f, 42f, 40f, 35f, 40f, 40f, 50f, 50f, 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.BOLD);
iTextSharp.text.Font calibrinormal = new iTextSharp.text.Font(bfCalibri, 9, iTextSharp.text.Font.NORMAL);
iTextSharp.text.Font calibri10bold2 = new iTextSharp.text.Font(bfCalibri, 9, iTextSharp.text.Font.BOLD, iTextSharp.text.BaseColor.BLACK);
iTextSharp.text.Font calibri10bold3 = new iTextSharp.text.Font(bfCalibri, 8, iTextSharp.text.Font.NORMAL, iTextSharp.text.BaseColor.WHITE);
iTextSharp.text.Font calibri2 = new iTextSharp.text.Font(bfCalibri, 7, iTextSharp.text.Font.ITALIC);

//Adding Header row
foreach (DataGridViewColumn column in ItemsDataGridView.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); //255, 165, 61 //243, 70, 5
    pdfTable.AddCell(cell)
}
//Adding DataRow
foreach (DataGridViewRow row in ItemsDataGridView.Rows)
{
    foreach (DataGridViewCell cell in row.Cells)
    {
        PdfPCell cell2 = new PdfPCell(new Phrase(cell.Value.ToString(), calibri2));
        cell2.HorizontalAlignment = Element.ALIGN_RIGHT;
        cell2.VerticalAlignment = Element.ALIGN_MIDDLE;
        pdfTable.AddCell(cell2);
    }
}
PdfPTable pdfTable2 = new PdfPTable(2);
pdfTable2.SetWidths(new int[] { 1, 2 }); // kolone
pdfTable2.DefaultCell.Padding = 1;   //odstojanje od linija unutar tabele
pdfTable2.WidthPercentage = 29.62f;
pdfTable2.HorizontalAlignment = Element.ALIGN_RIGHT;
pdfTable2.DefaultCell.BorderWidth = .5f;

// float[] sirina2 = new float[] { 75.59f, 85f };
float[] sirina2 = new float[] { 80f, 100f };
pdfTable2.SetWidths(sirina2);

//   Next we need to add rows and columns to the above table object.This can be added by using the AddCell method of the above table object.

//pdfTable2.AddCell("IZNOS BEZ PDV:");
// pdfTable2.AddCell(iznos_bez_pdvTextBox.Text.ToString() + " KM");
// pdfTable2.AddCell("IZNOS PDV");

//pdfTable2.AddCell(pdvTextBox1.Text.ToString() + " KM");
// pdfTable2.AddCell("UKUPNO");
// pdfTable2.AddCell(iznos_sa_pdvTextBox1.Text.ToString() + " KM");

// PdfPCell cellid = new PdfPCell(new Phrase("ID", calibri));
// pdfTable2.AddCell(cellid);
// cellid.HorizontalAlignment = Element.ALIGN_RIGHT;
// cellid.BackgroundColor = BaseColor.LIGHT_GRAY;
// cellid.BorderWidth = 1;
// cellid.BorderColor = BaseColor.BLUE;
// cellid.Padding = 5;
// pdfTable2.AddCell(cellid);
// PdfPCell pdv_cell_pdf = new PdfPCell(new Phrase("PDV: ", calibri));
// pdfTable2.AddCell(pdv_cell_pdf);
//   pdv_cell_pdf.HorizontalAlignment = Element.ALIGN_RIGHT;
// pdv_cell_pdf.BackgroundColor = BaseColor.GREEN;
// pdv_cell_pdf.BorderWidth = 0.5f;
//  pdv_cell_pdf.BorderColor = BaseColor.BLUE;
// pdv_cell_pdf.Padding = 5;
// pdfTable2.AddCell("Ukupno bez PDV");
PdfPCell ukupno_bez_pdv_cell2_pdf = new PdfPCell(new Phrase("Ukupno bez PDV:", calibri10bold2));
PdfPCell ukupno_bez_pdv_cell_pdf = new PdfPCell(new Phrase(IznosSaRabatomBezPdvTextBox.Text.ToString() + " KM", calibri10bold2));
ukupno_bez_pdv_cell2_pdf.HorizontalAlignment = Element.ALIGN_RIGHT;
ukupno_bez_pdv_cell_pdf.HorizontalAlignment = Element.ALIGN_RIGHT;
// ukupno_bez_pdv_cell_pdf.BackgroundColor = new iTextSharp.text.BaseColor(41, 128, 185);
//   ukupno_bez_pdv_cell2_pdf.BackgroundColor = new iTextSharp.text.BaseColor(41, 128, 185);
// ukupno_bez_pdv_cell2_pdf.BorderWidth = 1;
//ukupno_bez_pdv_cell_pdf.BorderColor = BaseColor.BLACK;
//ukupno_bez_pdv_cell2_pdf.Padding = 5;
// pdfTable2.AddCell(ukupno_bez_pdv_cell_pdf);
pdfTable2.AddCell(ukupno_bez_pdv_cell2_pdf);
pdfTable2.AddCell(ukupno_bez_pdv_cell_pdf);
PdfPCell pdv_cell5_pdf = new PdfPCell(new Phrase("PDV: ", calibri10bold2));
PdfPCell pdv_cell6_pdf = new PdfPCell(new Phrase(PdvTextBox.Text.ToString() + " KM", calibri10bold2));
pdv_cell5_pdf.HorizontalAlignment = Element.ALIGN_RIGHT;
pdv_cell6_pdf.HorizontalAlignment = Element.ALIGN_RIGHT;
// pdv_cell5_pdf.BackgroundColor = new iTextSharp.text.BaseColor(41, 128, 185);
// pdv_cell6_pdf.BackgroundColor = new iTextSharp.text.BaseColor(41, 128, 185);
// pdv_cell6_pdf.BorderWidth = 3;
// pdv_cell6_pdf.BorderColor = BaseColor.BLUE;
// pdv_cell6_pdf.Padding = 5;
// pdfTable2.AddCell(pdv_cell6_pdf);

pdfTable2.AddCell(pdv_cell5_pdf);
pdfTable2.AddCell(pdv_cell6_pdf);

PdfPCell ukupno_sa_pdv_cell3_pdf = new PdfPCell(new Phrase("UKUPNO: ", calibri10bold2));
PdfPCell ukupno_sa_pdv_cell4_pdf = new PdfPCell(new Phrase(IznosSaPdvTextBox.Text.ToString() + " KM", calibri10bold2));

ukupno_sa_pdv_cell3_pdf.HorizontalAlignment = Element.ALIGN_RIGHT;
ukupno_sa_pdv_cell3_pdf.VerticalAlignment = Element.ALIGN_MIDDLE;
ukupno_sa_pdv_cell4_pdf.HorizontalAlignment = Element.ALIGN_RIGHT;
ukupno_sa_pdv_cell4_pdf.VerticalAlignment = Element.ALIGN_MIDDLE;
// ukupno_sa_pdv_cell3_pdf.BackgroundColor = new iTextSharp.text.BaseColor(41, 128, 185);
// ukupno_sa_pdv_cell4_pdf.BackgroundColor = new iTextSharp.text.BaseColor(41, 128, 185);
//  ukupno_sa_pdv_cell4_pdf.BorderWidth = 3;
ukupno_sa_pdv_cell4_pdf.BorderColor = BaseColor.BLACK;
ukupno_sa_pdv_cell3_pdf.Padding = 1;
ukupno_sa_pdv_cell4_pdf.Padding = 1;

pdfTable2.AddCell(ukupno_sa_pdv_cell3_pdf);
pdfTable2.AddCell(ukupno_sa_pdv_cell4_pdf);

// tabela za obracun poreza

PdfPTable pdvTable = new PdfPTable(2);
pdvTable.SetWidths(new int[] { 1, 2 }); // kolone
pdvTable.DefaultCell.Padding = 1;   //odstojanje od linija unutar tabele
pdvTable.WidthPercentage = 30f;
pdvTable.HorizontalAlignment = Element.ALIGN_LEFT;
pdvTable.DefaultCell.BorderWidth = .5f;

float[] sirinapdv = new float[] { 75.59f, 85f };
pdvTable.SetWidths(sirinapdv);
//header
PdfPCell cellpdv = new PdfPCell(new Phrase("Kalkulacija poreza", calibri10bold3));
cellpdv.BackgroundColor = new iTextSharp.text.BaseColor(41, 128, 185); //255, 165, 61 //243, 70, 5
cellpdv.Colspan = 2;

cellpdv.HorizontalAlignment = 1; //0=Left, 1=Centre, 2=Right
cellpdv.VerticalAlignment = 1; //0=Left, 1=Centre, 2=Right
pdvTable.AddCell(cellpdv);
//   Next we need to add rows and columns to the above table object.This can be added by using the AddCell method of the above table object.

//pdvTable.AddCell("IZNOS BEZ PDV:");
// pdvTable.AddCell(iznos_bez_pdvTextBox.Text.ToString() + " KM");
// pdvTable.AddCell("IZNOS PDV");

//pdvTable.AddCell(pdvTextBox1.Text.ToString() + " KM");
// pdvTable.AddCell("UKUPNO");
// pdvTable.AddCell(iznos_sa_pdvTextBox1.Text.ToString() + " KM");

// PdfPCell cellid = new PdfPCell(new Phrase("ID", calibri));
// pdvTable.AddCell(cellid);
// cellid.HorizontalAlignment = Element.ALIGN_RIGHT;
// cellid.BackgroundColor = BaseColor.LIGHT_GRAY;
// cellid.BorderWidth = 1;
// cellid.BorderColor = BaseColor.BLUE;
// cellid.Padding = 5;
// pdvTable.AddCell(cellid);

// PdfPCell pdv_cell_pdf = new PdfPCell(new Phrase("PDV: ", calibri));
// pdvTable.AddCell(pdv_cell_pdf);
//   pdv_cell_pdf.HorizontalAlignment = Element.ALIGN_RIGHT;
// pdv_cell_pdf.BackgroundColor = BaseColor.GREEN;
// pdv_cell_pdf.BorderWidth = 0.5f;
//  pdv_cell_pdf.BorderColor = BaseColor.BLUE;
// pdv_cell_pdf.Padding = 5;
// pdvTable.AddCell("Ukupno bez PDV");

PdfPCell ukupno_bez_pdv_cell_pdf_pdv23 = new PdfPCell(new Phrase("Osnovica PDV:", calibri2));
PdfPCell ukupno_bez_pdv_cell_pdf_pdv24 = new PdfPCell(new Phrase(IznosSaRabatomBezPdvTextBox.Text.ToString() + " KM", calibri2));

ukupno_bez_pdv_cell_pdf_pdv23.HorizontalAlignment = Element.ALIGN_CENTER;
ukupno_bez_pdv_cell_pdf_pdv24.HorizontalAlignment = Element.ALIGN_CENTER;
// ukupno_bez_pdv_cell_pdf_pdv.BackgroundColor = new iTextSharp.text.BaseColor(41, 128, 185);
//  ukupno_bez_pdv_cell_pdf_pdv2.BackgroundColor = new iTextSharp.text.BaseColor(41, 128, 185);
// ukupno_bez_pdv_cell_pdf_pdv.BorderWidth = 1;
//ukupno_bez_pdv_cell_pdf_pdv.BorderColor = BaseColor.BLACK;
//ukupno_bez_pdv_cell_pdf_pdv.Padding = 5;
// pdvTable.AddCell(ukupno_bez_pdv_cell_pdf);
pdvTable.AddCell(ukupno_bez_pdv_cell_pdf_pdv23);
pdvTable.AddCell(ukupno_bez_pdv_cell_pdf_pdv24);
PdfPCell pdv_cell7_pdf = new PdfPCell(new Phrase("Iznos PDV: ", calibri2));
PdfPCell pdv_cell8_pdf = new PdfPCell(new Phrase(PdvTextBox.Text.ToString() + " KM", calibri2));
pdv_cell7_pdf.HorizontalAlignment = Element.ALIGN_CENTER;
pdv_cell8_pdf.HorizontalAlignment = Element.ALIGN_CENTER;
// pdv_cell5_pdf.BackgroundColor = new iTextSharp.text.BaseColor(41, 128, 185);
// pdv_cell6_pdf.BackgroundColor = new iTextSharp.text.BaseColor(41, 128, 185);
// pdv_cell6_pdf.BorderWidth = 3;
// pdv_cell6_pdf.BorderColor = BaseColor.BLUE;
// pdv_cell6_pdf.Padding = 5;
// pdfTable2.AddCell(pdv_cell6_pdf);

pdvTable.AddCell(pdv_cell7_pdf);
pdvTable.AddCell(pdv_cell8_pdf);

PdfPCell ukupno_sa_pdv_cell9_pdf = new PdfPCell(new Phrase("Ukupno sa PDV: ", calibri2));
PdfPCell ukupno_sa_pdv_cell10_pdf = new PdfPCell(new Phrase(IznosSaPdvTextBox.Text.ToString() + " KM", calibri2));

ukupno_sa_pdv_cell9_pdf.HorizontalAlignment = Element.ALIGN_CENTER;
ukupno_sa_pdv_cell10_pdf.VerticalAlignment = Element.ALIGN_CENTER;
ukupno_sa_pdv_cell10_pdf.HorizontalAlignment = Element.ALIGN_CENTER;
ukupno_sa_pdv_cell9_pdf.VerticalAlignment = Element.ALIGN_CENTER;
// ukupno_sa_pdv_cell3_pdf.BackgroundColor = new iTextSharp.text.BaseColor(41, 128, 185);
// ukupno_sa_pdv_cell4_pdf.BackgroundColor = new iTextSharp.text.BaseColor(41, 128, 185);
//  ukupno_sa_pdv_cell4_pdf.BorderWidth = 3;
ukupno_sa_pdv_cell10_pdf.BorderColor = BaseColor.BLACK;
ukupno_sa_pdv_cell10_pdf.Padding = 1;
ukupno_sa_pdv_cell9_pdf.Padding = 1;

pdvTable.AddCell(ukupno_sa_pdv_cell9_pdf);
pdvTable.AddCell(ukupno_sa_pdv_cell10_pdf);

string korisnik_programa = "";
using (SqlConnection openCon = new SqlConnection(Con))
{
    SqlDataReader reader;
    SqlCommand cmd = new SqlCommand();
    cmd.CommandText = "select puno_ime from podaci_o_korisniku";
    cmd.Connection = openCon;
    openCon.Open();
    reader = cmd.ExecuteReader();

    while (reader.Read())
    {
        korisnik_programa = reader[0].ToString();

    }
    openCon.Close();
}

string adresa = "";
using (SqlConnection openCon = new SqlConnection(Con))
{
    SqlDataReader reader;
    SqlCommand cmd2 = new SqlCommand();
    cmd2.CommandText = "select adresa from podaci_o_korisniku";
    cmd2.Connection = openCon;
    openCon.Open();
    reader = cmd2.ExecuteReader();

    while (reader.Read())
    {
        adresa = reader[0].ToString();

    }
    openCon.Close();
}

string jib2 = "";
using (SqlConnection openCon = new SqlConnection(Con))
{
    SqlDataReader reader;
    SqlCommand cmd3 = new SqlCommand();
    cmd3.CommandText = "select jib2 from podaci_o_korisniku";
    cmd3.Connection = openCon;
   openCon.Open();
   reader = cmd3.ExecuteReader();

   while (reader.Read())
   {
       jib2 = reader[0].ToString();

   }
   openCon.Close();

}

string pib2 = "";
using (SqlConnection openCon2 = new SqlConnection(Con))
{
    SqlDataReader reader2;
    SqlCommand cmd4 = new SqlCommand();
    cmd4.CommandText = "select pib2 from podaci_o_korisniku";
    cmd4.Connection = openCon2;
    openCon2.Open();
    reader2 = cmd4.ExecuteReader();

    while (reader2.Read())
    {
        pib2 = reader2[0].ToString();

    }
    openCon2.Close();
}

string mesto = "";
using (SqlConnection openCon3 = new SqlConnection(Con))
{
    SqlDataReader reader3;
    SqlCommand cmd5 = new SqlCommand();
    cmd5.CommandText = "select mesto from podaci_o_korisniku";
    cmd5.Connection = openCon3;
    openCon3.Open();
    reader3 = cmd5.ExecuteReader();

    while (reader3.Read())
    {
        mesto = reader3[0].ToString();

    }
    openCon3.Close();
}

string ziro_racun2 = "";
using (SqlConnection openCon6 = new SqlConnection(Con))
{
    SqlDataReader reader6;
    SqlCommand cmd6 = new SqlCommand();
    cmd6.CommandText = "select ziro_racun2 from podaci_o_korisniku";
    cmd6.Connection = openCon6;
    openCon6.Open();
    reader6 = cmd6.ExecuteReader();

    while (reader6.Read())
    {
         ziro_racun2 = reader6[0].ToString();

     }
     openCon6.Close();

}
string e_mail = "";
using (SqlConnection openCon7 = new SqlConnection(Con))
{
    SqlDataReader reader7;
    SqlCommand cmd7 = new SqlCommand();
    cmd7.CommandText = "select e_mail from podaci_o_korisniku";
    cmd7.Connection = openCon7;
    openCon7.Open();
    reader7 = cmd7.ExecuteReader();

    while (reader7.Read())
    {
        e_mail = reader7[0].ToString();

    }
    openCon7.Close();
}

string redni_broj = "";
using (SqlConnection openCon8 = new SqlConnection(Con))
{
    SqlDataReader reader8;
    SqlCommand cmd8 = new SqlCommand();
    cmd8.CommandText = "select FORMAT(redni_broj, '00000') from dbo.mp_ulazni_racun_roba_lista where id=" + id_fakture;
        cmd8.Connection = openCon8;
        openCon8.Open();
        reader8 = cmd8.ExecuteReader();

        while (reader8.Read())
        {
            redni_broj = reader8[0].ToString();

        }
        openCon8.Close();
    }

string kupac = "";
using (SqlConnection openCon9 = new SqlConnection(Con))
{
    SqlDataReader reader9;
    SqlCommand cmd9 = new SqlCommand();
    cmd9.CommandText = "select poslovni_partner from dbo.mp_ulazni_racun_roba_lista where id=" + id_fakture;
    cmd9.Connection = openCon9;
    openCon9.Open();
    reader9 = cmd9.ExecuteReader();

    while (reader9.Read())
    {
        kupac = reader9[0].ToString();

    }
    openCon9.Close();

}

string datum = "";
using (SqlConnection openCon10 = new SqlConnection(Con))
{
    SqlDataReader reader10;
    SqlCommand cmd10 = new SqlCommand();
    // cmd10.CommandText = "select datum from mp_racun_lista where id=" + id_fakture;
    cmd10.CommandText = "SELECT CONVERT(VARCHAR(10), datum , 104) from dbo.mp_ulazni_racun_roba_lista where id=" + id_fakture;
    cmd10.Connection = openCon10;
    openCon10.Open();
    reader10 = cmd10.ExecuteReader();

    while (reader10.Read())
    {
        datum = reader10[0].ToString();

    }
    openCon10.Close();
}


string valuta_placanja_pdf = "";
using (SqlConnection openCon11 = new SqlConnection(Con))
{
    SqlDataReader reader11;
    SqlCommand cmd11 = new SqlCommand();
    cmd11.CommandText = "SELECT CONVERT(VARCHAR(10), valuta_placanja , 104) AS [DD.MM.YYYY] from .mp_ulazni_racun_roba_lista where id=" + id_fakture;
    cmd11.Connection = openCon11;
    openCon11.Open();
    reader11 = cmd11.ExecuteReader();

    while (reader11.Read())
    {
        valuta_placanja_pdf = reader11[0].ToString();

    }
    openCon11.Close();
}

//string mesto_izdavanja_pdf = "";
//using (SqlConnection openCon12 = new SqlConnection(con))
//{
//    SqlDataReader reader12;
//    SqlCommand cmd12 = new SqlCommand();
//    cmd12.CommandText = "select mesto_izdavanja from dbo.mp_ulazni_racun_roba_lista where id=" + id_fakture;
//    cmd12.Connection = openCon12;
//    openCon12.Open();
//    reader12 = cmd12.ExecuteReader();

//    while (reader12.Read())
//    {
//        mesto_izdavanja_pdf = reader12[0].ToString();

//    }
//    openCon12.Close();
//}

//string fakturisao_pdf = "";
//using (SqlConnection openCon13 = new SqlConnection(con))
//{
//    SqlDataReader reader13;
//    SqlCommand cmd13 = new SqlCommand();
//    cmd13.CommandText = "select fakturisao from dbo.mp_ulazni_racun_roba_lista where id=" + id_fakture;
//    cmd13.Connection = openCon13;
//    openCon13.Open();
//    reader13 = cmd13.ExecuteReader();

//    while (reader13.Read())
//    {
//        fakturisao_pdf = reader13[0].ToString();

//    }
//    openCon13.Close();
//}

string slogan_korisnika = "";
using (SqlConnection openCon14 = new SqlConnection(Con))
{
    SqlDataReader reader14;
    SqlCommand cmd14 = new SqlCommand();
    cmd14.CommandText = "select slogan_korisnika from podaci_o_korisniku";
    cmd14.Connection = openCon14;
    openCon14.Open();
    reader14 = cmd14.ExecuteReader();

    while (reader14.Read())
    {
        slogan_korisnika = reader14[0].ToString();

    }
    openCon14.Close();
}

string jib_pdf = "";
using (SqlConnection openCon15 = new SqlConnection(Con))
{
    SqlDataReader reader15;
    SqlCommand cmd15 = new SqlCommand();
    cmd15.CommandText = "select jib from dbo.mp_ulazni_racun_roba_lista where id=" + id_fakture;
    cmd15.Connection = openCon15;
    openCon15.Open();
    reader15 = cmd15.ExecuteReader();

    while (reader15.Read())
    {
        jib_pdf = reader15[0].ToString();

    }
    openCon15.Close();
}

string pib_pdf = "";
using (SqlConnection openCon16 = new SqlConnection(Con))
{
    SqlDataReader reader16;
    SqlCommand cmd16 = new SqlCommand();
    cmd16.CommandText = "select pib from dbo.mp_ulazni_racun_roba_lista where id=" + id_fakture;
    cmd16.Connection = openCon16;
    openCon16.Open();
    reader16 = cmd16.ExecuteReader();

    while (reader16.Read())
    {
        pib_pdf = reader16[0].ToString();

    }
    openCon16.Close();
}

//string postanski_broj_kupca_pdf = "";
//using (SqlConnection openCon17 = new SqlConnection(con))
//{
//    SqlDataReader reader17;
//    SqlCommand cmd17 = new SqlCommand();
//    cmd17.CommandText = "select postanski_broj_kupca from dbo.mp_ulazni_racun_roba_lista where id=" + id_fakture;
//    cmd17.Connection = openCon17;
//    openCon17.Open();
//    reader17 = cmd17.ExecuteReader();

//    while (reader17.Read())
//    {
//        postanski_broj_kupca_pdf = reader17[0].ToString();

//    }
//    openCon17.Close();
//}

string adresa_kupca_pdf = "";
using (SqlConnection openCon18 = new SqlConnection(Con))
{
    SqlDataReader reader18;
    SqlCommand cmd18 = new SqlCommand();
    cmd18.CommandText = "select adresa from dbo.mp_ulazni_racun_roba_lista where id=" + id_fakture;
    cmd18.Connection = openCon18;
    openCon18.Open();
    reader18 = cmd18.ExecuteReader();

    while (reader18.Read())
    {
        adresa_kupca_pdf = reader18[0].ToString();

    }
    openCon18.Close();
}

string mesto_kupca_pdf = "";
using (SqlConnection openCon19 = new SqlConnection(Con))
{
    SqlDataReader reader19;
    SqlCommand cmd19 = new SqlCommand();
       cmd19.CommandText = "select mesto from dbo.mp_ulazni_racun_roba_lista where id=" + id_fakture;
       cmd19.Connection = openCon19;
       openCon19.Open();
       reader19 = cmd19.ExecuteReader();

       while (reader19.Read())
       {
           mesto_kupca_pdf = reader19[0].ToString();

       }
       openCon19.Close();
}

string drzava_kupca_pdf = "";
using (SqlConnection openCon20 = new SqlConnection(Con))
{
    SqlDataReader reader20;
    SqlCommand cmd20 = new SqlCommand();
    cmd20.CommandText = "select drzava from dbo.mp_ulazni_racun_roba_lista where id=" + id_fakture;
    cmd20.Connection = openCon20;
    openCon20.Open();
    reader20 = cmd20.ExecuteReader();

    while (reader20.Read())
    {
        drzava_kupca_pdf = reader20[0].ToString();

    }
    openCon20.Close();
}

byte[] Memorandum = null;

using (SqlConnection openCon21 = new SqlConnection(Con))
using (SqlCommand cmd21 = new SqlCommand())
{
    cmd21.CommandText = "select memorandum from podaci_o_korisniku";
    cmd21.Connection = openCon21;
    openCon21.Open();

    using (SqlDataReader reader21 = cmd21.ExecuteReader())
    {
        if (reader21.Read() && !reader21.IsDBNull(0))
        {
            Memorandum = (byte[])reader21[0];
        }
        else
        {
            Memorandum = null;
        }
    }
}

if (Memorandum == null)
{
    MessageBox.Show("Ucitajte zaglavlje kako biste odštampali dokument!" + "\n" + "Zaglavlje možete ucitati u:" + "\n" + "Šifarnici /1500 Osnovni podaci / 1501 Podaci o korisniku ", "Informacija", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else
{
    iTextSharp.text.Image logo = iTextSharp.text.Image.GetInstance(Memorandum);

    //Exporting to PDF
    //drzava_kupca
    string folderPath = "C:\\Program files\\BSS\\PDFs\\";
    if (!Directory.Exists(folderPath))
    {
        Directory.CreateDirectory(folderPath);
    }
    using (FileStream stream = new FileStream(folderPath + "Ulazni racun - ROBA.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 calibriSubTitle2 = new iTextSharp.text.Font(bfCalibri, 10);
        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);
        Document pdfDoc = new Document(PageSize.A4, 20f, 15f, 10f, 130f);
        PdfWriter writer = PdfWriter.GetInstance(pdfDoc, stream);
        //  PdfWriter.GetInstance(pdfDoc, stream, writer);
        pdfDoc.Open();

        logo.ScaleAbsolute(550, 110);
        logo.SetAbsolutePosition(25, 700);
        pdfDoc.Add(logo);

        //pdfDoc.Add(new ListItem(" "));
        //pdfDoc.Add(new ListItem(" "));
        //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(" "));
        pdfDoc.Add(new ListItem(" "));
        pdfDoc.Add(new ListItem(" "));
        pdfDoc.Add(new ListItem(" "));
        pdfDoc.Add(new ListItem(" "));
        pdfDoc.Add(new ListItem(" "));
        pdfDoc.Add(new ListItem(" "));
        pdfDoc.Add(new ListItem(" "));
        pdfDoc.Add(new ListItem("                 Ulazni racun - ROBA                                                             Broj: " + DateTime.Now.Year + " - 2101 - " + redni_broj, calibriSubTitlebold));
        // pdfDoc.Add(new ListItem(" "));
        pdfDoc.Add(new ListItem(" "));
        pdfDoc.Add(new ListItem("                               Dokument - 2101                                                                                  Dobavljac: " + kupac, calibri10bold));
        pdfDoc.Add(new ListItem("                       Datum: " + datum + "                                                                                           JIB: " + jib_pdf, calibriSubTitle2));
        pdfDoc.Add(new ListItem("        Valuta placanja: " + valuta_placanja_pdf + "                                                                                         PIB: " + pib_pdf, calibriSubTitle2));
        pdfDoc.Add(new ListItem("                                                                                                                                            Adresa: " + adresa_kupca_pdf, calibriSubTitle2));
        pdfDoc.Add(new ListItem("                                                                                                                                             Mesto: " + mesto_kupca_pdf, calibriSubTitle2));
        pdfDoc.Add(new ListItem("                                                                                                                                             " + drzava_kupca_pdf, calibriSubTitle2));
        // pdfDoc.Add(new ListItem("                  Ugovor:   " + ugovorTextBox.Text + "                                                                                                           ", calibriSubTitle2));
        // pdfDoc.Add(new ListItem(" "));
        pdfDoc.Add(new ListItem(" "));
        pdfDoc.Add(new ListItem(" "));
        // pdfDoc.Add(new ListItem(" "));
        // pdfDoc.Add(pdfTable);
        //pdfDoc.Add(pdfTable);
        //pdfDoc.Add(pdfTable2);
        //pdfDoc.Add(pdvTable);
        // pdfDoc.Add(new ListItem("Broj fiskalnog racuna: " + fiskalni_brojTextBox.Text, calibri));
        pdfDoc.Add(new ListItem("", calibri));
        //pdfDoc.Add(new ListItem("Napomena: NISMO U SISTEMU PDV-a.", calibri));
        // pdfDoc.Add(new ListItem("Napomena: " + ugovorTextBox.Text, calibri));
        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 - 40f, 17f, pdfDoc.PageSize.Width - 130f, 50f);  //vedran mocic
        iTextSharp.text.Rectangle mp = new iTextSharp.text.Rectangle(pdfDoc.PageSize.Width - 140f, 75f, pdfDoc.PageSize.Width - 235f, 50f);  //mp
        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);
        // iTextSharp.text.Rectangle jib_pdf_ispis= new iTextSharp.text.Rectangle(pdfDoc.PageSize.Width - 140f, 120f, pdfDoc.PageSize.Width - 289f, 620f);
        // iTextSharp.text.Rectangle pib_pdf_ispis = new iTextSharp.text.Rectangle(pdfDoc.PageSize.Width - 100f, 120f, pdfDoc.PageSize.Width - 40f, 620f);
        cb1.SetLineWidth(2);
        cb1.RoundRectangle(17f, 548f, 260f, 100f, 10f); // podaci o placanju rectangle
        cb1.Stroke();
        cb1.SetLineWidth(2);
        cb1.RoundRectangle(320f, 548f, 260f, 100f, 10f); // kupac rectangle
        cb1.Stroke();
        //cb1.SetLineWidth(2);
        // cb1.RoundRectangle(130f, 548f, 150f, 30f, 10f); // ukupno za uplatu rectangle
        // cb1.Stroke();
        //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(10f, 691f, 570f, 140f, 10f); // zaglavlje rectangle
        cb1.Stroke();
        cb.SetLineWidth(1);
        cb.MoveTo(10f, 683f);  //linija gornja zaglavlje
        cb.LineTo(580f, 683f);
        cb.Stroke();
        cb.SetLineWidth(1);
        cb.MoveTo(10f, 659f); // linija donja zaglavlje
        cb.LineTo(580f, 659f);
        cb.Stroke();
        cb.SetLineWidth(0.5);
        cb1.RoundRectangle(415f, 30f, 160f, 60f, 10f);// primio rectangle
        cb1.Stroke();
        cb.SetLineWidth(1);
        cb.MoveTo(15f, 535f);// linija izmedju kupac rectangle i tabele
        cb.LineTo(585f, 535f);
        cb.Stroke();
        pdfDoc.Add(pdfTable);
        pdfDoc.Add(pdfTable2);
        pdfDoc.Add(pdvTable);
        //cb.SetLineWidth(0.5);
        //cb1.RoundRectangle(220f, 30f, 160f, 60f, 10f);// otpremio rectangle
        //cb1.Stroke();
        //cb.SetLineWidth(0.5);
        //cb.RoundRectangle(20f, 30f, 160f, 60f, 10f); // fakturisao rectangle
        //cb.Stroke();
        // You can add text inside this rectangle like this:
        ColumnText ct = new ColumnText(cb);
        ct.SetSimpleColumn(primio);
        ct.AddElement(new Paragraph("       Primio: ", calibri10bold));
        ct.Go();
        //ct.SetSimpleColumn(jib_pdf_ispis);
        //ct.AddElement(new Paragraph("            JIB:" + jib_pdf, calibri10bold));
        // ct.Go();
        //ct.SetSimpleColumn(pib_pdf_ispis);
        // ct.AddElement(new Paragraph("            PIB:" + pib_pdf, calibri10bold));
        // ct.Go();
        // ColumnText ct2 = new ColumnText(cb);
        // ct.SetSimpleColumn(fakturisao);
        // ct.AddElement(new Paragraph("Fakturisao: ", calibri10bold));
        // ct.AddElement(new Paragraph("Fakturisao: " + fakturisao_pdf, calibriSubTitle));
        // ct.Go();
        // ct.SetSimpleColumn(slogan_korisnika2); //slogan_korisnika text
        // ct.AddElement(new Paragraph(slogan_korisnika, calibri10bold));
        //    ct.Go();
        //ct.SetSimpleColumn(otpremio);// Vedran Mocic
        //ct.AddElement(new Paragraph(fakturisaoComboBox.Text, calibri10bold));
        //ct.Go();
        ct.SetSimpleColumn(mp);// Vedran Mocic
        ct.AddElement(new Paragraph("M.P.", calibri10bold));
        ct.Go();
        //ct.SetSimpleColumn(ukupno_za_uplatu);// otpremio text
        // ct.AddElement(new Paragraph(iznos_sa_pdvTextBox.Text.ToString(), calibri22bold));
        //ct.Go();
        //  pdfDoc.Add(pdfTable);
        //  pdfDoc.Add(pdfTable2);
        //cb.SetColorStroke(new BaseColor(41, 128, 185));// boja linija
        //pdfDoc.Add(pdfTable2);
        //cb.MoveTo(30f, 45f); // linija donja fakturisao
        // cb.LineTo(170f, 45f);
        // cb.Stroke();
        cb.MoveTo(425f, 45f); // linija donja primio
        cb.LineTo(565f, 45f);
        cb.Stroke();
        //cb.MoveTo(230f, 45f); // linija donja otpremio
        //cb.LineTo(370f, 45f);
        //cb.Stroke();
        cb.SetLineWidth(0.5);
        cb.MoveTo(20f, 20f); // linija donja slogan firme
        cb.LineTo(580f, 20f);
        cb.Stroke();
        //  cb.SetLineWidth(0.5);
        // cb.MoveTo(5f, 5f);   //linija korisnik programa
        // cb.LineTo(5f, 820f);
        //  cb.Stroke();
        pdfDoc.Close();
        stream.Close();
        System.Diagnostics.Process.Start(folderPath + "Ulazni racun - ROBA.pdf");
    }
}

Result I need:

Thank you


Answers (1)