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");

 

Response.Cache.SetCacheability(HttpCacheability.NoCache);

StringWriter sw = newStringWriter();

HtmlTextWriter hw = newHtmlTextWriter(sw);

 

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

dlPressRelease.RenderControl(hw);

 

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);

pdfDoc.Open();

 

pdfDoc.Add(jpg);

        

htmlparser.Parse(sr);

pdfDoc.Add(paragraph);

pdfDoc.Close();

Response.Write(pdfDoc);

Response.End();

 

 

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(jpg);

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
http://www.mikesdotnetting.com/Article/87/iTextSharp-Working-with-images
Share this article   |    Print    |    Article read by 10622 times
Author:
Rohit kakria
I am software developer
Related Articles: No related article
Related Interview Questions: No related interview question