Using iTextSharp to print a PDF with multiple tables

I have a detailed bill to print that pulls up billing details from various departments. I need them to be printed on a PDF. But I use plenty of select procedures as I have to display a lot of tables so I need code that I can reuse.

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

The namespaces I used:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.IO;
using iTextSharp.text;
using iTextSharp.text.pdf;
using iTextSharp.text.html;
using iTextSharp.text.html.simpleparser;

The Print button click event that generates a PDF:

protected void btnPrintToPDF_Click(object sender, EventArgs e)
    {
    //Default Settings for your PDF File
    HttpContext.Current.Response.Clear();
    HttpContext.Current.Response.ContentType = "application/pdf";
    HttpContext.Current.Response.AddHeader("content-disposition", string.Format("attachment;filename={0}.pdf", "PDFExport"));
    HttpContext.Current.Response.Charset = "utf-8";
    HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.UTF8;
    HttpContext.Current.Response.Cache.SetCacheability(HttpCacheability.NoCache);

    //Initialize a StringSwriter
    StringWriter stringWrite = new StringWriter();
    DataTable dt = new DataTable();

    //Info line in PDF
    stringWrite.WriteLine("Clothing department");
    //Fetch DataTable
    dt = getDataTablebyProcedure("SelectFromClothingDepartment");
    //Append it to String Writer
    stringWrite = getStringWriterbyDataTable(dt, stringWrite);

    //Info line in PDF
    stringWrite.WriteLine("Food Department");
    //Fetch DataTable
    dt = getDataTablebyProcedure("SelectFromFoodDepartment");
    //Append it to Stwing Writer
    stringWrite = (getStringWriterbyDataTable(dt, stringWrite));

    //As many procedures as you want to go here

    //Finally Write the writer into a reader
    StringReader sr = new StringReader(stringWrite.ToString());
    //Generate the pdf
    Document pdfDoc = new Document(new Rectangle(288f, 144f), 10f, 10f, 10f, 0f);
    pdfDoc.SetPageSize(PageSize.A4.Rotate());
    HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
    PdfWriter.GetInstance(pdfDoc, HttpContext.Current.Response.OutputStream);
    pdfDoc.Open();
    htmlparser.Parse(sr);
    pdfDoc.Close();
    HttpContext.Current.Response.Write(pdfDoc);
    HttpContext.Current.Response.End();
    }

Procedure that returns a datatable of your Select procedure

 protected DataTable getDataTablebyProcedure(string ProcedureName)
    {
    SqlDataAdapter da= new SqlDataAdapter();
    DataTable dt=new DataTable();
    try
        {//open a connection to your DB
        da.Fill(dt);
        }
    catch (Exception ex)
        {//handle Exception
        }
    finally
        {//Close Connection
        }
    return dt;
    }

Function that appends datatables to your StringWriter

protected static StringWriter getStringWriterbyDataTable(DataTable dt, StringWriter stringWrite1 )
    {
    //System.IO.StringWriter stringWrite1 = new System.IO.StringWriter();
    System.Web.UI.HtmlTextWriter htmlWrite1 = new HtmlTextWriter(stringWrite1);
    DataGrid myDataGrid1 = new DataGrid();
    myDataGrid1.DataSource = dt;
    myDataGrid1.DataBind();
    myDataGrid1.RenderControl(htmlWrite1);
    return stringWrite1;
    }

Well, this worked really well for me, so I’m hoping it helps someone someday.


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x