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/
This entry was posted on May 11, 2010 at 6:00 PM and is filed under ASP.NET. Tagged: ASP.NET, CSharp, iTextSharp, iTextSharp - Working with images, itextsharp background image in asp.net, iTextSharp: Generating a Basic PDF file (ASP.NET/C#), PDF, Set Background image to itextsharp table. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.



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…..
murilo said
you save my life
hahaha
thank you
Mike said
Can you show an example of the pdf please?
viralsarvaiya said
please try this code and check the pdf, that is ur example
khantil said
Thanx,
u really helped me, keep it up..!!
veena said
Thank you,it helped me alot
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.?
Abi Bwe'e said
thx a lot, being turning upside dwon for this simple code and logic (^_^)
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.