In this article I will explain with an example, how to export (convert) Image to PDF using iTextSharp in ASP.Net with C# and VB.Net.
The Image file will be first uploaded using FileUpload control and saved into a Folder (Directory), then the Image file will be added into the iTextSharp PDF document and ultimately downloaded as PDF file in ASP.Net.
 
 
HTML Markup
The following HTML Markup consists of an ASP.Net FileUpload control and a Button.
<asp:FileUpload ID="FileUpload1" runat="server" />  
<asp:Button Text="Upload" runat="server" OnClick = "Upload" />
 
 
Namespaces
You will need to import the following namespaces.
C#
using System.IO;
using iTextSharp.text;
using iTextSharp.text.pdf;
 
VB.Net
Imports System.IO
Imports iTextSharp.text
Imports iTextSharp.text.pdf
 
 
Converting Image to PDF using iTextSharp and downloading the PDF file in ASP.Net
When the Upload Button is clicked, first the uploaded Image file is saved into a Folder (Directory).
Then the Image file is read using its path and added to an iTextSharp PDF document.
Finally, the iTextSharp PDF document is downloaded as PDF using Response Stream.
C#
protected void Upload(object sender, EventArgs e)
{
    //Save the Uploaded Image file.
    string filePath = Server.MapPath("~/Uploads/") + Path.GetFileName(FileUpload1.PostedFile.FileName);
    FileUpload1.SaveAs(filePath);
 
    //Initialize the PDF document object.
    using (Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 10f, 10f))
    {
        PdfWriter writer = PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
        pdfDoc.Open();
 
        //Add the Image file to the PDF document object.
        iTextSharp.text.Image img = iTextSharp.text.Image.GetInstance(filePath);
        pdfDoc.Add(img);
        pdfDoc.Close();
 
        //Download the PDF file.
        Response.ContentType = "application/pdf";
        Response.AddHeader("content-disposition", "attachment;filename=ImageExport.pdf");
        Response.Cache.SetCacheability(HttpCacheability.NoCache);
        Response.Write(pdfDoc);
        Response.End();
    }
}
 
VB.Net
Protected Sub Upload(ByVal sender As Object, ByVal e As EventArgs)
    'Save the Uploaded Image file.
    Dim filePath As String = Server.MapPath("~/Uploads/") + Path.GetFileName(FileUpload1.PostedFile.FileName)
    FileUpload1.SaveAs(filePath)
 
    'Initialize the PDF document object.
    Using pdfDoc As Document = New Document(PageSize.A4, 10.0F, 10.0F, 10.0F, 10.0F)
        Dim writer As PdfWriter = PdfWriter.GetInstance(pdfDoc, Response.OutputStream)
        pdfDoc.Open()
 
        'Add the Image file to the PDF document object.
        Dim img As iTextSharp.text.Image = iTextSharp.text.Image.GetInstance(filePath)
        pdfDoc.Add(img)
        pdfDoc.Close()
 
        'Download the PDF file.
        Response.ContentType = "application/pdf"
        Response.AddHeader("content-disposition", "attachment;filename=ImageExport.pdf")
        Response.Cache.SetCacheability(HttpCacheability.NoCache)
        Response.Write(pdfDoc)
        Response.[End]()
    End Using
End Sub
 
 
Screenshot
Export (Convert) Image to PDF using iTextSharp in ASP.Net with C# and VB.Net
 
 
Downloads