Write DataBase Data to PDF File

A file with .pdf file extension is called PDF (Portable Document Format) file. Nowadays PDF files are frequently used in important documents such as tax papers, bank statements, and other forms of documents that require the user to fill in data. There is a usual question that how to read data from a database using sql statements and write that data to a PDF document.

PDFsharp is the Open Source library for creating and modifying Adobe PDF documents programmatically. You can freely download the Assemblies version from the following link:

After download the zip file, extract it and add the reference to your c# project, 

  1. using System;  
  2. using System.Windows.Forms;  
  3. using System.Diagnostics;  
  4. using PdfSharp;  
  5. using PdfSharp.Drawing;  
  6. using PdfSharp.Pdf;  
  7. using System.Data.SqlClient;  
  8. using System.Data;  
  9. namespace WindowsFormsApplication1  
  10. {  
  11.     public partial class Form1: Form   
  12.     {  
  13.         public Form1()  
  14.         {  
  15.             InitializeComponent();  
  16.         }  
  17.         private void button1_Click(object sender, EventArgs e)   
  18.         {  
  19.             try  
  20.             {  
  21.                 string connetionString = null;  
  22.                 SqlConnection connection;  
  23.                 SqlCommand command;  
  24.                 SqlDataAdapter adapter = new SqlDataAdapter();  
  25.                 DataSet ds = new DataSet();  
  26.                 int i = 0;  
  27.                 string sql = null;  
  28.                 int yPoint = 0;  
  29.                 string pubname = null;  
  30.                 string city = null;  
  31.                 string state = null;  
  32.                 connetionString = "Data Source=codbazar;Initial Catalog=pdf;User ID=sa;Password=cod123";  
  33.                 sql = "select pub_name,city,country from pdftable";  
  34.                 connection = new SqlConnection(connetionString);  
  35.                 connection.Open();  
  36.                 command = new SqlCommand(sql, connection);  
  37.                 adapter.SelectCommand = command;  
  38.                 adapter.Fill(ds);  
  39.                 connection.Close();  
  40.                 PdfDocument pdf = new PdfDocument();  
  41.                 pdf.Info.Title = "Database to PDF";  
  42.                 PdfPage pdfPage = pdf.AddPage();  
  43.                 XGraphics graph = XGraphics.FromPdfPage(pdfPage);  
  44.                 XFont font = new XFont("Verdana", 20, XFontStyle.Regular);  
  45.                 yPoint = yPoint + 100;  
  46.                 for (i = 0; i < = ds.Tables[0].Rows.Count - 1; i++)  
  47.                 {  
  48.                     pubname = ds.Tables[0].Rows[i].ItemArray[0].ToString();  
  49.                     city = ds.Tables[0].Rows[i].ItemArray[1].ToString();  
  50.                     state = ds.Tables[0].Rows[i].ItemArray[2].ToString();  
  51.                     graph.DrawString(pubname, font, XBrushes.Black, new XRect(40, yPoint, pdfPage.Width.Point, pdfPage.Height.Point), XStringFormats.TopLeft);  
  52.                     graph.DrawString(city, font, XBrushes.Black, new XRect(280, yPoint, pdfPage.Width.Point, pdfPage.Height.Point), XStringFormats.TopLeft);  
  53.                     graph.DrawString(state, font, XBrushes.Black, new XRect(420, yPoint, pdfPage.Width.Point, pdfPage.Height.Point), XStringFormats.TopLeft);  
  54.                     yPoint = yPoint + 40;  
  55.                 }  
  56.                 string pdfFilename = "dbtopdf.pdf";  
  57.                 pdf.Save(pdfFilename);  
  58.                 Process.Start(pdfFilename);  
  59.             } catch (Exception ex)   
  60.             {  
  61.                 MessageBox.Show(ex.ToString());  
  62.             }  
  63.         }  
  64.     }