Search - Articles
DevASP.NET for ASP.NET, VB.NET, XML and C# (C-Sharp) Developers Tuesday, March 03, 2009
Dev Articles
Search Directory
SQL Server

How to add image in PDF file using iTextSharp in ASP.NET

Author: Rachel Nicole
Download Source Code :

The purpose of this article is to explain how we can add image in a PDF file using iTextSharp in ASP.NET.

This article is the continuation of my articles on iTextSharp and PDF. In previous article, I have explained little bit about iTextSharp and how we can read PDF file using iTextSharp. Manipulating PDF files using iTextSharp is very easy to code and easy to understand. In this article, I will show you that how easily we can write text and add image in PDF file using iTextSharp in ASP.NET. I will show you sample code in both C# and VB.NET and you can also download code in zipped format by clicking on the above given link.

First we need to create an object of Document class and create a PDF file using GetInstance() method of PdfWriter class of iTextSharp. Then open the file and write text using Paragraph class of iTextSharp. We will use Image class and GetInstance() method of iTextSharp.text to get our image. At the end, by using Add() method of Document class, insert text and image to PDF file.
  1. Open Visual Studio 2010
  2. File > New > Web Site
  3. Visual Basic or Visual C#  > ASP.NET Empty Web Site
  4. Right click on web site > Add New Item > Web Form
  5. Right click on web site > Add Reference > Browse > iTextSharp.dll
  6. Open code behind file and include following namespaces

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

  7.   Write below code in page load event

    Document myDoc = new Document(iTextSharp.text.PageSize.LETTER, 10, 10, 50, 50); 
        string pdfFile = Server.MapPath(".") + "/Files/TestPDFfile.pdf";
        string imageFile = Server.MapPath(".") + "/Files/TestImage.jpg";
        PdfWriter writer = PdfWriter.GetInstance(myDoc, new FileStream(pdfFile, FileMode.Create));
        Paragraph para = new Paragraph("Let's write some text before inserting image.");              
        iTextSharp.text.Image myImage = iTextSharp.text.Image.GetInstance(imageFile);
        myImage.ScaleToFit(300f, 250f);
        myImage.SpacingBefore = 50f;
        myImage.SpacingAfter = 10f;
        myImage.Alignment = Element.ALIGN_CENTER;
    catch (Exception ex)
    Dim myDoc As New Document(iTextSharp.text.PageSize.LETTER, 10, 10, 50, 50)
        Dim pdfFile As String = Server.MapPath(".") + "/Files/TestPDFfile.pdf"
        Dim imageFile As String = Server.MapPath(".") + "/Files/TestImage.jpg"
        Dim writer As PdfWriter = PdfWriter.GetInstance(myDoc, New FileStream(pdfFile, FileMode.Create))
        Dim para As New Paragraph("Let's write some text before inserting image.")
        Dim myImage As iTextSharp.text.Image = iTextSharp.text.Image.GetInstance(imageFile)
        myImage.ScaleToFit(300.0F, 250.0F)
        myImage.SpacingBefore = 50.0F
        myImage.SpacingAfter = 10.0F
        myImage.Alignment = Element.ALIGN_CENTER
    Catch ex As Exception
    End Try

  8. View web site in browser and see the PDF file in specified location.


Add Article Comment:
Name :
Email Address :
Comments :
<< How to read PDF file using iTextSharp in ASP.NET

Disclaimer - Privacy
© 2002-2017