You must Sign In to post a response.
  • Category: .NET

    How to add any image in pdf

    actually i have completed retrieve values from database and show in pdf when button click but i want to add image to pdf my image in C:\temp how to add this image to pdf doc below is my code any body give me any example
    protected void bt11_Click(object sender, EventArgs e)
    string cs = "Data Source=HOME;Initial Catalog=Registration;Integrated Security=True";
    SqlConnection con = new SqlConnection(cs);
    SqlCommand cmd = new SqlCommand("select * from employeep where Salary"+ DropDownList1.SelectedValue +"", con);
    SqlDataAdapter da = new SqlDataAdapter(cmd);
    DataSet ds = new DataSet();
    GridView1.DataSource = ds;
    string Filepath = Server.MapPath("~report.pdf") ;

    // iTextSharp.text.Image jpg = iTextSharp.text.Image.GetInstance(Filepath);
    FileStream fs = new FileStream(Filepath, System.IO.FileMode.Create);
    StringWriter sw = new StringWriter();
    HtmlTextWriter hw = new HtmlTextWriter(sw);


    StringReader sr = new StringReader(sw.ToString());
    Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 10f, 0f);
    HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
    PdfWriter.GetInstance(pdfDoc, fs);
    GridView1.Visible = false;
    iframepdf.Attributes.Add("src", "~report.pdf");

  • #761860

    Please use the code below:

    string imageURL = Server.MapPath(".") + "/sacrifice1.png";
    iTextSharp.text.Image img = iTextSharp.text.Image.GetInstance(imageURL);
    //Resize image based on the requirement
    img.ScaleToFit(80f, 80f);
    //Align image to the left
    img.Alignment = Element.ALIGN_LEFT;

    Miss. Jain
    Microsoft Certified Technology Specialist in .Net

  • #761872

    You can try the following code Pdfsharp

    zImageL = Path.Combine(System.Web.HttpContext.Current.Server.MapPath("."), zImageL );
    Image image = section.AddImage(zImageL );
    image.Height = "1cm";
    image.LockAspectRatio = true;
    image.Left = ShapePosition.Left;
    image.Top = ShapePosition.Top;
    image.RelativeVertical = RelativeVertical.Line;

    By Nathan
    Direction is important than speed

  • #761931

    Hello Velidiviram,

    Add reference of :


    You can try the below code :

    Document doc = new Document(PageSize.A4, 10f, 10f, 100f, 0f);

    string imageURL = Server.MapPath(".") + "/image2.jpg";
    iTextSharp.text.Image jpg = iTextSharp.text.Image.GetInstance(imageURL);
    //Resize image depend upon your need
    jpg.ScaleToFit(140f, 120f);
    //Give space before image
    jpg.SpacingBefore = 10f;
    //Give some space after the image
    jpg.SpacingAfter = 1f;
    jpg.Alignment = Element.ALIGN_LEFT;

    Hope this will help you.

    Nirav Lalan
    DNS Gold Member
    "Failure is the path of least persistence"

  • #761932
    Hello Velidiviram,

    Refer the below code :

    This will help you, also contains the code to encrypt the pdf with password.

    Hope this will help you.

    Nirav Lalan
    DNS Gold Member
    "Failure is the path of least persistence"

  • #762568
    Hi there,

    Besides using iTextsharp, you could also try following method to create PDF document and insert an image to a certain position in PDF from local folder.

    Hope this helps.

  • Sign In to post your comments