Click here to hide categories Click here to show left categories

User: Home          welcome : Guest          Log In / Register here     

iTextBox - Convert datalist and image in PDF

iTextSharp is open source library which is used to complete the requirement for conversion in PDF with .net.

In this tutorial we will show you how to use the PDF library “iTextSharp”. If you have requirement that you need to convert document or content showing in datalist, datagrid, gridview or any content paragraph, in PDF format. If is very easy to convert with help of the iTextSharp.

With iTextSharp you can also can change the format of content, add any image in pdf, set background (image) of PDF.


Link for download the iTextSharp.

To add images in PDF you need to give the image path to iTextSharp method.

Namespaces used in document:


using iTextSharp.text;

using iTextSharp.text.pdf;

using iTextSharp.text.html;

using iTextSharp.text.html.simpleparser;

string imageFilePath = Server.MapPath(".") + "images/Imagename.gif";

iTextSharp.text.Image jpg = iTextSharp.text.Image.GetInstance(imageFilePath);


jpg.Alignment = Element.ALIGN_CENTER;


jpg.Alignment = Element.ALIGN_RIGHT;


For give the size to image


jpg.ScaleToFit(70, 40);


If you want to choose image as background then,


jpg.Alignment = iTextSharp.text.Image.UNDERLYING;


If you want to give absolute/specified fix position to image.


jpg.SetAbsolutePosition(7, 780);


It will show the image on Top of the page at left side.


For adding the paragraph we need to enter some more code.



Paragraph paragraph = newParagraph("For further information please contact:\nTest content");


It will show the information in two lines like given below:

For further information please contact:

Test content

\n is for new line in paragraph


Below we are giving the complete code for the information.


Response.ContentType = "application/pdf";

Response.AddHeader("content-disposition", "attachment;filename=nameofPDF.pdf");



StringWriter sw = newStringWriter();

HtmlTextWriter hw = newHtmlTextWriter(sw);


dlPressRelease.DataBind(); // datalist name will be here



string imageFilePath = Server.MapPath(".") + "images/Imagename.gif";


iTextSharp.text.Image jpg = iTextSharp.text.Image.GetInstance(imageFilePath);


StringReader sr = newStringReader(sw.ToString());


// Page site and margin left, right, top, bottom is defined

Document pdfDoc = newDocument(PageSize.A4, 10f, 10f, 10f, 0f);


Paragraph paragraph = newParagraph("For further information please contact:\n\Test content");


//Resize image depend upon your need

jpg.ScaleToFit(70, 40);


jpg.SetAbsolutePosition(7, 780);


HTMLWorker htmlparser = newHTMLWorker(pdfDoc);

PdfWriter .GetInstance(pdfDoc, Response.OutputStream);












There are some other options we can use according to our need while developing the application.


jpg.ScaleToFit(250f, 250f);

jpg.Border = Rectangle.BOX;

jpg.BorderColor = Color.YELLOW;

jpg.BorderWidth = 5f;


pdfDoc.Add(newParagraph("Original Width: " + jpg.Width.ToString()));

pdfDoc.Add(newParagraph("Original Height " + jpg.Height.ToString()));

pdfDoc.Add(newParagraph("Scaled Width: " + jpg.ScaledWidth.ToString()));

pdfDoc.Add(newParagraph("Scaled Height " + jpg.ScaledHeight.ToString()));

float Resolution = jpg.Width / jpg.ScaledWidth * 72f;

pdfDoc.Add(newParagraph("Resolution: " + Resolution));


jpg.Rotation = (float)Math.PI / 2;

jpg.RotationDegrees = 90f;


It will rotate the image on specific angel.

Helpful Website Url
Share this article   |    Print    |    Article read by 10468 times
Rohit kakria
I am software developer
Related Articles: No related article
Related Interview Questions: No related interview question