Home > Articles > .Net Framework Articles > Convert SQL data to pdf

Convert SQL data to pdf

by kumbhani bhavesh   on Jun 26, 2016   Category: .Net Framework  | Level: Advance  |  Views: 1855    |  Points: 100   
Like this article? Bookmark and Share:
this article is convert sql data into pdf format .

Convert SQL Data base to PDF


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.

 Spire.pdf is use for creating and modifying Adobe PDF documents programmatically. Spire.pdf is provided by Spire.Office.

Now I will explain using some simple steps.

Step 1:

Download from http://www.e-iceblue.com/Introduce/spire-office-for-net.html . After Download this Right click on Reference and select Add Reference. On Reference Manager Click on Browse Button and Select all files in NET4.0 as see in Image below.

You Will find Spire’s dll file in References file

Step 2:

In this I see you how Sql Data is convert in pdf file. See the design of SQL To PDF

First of all we have to connect with our system Database. For I provide Button is “Connect With DataBase”  When We click on it it will open onnectionDialog




This Code is for Get Connection Dialog

DataConnectionDialog dlg = new DataConnectionDialog();


            if (DataConnectionDialog.Show(dlg) == DialogResult.OK)


                label1.Text = dlg.ConnectionString;

                bool isSavePasswordChecked = IsSavePasswordChecked(dlg);

                label2.Text = "Connection Successfully";


Step 3:

After Successful connection we will get all tables in drop down and when we select table, it’s data will fill in gridview . and after click on Export To pdf button it will convert Grid View to pdf File and give output like.

Step 4:

Now Last step is create PDF file of selected data. For this we will click on “Export to PDF” button it will convert all data in PDF file

 Code For Convert in pdf :

 PdfDocument pdf = new PdfDocument();

 PdfHtmlLayoutFormat htmlLayoutFormat = new            PdfHtmlLayoutFormat();

            htmlLayoutFormat.IsWaiting = false;

            PdfPageSettings setting = new PdfPageSettings();

            setting.Size = PdfPageSize.A2;

           Thread thread = new Thread(() =>

            { pdf.LoadFromHTML(DGVtoString(gridtable), false, setting, htmlLayoutFormat); });




            SaveFileDialog dialog = new SaveFileDialog();

            dialog.Filter = "PDF document (*.pdf)|*.pdf";

            DialogResult result = dialog.ShowDialog();

            string filename = dialog.FileName;

            if (filename != "")


                pdf.SaveToFile(filename, Spire.Pdf.FileFormat.PDF);



Above code I use DGVtoString(gridtable) . It get data from gridview and store in html format

Code Of DGVtoString(gridtable):

public static string DGVtoString(DataGridView dgv)


            using (StringWriter sw = new StringWriter())


                using (HtmlTextWriter hw = new HtmlTextWriter(sw))


                    System.Text.StringBuilder sb = new System.Text.StringBuilder();

                    sb.Append("<br />");



                    sb.AppendFormat("<table border='1px' align='center' height='auto' width='100%'>");


                    foreach (DataGridViewColumn column in dgv.Columns)







                    foreach (DataGridViewRow row in dgv.Rows)



                        foreach (DataGridViewCell cell in row.Cells)











                    return sb.ToString();




Finaly we create PDF File

Using library we can easily export data from database to PDF just using some line of code.


Like this article? Bookmark and Share:

Most viewed Articles

User Comments

No response found, be the first to review this article.

Submit feedback about this article

Please sign in to post feedback

Latest Posts