Click here to Skip to main content
16,016,537 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi. I want to export data in Listview in C# project using iTextSharp. I am beginner in C#, Please help me to solve this problem. The source code is as following: 


C#
using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
using System.Windows.Forms; 
using System.IO; 
using iTextSharp.text; 
using iTextSharp.text.pdf; 

namespace Loan_Calculator 
{ 
    public partial class Form1 : Form 
    { 
        public Form1() 
        { 
            InitializeComponent(); 

            this.listView1.ColumnWidthChanging += new ColumnWidthChangingEventHandler(listView1_ColumnWidthChanging); 
        } 

        void listView1_ColumnWidthChanging(object sender, ColumnWidthChangingEventArgs e) 
        { 
            Console.Write("Column Resizing"); 
            e.NewWidth = this.listView1.Columns[e.ColumnIndex].Width; 
            e.Cancel = true; 
        } 

        double kreditmeblegi; 
        double illikfaiz; 
        double fm; 
        double value1; 
        double ay; 
        double il; 
        double ayliqodenis; 
        double umumiodenis; 
        double esasborc; 
        double faiz; 
        double qaliqmebleg; 
        double odenilenfaiz; 
        int nomre; 

        private void button1_Click(object sender, EventArgs e)
        {
            if (comboBox2.Text == "ay")
                {
                    kreditmeblegi = Convert.ToDouble(textBox1.Text);
                    illikfaiz = Convert.ToDouble(textBox2.Text);
                    ay = Convert.ToDouble(textBox3.Text);

                    fm = illikfaiz / (12 * 100);

                    value1 = 1 - (1 / Math.Pow((1 + fm), ay));

                    ayliqodenis = (kreditmeblegi * fm) / value1;
                    umumiodenis = ayliqodenis * ay;
                    odenilenfaiz = umumiodenis - kreditmeblegi;

                    DateTime date = this.dateTimePicker1.Value.Date;
                    DateTime nyear = new DateTime();
                    string format = "dd / MM / yyyy";

                    qaliqmebleg = kreditmeblegi;

                    for (int i = 1; i <= ay; i++)
                    {
                        nomre = nomre + 1;
                        ListViewItem lvi = new ListViewItem(nomre.ToString());
                        nyear = date.AddMonths(i);
                        lvi.SubItems.Add(nyear.ToString(format));
                        lvi.SubItems.Add(ayliqodenis.ToString("N2"));
                        
                        faiz = qaliqmebleg * fm;
                        esasborc = ayliqodenis - faiz;
                        qaliqmebleg = qaliqmebleg - esasborc;

                        lvi.SubItems.Add(esasborc.ToString("N2"));
                        lvi.SubItems.Add(faiz.ToString("N2"));
                        lvi.SubItems.Add(qaliqmebleg.ToString("N2"));
                        
                        listView1.Items.Add(lvi);
                    }

                    button1.Enabled = false;
                }

                else
                {
                    kreditmeblegi = Convert.ToDouble(textBox1.Text);
                    illikfaiz = Convert.ToDouble(textBox2.Text);
                    il = Convert.ToDouble(textBox3.Text) * 12;

                    fm = illikfaiz / (12 * 100);

                    value1 = 1 - (1 / Math.Pow((1 + fm), il));

                    ayliqodenis = (kreditmeblegi * fm) / value1;
                    umumiodenis = ayliqodenis * il;
                    odenilenfaiz = umumiodenis - kreditmeblegi;

                    DateTime date = this.dateTimePicker1.Value.Date;
                    DateTime nyear = new DateTime();
                    string format = "dd / MM / yyyy";

                    qaliqmebleg = kreditmeblegi;

                    for (int j = 1; j <= il; j++)
                    {
                        nomre = nomre + 1;
                        ListViewItem lvi = new ListViewItem(nomre.ToString());
                        nyear = date.AddMonths(j);
                        lvi.SubItems.Add(nyear.ToString(format));
                        lvi.SubItems.Add(ayliqodenis.ToString("N2"));

                        faiz = qaliqmebleg * fm;
                        esasborc = ayliqodenis - faiz;
                        qaliqmebleg = qaliqmebleg - esasborc;

                        lvi.SubItems.Add(esasborc.ToString("N2"));
                        lvi.SubItems.Add(faiz.ToString("N2"));
                        lvi.SubItems.Add(qaliqmebleg.ToString("N2"));

                        listView1.Items.Add(lvi);
                    }

                    button1.Enabled = false;
                }

                if (Convert.ToDouble(textBox1.Text) == 0 || Convert.ToDouble(textBox2.Text) == 0 || Convert.ToDouble(textBox3.Text) == 0)
                {
                    MessageBox.Show("Sıfır daxil etmək olmaz!");
                    button1.Enabled = true;
                    listView1.Items.Clear();
                }

                label9.Text = ayliqodenis.ToString("N2") + " " + comboBox1.Text;
                label10.Text = umumiodenis.ToString("N2") + " " + comboBox1.Text;
                label12.Text = odenilenfaiz.ToString("N2") + " " + comboBox1.Text;            
        }
        

        private void Form1_Load(object sender, EventArgs e) 
        { 
            
        } 

        private void ExporttoPDFToolStripMenuItem_Click(object sender, EventArgs e) 
        { 
            FileStream fs = new FileStream("Loan Calculator.pdf", FileMode.Create, FileAccess.Write, FileShare.None); 
            Document doc = new Document(PageSize.A4); 
            PdfWriter writer = PdfWriter.GetInstance(doc, fs); 
            doc.Open(); 
            doc.Add(new Paragraph("Hello World")); 
            doc.Close(); 
        } 
    } 
} 


What I have tried:

I can export Hello world sentence to PDF, but I don't how to export Listview to PDF.
Posted
Updated 14-Mar-22 18:32pm

Please have a look at below code
C#
listView1.View = View.Details;
listView1.GridLines = true;
listView1.FullRowSelect = false;

//Add column header
listView1.Columns.Add("ProductName", 100);
listView1.Columns.Add("Price", 70);
listView1.Columns.Add("Quantity", 70);

//Add items in the listview
string[] arr = new string[4];
ListViewItem itm;

//Add first item
arr[0] = "product_1";
arr[1] = "100";
arr[2] = "10";
itm = new ListViewItem(arr);
listView1.Items.Add(itm);

//Add second item
arr[0] = "product_2";
arr[1] = "200";
arr[2] = "20";
itm = new ListViewItem(arr);
listView1.Items.Add(itm);



//Creating iTextSharp Table from the DataTable data
PdfPTable pdfTable = new PdfPTable(listView1.Columns.Count);
pdfTable.DefaultCell.Padding = 3;
pdfTable.WidthPercentage = 30;
pdfTable.HorizontalAlignment = Element.ALIGN_LEFT;
pdfTable.DefaultCell.BorderWidth = 1;

//Adding Header row
foreach (ColumnHeader column in listView1.Columns)
{
    PdfPCell cell = new PdfPCell(new Phrase(column.Text));
    pdfTable.AddCell(cell);
}

//Adding DataRow
foreach (ListViewItem itemRow in listView1.Items)
{
    int i = 0;
    for (i = 0; i < itemRow.SubItems.Count - 1; i++)
    {
        pdfTable.AddCell(itemRow.SubItems[i].Text);
    }
}

//Exporting to PDF
string folderPath = @"D:/Temp/";
if (!Directory.Exists(folderPath))
{
    Directory.CreateDirectory(folderPath);
}
using (FileStream stream = new FileStream(folderPath + "DataGridViewExport.pdf", FileMode.Create))
{
    Document pdfDoc = new Document(PageSize.A2, 10f, 10f, 10f, 0f);
    PdfWriter.GetInstance(pdfDoc, stream);
    pdfDoc.Open();
    pdfDoc.Add(pdfTable);
    pdfDoc.Close();
    stream.Close();
}
 
Share this answer
 
v2
It's saying to me that I can't convert Document into iTextSharp...
 
Share this answer
 
Comments
Richard Deeming 15-Mar-22 4:34am    
Your error message is not a "solution" to this already-solved question.
CHill60 15-Mar-22 4:53am    
If you have a problem of your own then either use the "Have a Question or Comment?" link next to the solution OR post your own question using the red "Ask a Question" link at the top of this page.
Do not post questions or comments as a "Solution" to another post

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900