Image To PDF in C# MVC

In this article, I’m gonna discuss how to convert images to PDF. To convert images to PDF, I’m gonna use iTextSharp, So I will let you know how to use iTextSharp to convert Images to PDF in .Net MVC using C#. So let’s start it.

Adding Reference Of iTextSharp Library

To add a reference, go to the Manage NuGet Packages in your project and search for the iTextSharp package. Once you found it, install the library.

Namespaces

using iTextSharp.text;
using iTextSharp.text.pdf;
using System.IO;

Code To Convert Image To PDF

public FileResult ImgToPDF()
        {
            string[] ImagePath = new string[3] { "D:/Chand/flowers/Flower1.png", "D:/Chand/flowers/flower4.png", "D:/Chand/flowers/flower2.png" };

            using (MemoryStream stream = new System.IO.MemoryStream())
            {
                //declare the PDF document object.
                using (Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 10f, 10f))
                {
                    PdfWriter writer = PdfWriter.GetInstance(pdfDoc, stream);
                    pdfDoc.Open();

                    //Add the Image file to the PDF.
                    foreach (var image in ImagePath)
                    {
                        iTextSharp.text.Image img = iTextSharp.text.Image.GetInstance(image);
                        pdfDoc.SetPageSize(img);
                        pdfDoc.NewPage();
                        img.SetAbsolutePosition(0, 0);
                        pdfDoc.Add(img);

                    }
                    pdfDoc.Close();

                    //Download PDF.
                    return File(stream.ToArray(), "application/pdf", "ExportImages.pdf");
                }
            }
        }

The above function, I’m using to create a PDF file of images. Here I’m taking a static array of images, you can take images from anywhere as per your requirement.

The image file is read from the ImagePath array and added to the iTextSharp PDF document. pdfDoc.SetPageSize() is used to set the image properly in page. pdfDoc.NewPage() is used to add new page in a PDF. img.SetAbsolutePosition() is used to define coordinates of image(where to add image in page). I’m adding image at coordinate(0, 0).

Output

4 Comments

  1. Hazel

    can you show me how to do convert image along with gridview to pdf also..

    1
    0
    Reply
    1. Hi Hazel,

      In itextSharp, we can convert HTML string to PDF.
      So using the HTML string of your grid, you can convert them to PDF. Try the below code. Pass your HTML string to function.

      Note: Just make sure that images in the grid have full URLs like https://res.cloudinary.com/dizexseir/image/upload/w_300,c_limit,f_auto,q_auto/v1623234766/ProImages/Hana134_thumbnail.jpg

      Code:
      public void HtmlToPdf(string GridHtml)
      {
      Response.Clear();
      Response.ContentType = “application/pdf”;
      Response.AddHeader(“content-disposition”, “attachment;filename=” + “ExportHtmlToPDF.pdf”);
      Response.Cache.SetCacheability(HttpCacheability.NoCache);

      try
      {
      byte[] bPDF = null;
      MemoryStream stream = new System.IO.MemoryStream();
      TextReader txtReader = new StringReader(GridHtml);

      Document pdfDoc = new Document(PageSize.A4, 25, 25, 25, 25);
      PdfWriter writer = PdfWriter.GetInstance(pdfDoc, stream);
      HTMLWorker htmlWorker = new HTMLWorker(pdfDoc);

      pdfDoc.Open();
      htmlWorker.StartDocument();

      htmlWorker.Parse(txtReader);

      htmlWorker.EndDocument();
      htmlWorker.Close();
      pdfDoc.Close();

      bPDF = stream.ToArray();

      Response.BinaryWrite(bPDF);
      Response.End();
      }
      catch (System.Exception ex)
      {

      }
      }

      0
      0
      Reply
  2. Hazel

    can you show me how to do convert gridview along with gridview to pdf also..

    0
    0
    Reply
  3. Perfect Wallet

    how to integrate mantra mfs100 in flutter ?

    0
    0
    Reply

Submit a Comment

Your email address will not be published. Required fields are marked *

Subscribe

Select Categories