Question How to export listview to pdf

emammadov

New member
Joined
Sep 30, 2016
Messages
3
Programming Experience
Beginner
Hi. I have created loan calculator in C# and want to export the data in listview to pdf, but I don't know how to do it. I am using iTextSharp. I need your help. I am pasting source code here:

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;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            double kreditmeblegi;
            double illikfaiz;
            double fm;
            double value1;
            double ay;
            double il;
            double ayliqodenis;
            double umumiodenis;
            double esasborc;
            double faiz;
            double qaliqmebleg;

            if (string.IsNullOrEmpty(textBox1.Text) ||  string.IsNullOrEmpty(textBox2.Text) ||  string.IsNullOrEmpty(textBox3.Text))
            {
                MessageBox.Show("Rəqəmləri daxil edin.");
            }

            else
            {
                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;

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

                    DateTime date = this.dateTimePicker1.Value.Date;
                    DateTime nyear = new DateTime();
                    string format = "dd / MM / yyyy";
                    qaliqmebleg = kreditmeblegi;
                    for (int i = 1; i <= ay; i++)
                    {
                        nyear = date.AddMonths(i);
                        ListViewItem lvi = new ListViewItem(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;
                    }
                }

                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;

                    DateTime date = this.dateTimePicker1.Value.Date;
                    DateTime nyear = new DateTime();
                    string format = "dd / MM / yyyy";
                    qaliqmebleg = kreditmeblegi;
                    for (int j = 1; j <= il; j++)
                    {
                        nyear = date.AddMonths(j);
                        ListViewItem lvi = new ListViewItem(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);
                    }

                    label9.Text = ayliqodenis.ToString("N2") + " " + comboBox1.Text;
                    label10.Text = umumiodenis.ToString("N2") + " " + comboBox1.Text;
                    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;
                    }
                }
            }

        }

        private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!char.IsControl(e.KeyChar) && !char.IsDigit(e.KeyChar) &&
        (e.KeyChar != '.'))
            {
                e.Handled = true;
            }

            // only allow one decimal point
            if ((e.KeyChar == '.') && ((sender as TextBox).Text.IndexOf('.') > -1))
            {
                e.Handled = true;
            }
        }

        private void textBox2_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!char.IsControl(e.KeyChar) && !char.IsDigit(e.KeyChar) &&
        (e.KeyChar != '.'))
            {
                e.Handled = true;
            }

            // only allow one decimal point
            if ((e.KeyChar == '.') && ((sender as TextBox).Text.IndexOf('.') > -1))
            {
                e.Handled = true;
            }
        }

        private void textBox3_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!char.IsControl(e.KeyChar) && !char.IsDigit(e.KeyChar) &&
        (e.KeyChar != '.'))
            {
                e.Handled = true;
            }

            // only allow one decimal point
            if ((e.KeyChar == '.') && ((sender as TextBox).Text.IndexOf('.') > -1))
            {
                e.Handled = true;
            }
        }

        private void haqqındaToolStripMenuItem_Click(object sender, EventArgs e)
        {
            About about = new About();
            about.Show();
        }

        private void wiewHelpToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Help melumat = new Help();
            melumat.Show();
        }

        private void təmizləToolStripMenuItem_Click(object sender, EventArgs e)
        {
            textBox1.Clear();
            textBox2.Clear();
            textBox3.Clear();
            comboBox1.Text = "AZN";
            comboBox2.Text = "ay";
            dateTimePicker1.Value = DateTime.Now;
            label9.Text = "0";
            label10.Text = "0";
            listView1.Items.Clear();
            button1.Enabled = true;
        }

        private void ?ıxToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void comboBox1_KeyPress(object sender, KeyPressEventArgs e)
        {
            e.Handled = true;
        }

        private void comboBox2_KeyPress(object sender, KeyPressEventArgs e)
        {
            e.Handled = true;
        }

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

        private void pDFə?ıxartToolStripMenuItem_Click(object sender, EventArgs e)
        {
            FileStream fs = new FileStream("Kreditin Hesablanmasi.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("Kredit Kalkulyatoru"));
            doc.Add(new Paragraph("This is a page 2"));
            doc.Close();
        }
    }
}

loan-calculator.jpg
 
Last edited by a moderator:
I want to export only listview1 part. That part is as following:

for (int i = 1; i <= ay; i++) {
nyear = date.AddMonths(i);
ListViewItem lvi = new ListViewItem(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);
}
 
As you see, I can write "This a page 1" to PDF in the code. Since I am beginner in C Sharp, I don't know how to export listview to PDF.

private void pDFə?ıxartToolStripMenuItem_Click(object sender, EventArgs e) {
FileStream fs = new FileStream("Kreditin Hesablanmasi.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("This is a page 1"));

doc.Close();
}
 
This is a perfect example of looking at things too broadly. There is no magic button to export data from a ListView to a PDF. There are two distinct parts to this problem. Getting data out of a ListView is the same regardless of what you intend to do with that data and writing data to a PDF file is the same regardless of where that data comes from. In situations like this, you should always be breaking the problem up and looking to solve each part separately, then just put the partial solutions together to solve the whole.

So, traversing anything with rows and columns is generally a case of using nested loops and a ListView is no different, e.g.
foreach (ListViewItem item in myListView.Items)
{
    foreach (ListViewItem.ListViewSubItem subItem in item.SubItems)
    {
        var text = subItem.Text;
        // ...
    }
}
You can package the data up in the loop and then write it out at the end or you can write it out as you go. I've never used iTextSharp so I can't advise you specifically but I'm sure that a few web searches would give you all the info you needed on outputting the data in the format you want. A format that you haven't actually specified, by the way.
 
I know it's too late to answer this but maybe someone will look for the same question later...

all you need to do is to loop on your listview items and get the text from there in a collection. then use a PDF generation library to display the items as a list in your PDF,
I use PDFFlow library and it has a nice simple list and also a multilevel list for more complicated designs.

here is a code for you to create the list you want in PDF:

C#:
DocumentBuilder builder = DocumentBuilder.New();
var section = builder.AddSection();

//// 3 items in List
section.AddParagraph(“Asia”).SetMarginTop(50).SetListBulleted().ToSection()
.AddParagraph(“Africa”).SetListBulleted().ToSection()
.AddParagraph(“North America”).SetListBulleted().ToSection().ToDocument();
builder.Build(“Result.pdf”);

good luck :)
 
Back
Top Bottom