Do you own a Debenu Quick PDF Library 12/11/10/9/8/7? Upgrade to Debenu Quick PDF Library 13!

Foxit Quick PDF Library

Frequently Asked Question:

Return to FAQ Index

Generate PDF report using data from MS Access database

Question

Do you have any sample code that demonstrates how to generate a PDF dynamically that includes data from a Microsoft Access database?

Answer

I have included some sample code below using C# and a Microsoft Access database that demonstrates how easy it is to create PDF files dynamically based on content that's retrieved from a database.

This sample is a little long and more complex than most of the samples, so I have also uploaded a full working sample, including MS Access database that will help you quickly become familiar with Quick PDF Library.

Download the full sample from here.

The C# code used in this sample is provided below:

using System;
using System.Data;
using System.Data.OleDb;
using QuickPDFDLL0718;

// This example uses the DLL edition of Quick PDF Library

public class QPL
{
    public static void Main()
    {
        // Copy full path of *.mdb file here
        DBHandling db = new DBHandling(@"Books.mdb");
        //Output file name will be QLPBooks.pdf
        db.WriteBooksRecordsToPDF("QLPBooks.pdf");
    }

    class DBHandling
    {   // If you want to use the ActiveX edition of the library, swap
        // the two lines of code below.
        // QuickPDFAX0718.PDFLibrary pdf = new QuickPDFAX0718.PDFLibrary();

        PDFLibrary pdf = new PDFLibrary("QuickPDFDLL0718.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)
        {
            pdf.UnlockKey("..."); // Insert your license key here!
            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 -= 20;
            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(225, YPos, "Book Title " + Title);
        }

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

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

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

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

© 2015 Debenu & Foxit. All rights reserved. AboutBuyContactBlogNewsletterSupportFAQProduct UpdatesForum