Net-informations.com
SiteMap  | About    

Write database data to pdf file

pdf - Portable Document Format

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.

Database to PDF

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: Download PDFsharp Assemblies

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

pdf assembly files

If you want to know the step by step tutorial on how to create your first pdf file programmatically, follow the link : How to create PDF file programmatically

Write sql result to pdf file

Here we are using Pdfsharp library to generate the PDF document, before that we have to read the data from Database. So we create connection string and sql for doing this.

connetionString = "Data Source=YourServerName;Initial Catalog=pubs;User ID=sa;Password=zen412"; sql = "select pub_name,city,country from publishers"; connection = new SqlConnection(connetionString);

Write data from Dataset to pdf file

Retrieve the data and store it n a Dataset.

adapter.Fill(ds);

The next step is to create and initialize the PDFSharp Object to create the pdf document.

PdfDocument pdf = new PdfDocument(); PdfPage pdfPage = pdf.AddPage(); XGraphics graph = XGraphics.FromPdfPage(pdfPage); XFont font = new XFont("Verdana", 20, XFontStyle.Regular );

Finally we retrieve each row from the dataset and write to the pdf file.

graph.DrawString(pubname, font, XBrushes.Black, new XRect(40, yPoint, pdfPage.Width.Point, pdfPage.Height.Point), XStringFormats.TopLeft);

output sample display

Sql to PDF

The following C# source code shows how to retrieve the data from database and write to the pdf file.




using System;
using System.Windows.Forms;
using System.Diagnostics;
using PdfSharp;
using PdfSharp.Drawing;
using PdfSharp.Pdf;
using System.Data.SqlClient;
using System.Data;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                string connetionString = null;
                SqlConnection connection ;
                SqlCommand command ;
                SqlDataAdapter adapter = new SqlDataAdapter();
                DataSet ds = new DataSet();
                int i = 0;
                string sql = null;
                int yPoint = 0;
                string pubname = null;
                string city = null;
                string state = null;

                connetionString = "Data Source=YourServerName;Initial Catalog=pubs;User ID=sa;Password=zen412";
                sql = "select pub_name,city,country from publishers";
                connection = new SqlConnection(connetionString);
                connection.Open();
                command = new SqlCommand(sql, connection);
                adapter.SelectCommand = command;
                adapter.Fill(ds);
                connection.Close();

                PdfDocument pdf = new PdfDocument();
                pdf.Info.Title = "Database to PDF";
                PdfPage pdfPage = pdf.AddPage();
                XGraphics graph = XGraphics.FromPdfPage(pdfPage);
                XFont font = new XFont("Verdana", 20, XFontStyle.Regular );

                yPoint = yPoint + 100;

                for (i = 0; i < = ds.Tables[0].Rows.Count - 1; i++)
                {
                    pubname = ds.Tables[0].Rows[i].ItemArray[0].ToString ();
                    city = ds.Tables[0].Rows[i].ItemArray[1].ToString();
                    state = ds.Tables[0].Rows[i].ItemArray[2].ToString();
                        
                    graph.DrawString(pubname, font, XBrushes.Black, new XRect(40, yPoint, pdfPage.Width.Point, pdfPage.Height.Point), XStringFormats.TopLeft);

                    graph.DrawString(city, font, XBrushes.Black, new XRect(280, yPoint, pdfPage.Width.Point, pdfPage.Height.Point), XStringFormats.TopLeft);

                    graph.DrawString(state, font, XBrushes.Black, new XRect(420, yPoint, pdfPage.Width.Point, pdfPage.Height.Point), XStringFormats.TopLeft);

                    yPoint = yPoint + 40;
                }


                string pdfFilename = "dbtopdf.pdf";
                pdf.Save(pdfFilename);
                Process.Start(pdfFilename);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }
    }
}






net-informations.com (C) 2017    Founded by raps mk
All Rights Reserved. All other trademarks are property of their respective owners.