I have succeeded in making it work. Here is my code.
On page load, the uploaded pdf file will be displayed in literal control and a QR code will be generated and bind in image control
protected void Page_Load(object sender, EventArgs e)
{
BindCardAndQR();
}
private void BindCardAndQR()
{
if (Session["paperinv"] != null)
{
int id = Convert.ToInt32(Session["paperinv"]);
string sql = "SELECT * FROM tableinvoice WHERE Id = @Id";
SqlParameter[] parameters = new[] {
new SqlParameter("@Id", id)
};
// Bind Image3 for Card Image
DataTable dt = SelectFromDatabase(sql, parameters);
if (dt.Rows.Count > 0)
{
string embed = "<object data=\"{0}{1}\" type=\"application/pdf\"></object>";
ltEmbed.Text = string.Format(embed, ResolveUrl("Handler.ashx?Id="), id);
byte[] QRBytes = GetQRCodeBytes(Server.MapPath("~/PDFprint.aspx") + "?Id=" + id);
Image1.ImageUrl = "data:image/jpg;base64," + Convert.ToBase64String(QRBytes);
}
}
}
public byte[] GetQRCodeBytes(string url)
{
//QR code generated here
QRCodeEncoder encoder = new QRCodeEncoder();
Bitmap bi = encoder.Encode(url);
MemoryStream tmpSteam = new MemoryStream();
bi.Save(tmpSteam, ImageFormat.Jpeg);
return tmpSteam.ToArray();
}
Then on Button click, the QR code generated in image control will be attached to the uploaded pdf for downloading or printing
protected void btnPrinnt_Click(object sender, EventArgs e)
{
if (Session["paperinv"] != null)
{
int id = Convert.ToInt32(Session["paperinv"]);
string sql = "SELECT * FROM tableinvoice WHERE Id = @Id";
SqlParameter[] parameters = new[] {
new SqlParameter("@Id", id)
};
// Bind Image3 for Card Image
DataTable dt = SelectFromDatabase(sql, parameters);
if (dt.Rows.Count > 0)
{
string embed = "<object data=\"{0}{1}\" type=\"application/pdf\"></object>";
ltEmbed.Text = string.Format(embed, ResolveUrl("Handler.ashx?Id="), id);
byte[] QRBytes = GetQRCodeBytes(Server.MapPath("~/PDFprint.aspx") + "?Id=" + id);
Image1.ImageUrl = "data:image/jpg;base64," + Convert.ToBase64String(QRBytes);
iTextSharp.text.Image image = iTextSharp.text.Image.GetInstance(QRBytes);
byte[] pdfbytes = (byte[])dt.Rows[0]["Data"];
using (MemoryStream memoryStream = new MemoryStream())
{
var reader = new PdfReader(pdfbytes);
var stamper = new PdfStamper(reader, memoryStream);
var pdfContentByte = stamper.GetOverContent(1);
image.SetAbsolutePosition(10, reader.GetPageSize(1).Height - 100);
pdfContentByte.AddImage(image);
stamper.Close();
byte[] bytess = memoryStream.ToArray();
memoryStream.Close();
Response.Clear();
Response.ContentType = "application/pdf";
Response.AddHeader("Content-Disposition", "attachment; filename=Invoice.pdf");
Response.ContentType = "application/pdf";
Response.Buffer = true;
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.BinaryWrite(bytess);
Response.End();
}
}
}
}
public static DataTable SelectFromDatabase(string sql, SqlParameter[] parameters)
{
using (SqlConnection con = new SqlConnection("Data Source=(LocalDB)\\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\\Dataregister.mdf;Integrated Security=True"))
{
using (SqlCommand cmd = new SqlCommand(sql, con))
{
if (parameters != null)
{
cmd.Parameters.AddRange(parameters);
}
using (SqlDataAdapter sda = new SqlDataAdapter(cmd))
{
DataTable dt = new DataTable();
sda.Fill(dt);
return dt;
}
}
}
}