Getsuyodev, Programming is mainly used when constructing an application. Programming requires knowledge of application domains, algorithms, and programming language expertise. Programming can be developed in different logic based on developer knowledge.

Write database data to PDF file



A file with a .pdf file extension is called PDF (Portable Document Format) file. Nowadays, PDF files are frequently used in relevant 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 about 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: Download PDFsharp Assemblies

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

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 the 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);

The following C# source code shows how to retrieve the data from the 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());
            }
        }
    }
}

Comments are closed.

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More