Hello forum,
I recently learned how to attach image control to an uploaded pdf file in C#. It worked well, but the attached image position is not where I want it to be. I have a screenshot of the output below.
![](https://i.imgur.com/LNuNFkI.jpg)
The image is a QR code image and it shows on the top left of the uploaded PDF file. How can I make it show in the bottom center or bottom middle of the uploaded PDF file and also adjust its size because it is too big?
Here is my code that attaches the image control to the uploaded pdf
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;
}
}
}
}