locked
how can I Covert an image to pdf document using C# RRS feed

  • Question

  • User-296994568 posted

    Hi,

    Can anyone tell, how can I Covert an image to pdf document using c#

    Regards,

    shahzad

    Friday, March 30, 2012 7:35 AM

Answers

All replies

  • User1390720689 posted

    // creation of the document with a certain size and certain margins  
    iTextSharp.text.Document document = new iTextSharp.text.Document(iTextSharp.text.PageSize.A4, 0, 0, 0, 0);  
      
    // creation of the different writers  
    iTextSharp.text.pdf.PdfWriter writer = iTextSharp.text.pdf.PdfWriter.GetInstance(document, new System.IO.FileStream(Server.MapPath("~/App_Data/result.pdf"), System.IO.FileMode.Create));  
      
    // load the tiff image and count the total pages  
    System.Drawing.Bitmap bm = new System.Drawing.Bitmap(Server.MapPath("~/App_Data/source.tif"));  
    int total = bm.GetFrameCount(System.Drawing.Imaging.FrameDimension.Page);  
      
    document.Open();  
    iTextSharp.text.pdf.PdfContentByte cb = writer.DirectContent;  
    for (int k = 0; k < total; ++k)  
    {  
        bm.SelectActiveFrame(System.Drawing.Imaging.FrameDimension.Page, k);  
        iTextSharp.text.Image img = iTextSharp.text.Image.GetInstance(bm, System.Drawing.Imaging.ImageFormat.Bmp);  
        // scale the image to fit in the page  
        img.ScalePercent(72f / img.DpiX * 100);  
        img.SetAbsolutePosition(0, 0);  
        cb.AddImage(img);  
        document.NewPage();  
    }  
    document.Close(); 

    but for this you have to use thied party dll itextsharp.

    download it from:

    http://sourceforge.net/projects/itextsharp/

    Friday, March 30, 2012 7:42 AM
  • User-296994568 posted

    will you please elaborate "img.ScalePercent(72f / img.DpiX * 100);"

    this code always convert a half image.

    Friday, March 30, 2012 7:45 AM
  • User1046077057 posted

    HI,

    You can refer to the below link about the sample.

    http://www.codeproject.com/Articles/28609/Converting-Image-Files-to-PDF

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, April 5, 2012 8:10 AM
  • User-296994568 posted

    above code create the pdf, but if the image is large, created PDF contain incomplete image.

    Thursday, April 5, 2012 10:23 AM
  • User1046077057 posted

    Hi,

    If so, you can resize the image first when it is very large.

    http://forums.asp.net/t/1530762.aspx

    http://www.codeproject.com/Articles/31180/Image-resizing-with-asp-net

    Tuesday, April 10, 2012 11:04 PM
  • User88744855 posted

    which dlli need to include

    Tuesday, April 9, 2013 10:33 AM