using System;
using System.Data;
using System.Data.OleDb;
using PDFlibDLL;

namespace Generate_PDF_report_from_Microsoft_Access
{
    class Program
    {
        static void Main(string[] args)
        {
            // Copy full path of *.mdb file here or copy/paste the Books.mdb file into \bin\Debug or \bin\Release
            DBHandling db = new DBHandling(@".\Books.mdb");
            db.WriteBooksRecordsToPDF(@".\Books.pdf");
        }

        class DBHandling
        {	// If you want to use the ActiveX edition of the library, swap
            // the two lines of code below.
            // PDFlibAX.PDFlibrary PDF = new PDFlibAx.PDFlibrary();

            PDFlibrary PDF = new PDFlibrary(@"..\..\..\..\DLL\PDFlibDLL32.dll");

            OleDbConnection OldeDbcon;
            int YPos;

            public DBHandling(string FilePath)
            {
                string ConStr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=\"" + FilePath + "\";Persist Security Info=True";
                OldeDbcon = new OleDbConnection(ConStr);
            }

            public void WriteBooksRecordsToPDF(string PdfFileName)
            {
                int Id = PDF.NewDocument();

                string Query = "SELECT * FROM Books";

                OleDbCommand cmd = new OleDbCommand(Query, OldeDbcon);

                if (OldeDbcon.State != ConnectionState.Open) OldeDbcon.Open();
                OleDbDataReader rdr;
                try
                {
                    rdr = cmd.ExecuteReader();
                    while (rdr.Read())
                    {
                        YPos = 750;
                        WriteRecord(Convert.ToInt32(rdr["ID"]), Convert.ToString(rdr["Title"]), Convert.ToString(rdr["Author"]),
                        Convert.ToString(rdr["Publisher"]), Convert.ToDouble(rdr["Price"]), Convert.ToString(rdr["ISBN"]));
                        PDF.NewPage();
                    }
                    PDF.SaveToFile(PdfFileName);

                }
                catch (Exception Ex)
                {
                    Console.WriteLine(Ex.ToString());
                    Console.ReadLine();
                }
                OldeDbcon.Close();
            }

            public void WriteRecord(int Id, string Title, string Author, string Publisher, double Price, string ISBN)
            {
                WriteBookTitle(Title); YPos -= 40;
                WriteBookAuthor(Author); WriteBookPrice(Price); YPos -= 20;
                WriteBookPublisher(Publisher); YPos -= 20;
                WriteBookISBN(ISBN);
            }

            public void WriteBookTitle(string Title)
            {
                PDF.AddStandardFont(8);
                PDF.SelectFont(8);
                string FontName = PDF.FontName();
                PDF.SetTextSize(20);
                PDF.DrawText(200, YPos, "Book Title:  " + Title);
            }

            public void WriteBookAuthor(string Author)
            {
                PDF.AddStandardFont(0);
                PDF.SelectFont(0);
                string FontName = PDF.FontName();
                PDF.SetTextSize(16);
                PDF.DrawText(40, YPos, "Author:    " + Author);
            }

            public void WriteBookPublisher(string Publisher)
            {
                PDF.AddStandardFont(3);
                PDF.SelectFont(3);
                string FontName = PDF.FontName();
                PDF.SetTextSize(16);
                PDF.DrawText(40, YPos, "Publisher: " + Publisher);
            }

            public void WriteBookISBN(string ISBN)
            {
                PDF.AddStandardFont(3);
                PDF.SelectFont(3);
                string FontName = PDF.FontName();
                PDF.SetTextSize(16);
                PDF.DrawText(40, YPos, "ISBN:      " + ISBN);
            }

            public void WriteBookPrice(double Price)
            {
                PDF.AddStandardFont(3);
                PDF.SelectFont(3);
                string FontName = PDF.FontName();
                PDF.SetTextSize(16);
                PDF.DrawText(400, YPos, "Price: " + Price.ToString() + "$");
            }
        }

    }
}
