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 https://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 : https://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

Advertisements

14 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…..

      • Steven Burton said

        This solution doesn’t really help when the page content automatically overflows across multiple pages since you don’t explicitly add a new page at every page break. How would you do it in such a case?

  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.

  9. diamonrays said

    Thanks.

    But i need an two makepdf functions to print one command in gridview. if print button clicks then print goes on based on grid date like.
    one is old format and one is new format. if the employee is joined before 2007 then old format.else then new format.

    Please send the code

  10. Hung Nguyen said

    Thank you, I have solved my problem.

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 )

Google+ photo

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

Connecting to %s

 
%d bloggers like this: