Code Simplified – Viral Sarvaiya

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

Posts Tagged ‘PDF’

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

Posted in ASP.NET | Tagged: , , , , , , , | 14 Comments »

Make a PDF file from asp.net

Posted by Viral Sarvaiya on July 8, 2009


to make a pdf file hear I demonstrate as below,

you have to take a itextsharp.dll file from the following link

http://sourceforge.net/projects/itextsharp/

now the code is as follows,

Imports iTextSharp.text
Imports iTextSharp.text.pdf

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If IsPostBack = False Then
makepdf()
End If
End Sub

Sub makepdf()

Dim labelFont14 As Font = FontFactory.GetFont(FontFactory.TIMES_ROMAN, 14, Font.NORMAL)
labelFont14.Color = New iTextSharp.text.Color(0, 25, 25)

Dim labelFont12 As Font = FontFactory.GetFont(FontFactory.TIMES_ROMAN, 12, Font.NORMAL)
labelFont12.Color = New iTextSharp.text.Color(0, 25, 25)

'as like that you can define the font.

PageSize.A4.BackgroundColor = New iTextSharp.text.Color(255, 255, 255)
Dim doc As Document = New Document(PageSize.A4, 1, 0, 0, 30)
PdfWriter.GetInstance(doc, New FileStream(Server.MapPath("path/filename.pdf"),FileMode.Create))
doc.AddAuthor("Author name")

doc.Open()
doc.NewPage()

Dim tablemain As Table = New Table(1)

'here table(1) denotes the 1 collumns, when u add the cell then it creates the one column and then 2nd cell is make the 2nd row for that column

tablemain.BorderWidth = 0
tablemain.Width = 100.0F
tablemain.Padding = 0
tablemain.Spacing = 0

Dim celln As New Cell(New Chunk(“string” & vbCrLf, labelFont14))
celln.Width = 90.0F
celln.HorizontalAlignment = Element.ALIGN_CENTER
tablemain.AddCell(celln)

doc.Add(tablemain)

doc.Close()

as per your requirements you can set the collumns and rows

Posted in ASP.NET | Tagged: , , , , , | 1 Comment »

 
%d bloggers like this: