
// iTextSharp.text Dll ini indirmeniz gerek ve add references deyip eklemelisiniz.
//İndirebileceğiniz adres 
http://sourceforge.net/projects/itextsharp/
// Eklenmesi gerek namespaceler
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using iTextSharp.text;
using iTextSharp.text.pdf;
using System.IO;
using System.Windows.Forms;
// Pdf export etme classımız
class PDFExport
    {
        public string FileName { get; set; } //pdf oluşturacağımız dosya adı
        public string Text { get; set; } //dosyanın içinde oluşturacağımız pdf adı
        public int PdfRowIndex { get; set; } //pdfrowindex
        public string Path { get; set; }
        public DataTable ToDatatable()
        {
            //DataTable Oluştur            
            DataTable dt = new DataTable(“”);
            dt.Columns.Add(new DataColumn(“”, typeof(string)));
            dt.Columns.Add(new DataColumn(“”, typeof(string)));
            dt.Columns.Add(new DataColumn(“”, typeof(string)));
            dt.Columns.Add(new DataColumn(“”, typeof(string)));
            dt.Columns.Add(new DataColumn(“”, typeof(string)));
            dt.Columns.Add(new DataColumn(“”, typeof(string)));
            dt.Columns.Add(new DataColumn(“”, typeof(string)));
            dt.Columns.Add(new DataColumn(“”, typeof(string)));
            dt.Columns.Add(new DataColumn(“”, typeof(string)));
            dt.Columns.Add(new DataColumn(“”, typeof(string)));
            dt.Columns.Add(new DataColumn(“”, typeof(string)));
            dt.Columns.Add(new DataColumn(“”, typeof(string)));
            dt.Columns.Add(new DataColumn(“”, typeof(string)));
            return dt;
        }
        public void ToPdf(DataGridView dgGecKalanKitap)
        {
            DataTable dtPDF = ToDatatable();
            iTextSharp.text.Document document = new iTextSharp.text.Document();
            string dosya = “C:\Kütüphane Otamasyonu\gecKalan.pdf”; //PDF imiz nereye kayıt edilecek ?
            PdfWriter.GetInstance(document, new FileStream(dosya, FileMode.Create));
            BaseFont arial = BaseFont.CreateFont(“C:\windows\fonts\arial.ttf”, BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
            Font font = new Font(arial, 12, Font.NORMAL);
            document.Open();
            PdfPTable table = null;
            table = new PdfPTable(dgGecKalanKitap.Columns.Count);
            table.WidthPercentage = 100;
            string str = string.Empty;
            for (int i = 0; i < dgGecKalanKitap.Columns.Count; i++)
            {
                str += dgGecKalanKitap.Columns[i].HeaderText;
                if (dgGecKalanKitap.Columns.Count > i)
                    str += “+”;
            }
            string str2 = str.TrimEnd(‘+’).ToString();
            ///<summary></summary>
            /// DataGridView kolonlarının sayısı kadar belgenin başlıkları doldurulur.
            /// Pdf hücreleri oluşturulur.Dökumandaki başlık kısmı için ilk satır oluşturulur ve colspan yapılır.
            ///
            PdfPCell cell = new PdfPCell(new Phrase(dosya));
            cell.Colspan = dgGecKalanKitap.Columns.Count;
            cell.HorizontalAlignment = 1; //0=Left, 1=Centre, 2=Right
            table.AddCell(cell);
            ///<summary></summary>
            ///pdf tablosu hücreleri doldurulur
            ///
            for (int i = 0; i < dgGecKalanKitap.Columns.Count; i++)
            {
                table.AddCell(new Phrase(dgGecKalanKitap.Columns[i].HeaderText, font));
            }
            for (int i = 0; i < dtPDF.Rows.Count; i++)
            {
                for (int j = 0; j < dtPDF.Columns.Count; j++)
                {
                    table.AddCell(new Phrase(dtPDF.Rows[i][j].ToString(), font));
                    PdfRowIndex++;
                }
            }
            document.Add(table);
            MessageBox.Show(“Kaydınız Başarıyla Tamamlanmıştır!” + “n” + “Kayıt Yeri” + ” ” + dosya, “Aktarım Sonucu”, MessageBoxButtons.OKCancel, MessageBoxIcon.Information);
            document.Close();
        }
        /// <summary></summary>
     
     
    }
// Classımız yukardaki şimdi ise bunu istediğimiz yerde kullanalım. Sadece aşağıdaki gibi
// Kullanım export için yeterlidir. Geri kalan kısmını yukardaki class halledecektir.
// Sadece 
datagridview ismini düzgün vermeniz yeterli.
private void Gec_Kalan_Kitaplar_Load(object sender, EventArgs e)
        {
            EXCELPDFExporter PdfExport = new EXCELPDFExporter(); 
            PdfExport.FileName = this.Name;
            PdfExport.Text = PdfExport.FileName + “.pdf”;
            PdfExport.PdfRowIndex = 1;
            PdfExport.ToPdf(dataGridView1); // Hangi datagridview içinde ki verileri PDF olarak export edeceğiz
        }