Code Simplified – Viral Sarvaiya

Code Simplified – Viral Sarvaiya, Web Developer Friends, dot net Developer, Sql Server Developer

Background Image in PDF File with ItextSharp in Asp.Net

Posted by Viral Sarvaiya on May 11, 2010

my past post
http://viralsarvaiya.wordpress.com/2009/07/08/make-a-pdf-file-from-asp-net/
is demonstrate how to develop the pdf file, but when i search in web that have less example for the image in background.

here i demonstrate how to set image in background in pdf file.

step 1 :

follow the link to create the pdf :
http://viralsarvaiya.wordpress.com/2009/07/08/make-a-pdf-file-from-asp-net/

stpe 2 :

here i put the whole code for the image in background.

dont forget to download and put the itextsharp.dll file in bin folder.


void makePDF()
 {
 Response.ContentType = "application/pdf";

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

 Response.Cache.SetCacheability(HttpCacheability.NoCache);

 string imageFilePath = Server.MapPath(".") + "/images/test.jpg";

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

 // Page site and margin left, right, top, bottom is defined
 Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 10f, 0f);

 //Resize image depend upon your need
 //For give the size to image
 jpg.ScaleToFit(3000, 770);

 //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, 69);

 PdfWriter.GetInstance(pdfDoc, Response.OutputStream);

 pdfDoc.Open();

 pdfDoc.NewPage();

 Paragraph paragraph = new Paragraph("this is the testing text for demonstrate the image is in background \n\n\n this is the testing text for demonstrate the image is in background");

 pdfDoc.Add(jpg);

 pdfDoc.Add(paragraph);

 pdfDoc.Close();

 Response.Write(pdfDoc);

 Response.End();
 }

for more knowledge of itextsharp :

http://somewebguy.wordpress.com/2009/05/08/itextsharp-simplify-your-html-to-pdf-creation/


http://xpode.com/ShowArticle.aspx?ArticleId=134

About these ads

11 Responses to “Background Image in PDF File with ItextSharp in Asp.Net”

  1. dian said

    hai…
    love your post…
    i followed the codes above, and the image’s shown only in the first page. How could i make it appear on every page…?

    • viralsarvaiya said

      dear dian,

      when u add the page, u can use the loop otherwise use that image come to every page u add.

      hope this will help you…..

  2. murilo said

    you save my life
    hahaha

    thank you

  3. Mike said

    Can you show an example of the pdf please?

  4. khantil said

    Thanx,
    u really helped me, keep it up..!!

  5. veena said

    Thank you,it helped me alot

  6. Venkat said

    Thanks..! but i need to add subject -header to each page which is retrieved in Binary fromat from database in asp.net (vb). IS this Possible.?

  7. Abi Bwe'e said

    thx a lot, being turning upside dwon for this simple code and logic (^_^)

  8. Tintu mary george said

    can you show me the same in php?

    • Viral Sarvaiya said

      Hi Tintu mary george,

      Sorry i don’t know anything about php, but i think logic should be same in all programming language.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

 
Follow

Get every new post delivered to your Inbox.

Join 44 other followers

%d bloggers like this: