.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Articles >> ASP.NET >> Post New Resource Bookmark and Share   

 Subscribe to Articles

Add an Image to pdf document using ItextSharp

Posted By:Pankaj Mishra       Posted Date: January 06, 2010    Points: 25    Category: ASP.NET    URL: http://www.dotnetspark.com  
 

Add an Image to pdf document using ItextSharp

In this article we will show how to add Image in pdf document using Itextsharp in asp.net. For some report which is in pdf format we need to add image .jpg, .gif, .bmp or any image format.
Itextsharp is an open source free pdf generate library which will generate the pdf and yes you can add the image into it.

you can also refer to my articles on ItextSharp in asp.net here to create table in pdf doc here

Step 1 : Download the ITextsharp library from here.

Step 2 : Add Itextsharp.dll to your project by right click and add reference on the project explorer

Step 3 : Now add the Namespace to your .cs or .vb file

using iTextSharp.text;
using iTextSharp.text.pdf;

Step 4 :

Lets see this code which will be place image into PDF document

      // Now image in the pdf file
            string imageFilePath = Server.MapPath(".") + "/image/Sunset.jpg";
            iTextSharp.text.Image jpg = iTextSharp.text.Image.GetInstance(imageFilePath);

            //Resize image depend upon your need
            jpg.ScaleToFit(280f, 260f);

            //Give space before image
            jpg.SpacingBefore = 30f;

            //Give some space after the image
            jpg.SpacingAfter = 1f;
            jpg.Alignment = Element.ALIGN_CENTER;

            doc.Add(paragraph); // add paragraph to the document
            
            doc.Add(jpg); //add an image to the created pdf document

Now to see full code you can place the below code in any event like on button click

In C# sample code function

private void CreatePDFFile()
{
        Document doc = new Document(iTextSharp.text.PageSize.LETTER, 10, 10, 42, 35);
        try
        {
            string pdfFilePath = Server.MapPath(".") + "/pdf/myPdf.pdf";

            //Create Document class object and set its size to letter and give space left, right, Top, Bottom Margin
            PdfWriter wri = PdfWriter.GetInstance(doc, new FileStream(pdfFilePath, FileMode.Create));
            doc.Open();//Open Document to write

            //Write some content into pdf file
            Paragraph paragraph = new Paragraph("This is my first line using Paragraph.");
           
            // Now image in the pdf file
            string imageFilePath = Server.MapPath(".") + "/image/Sunset.jpg";
            iTextSharp.text.Image jpg = iTextSharp.text.Image.GetInstance(imageFilePath);

            //Resize image depend upon your need
            jpg.ScaleToFit(280f, 260f);

            //Give space before image
            jpg.SpacingBefore = 30f;

            //Give some space after the image
            jpg.SpacingAfter = 1f;
            jpg.Alignment = Element.ALIGN_CENTER;

            doc.Add(paragraph); // add paragraph to the document
            
            doc.Add(jpg); //add an image to the created pdf document
        }
        catch (DocumentException docEx)
        {
            //handle pdf document exception if any
        }
        catch (IOException ioEx)
        {
            // handle IO exception
        }
        catch (Exception ex)
        {
            // ahndle other exception if occurs
        }
        finally
        {
            //Close document and writer
            doc.Close();
           
        }
    }

Same above code vb.net method

 

Private Sub CreatePDFFile()
    Dim doc As New Document(iTextSharp.text.PageSize.LETTER, 10, 10, 42, 35)
    Try
        Dim pdfFilePath As String = Server.MapPath(".") & "/pdf/myPdf.pdf"
        
        'Create Document class object and set its size to letter and give space left, right, Top, Bottom Margin
        Dim wri As PdfWriter = PdfWriter.GetInstance(doc, New FileStream(pdfFilePath, FileMode.Create))
        doc.Open()
        'Open Document to write
        'Write some content into pdf file
        Dim paragraph As New Paragraph("This is my first line using Paragraph.")
        
        ' Now image in the pdf file
        Dim imageFilePath As String = Server.MapPath(".") & "/image/Sunset.jpg"
        Dim jpg As iTextSharp.text.Image = iTextSharp.text.Image.GetInstance(imageFilePath)
        
        'Resize image depend upon your need
        jpg.ScaleToFit(280F, 260F)
        
        'Give space before image
        jpg.SpacingBefore = 30F
        
        'Give some space after the image
        jpg.SpacingAfter = 1F
        jpg.Alignment = Element.ALIGN_CENTER
        
        doc.Add(paragraph)
        ' add paragraph to the document
            'add an image to the created pdf document
        doc.Add(jpg)
    Catch docEx As DocumentException
        'handle pdf document exception if any
    Catch ioEx As IOException
        ' handle IO exception
    Catch ex As Exception
        ' ahndle other exception if occurs
    Finally
        'Close document and writer
            
        doc.Close()
    End Try
End Sub

If you run your application you can see the image in pdf document like this

You can also donload the sample project using below donload link

Hope this will help all

Cheers!!!
Pankaj


 Subscribe to Articles

     

Further Readings:

Responses

No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More Articles on C#, ASP.Net, Vb.Net, SQL Server and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend