Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# ASP.NET itextsharp
#region
private void M_createPDF()
{
string path = Server.MapPath("PDF");
/*Logo Path*/
string strLogoPath = Server.MapPath("images") + "\\clock.png";
Document doc = new Document(PageSize.LETTER, 25F, 25F, 50F, 25F);
PdfWriter.GetInstance(doc, new FileStream(path + "/Font.pdf", FileMode.Create));
 

string imageFilePath = Server.MapPath(".") + "/images/pageheader_bckgr.gif";
 
iTextSharp.text.Image jpg = iTextSharp.text.Image.GetInstance(imageFilePath);
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);
 

doc.Open();
 
Rectangle page = doc.PageSize;
Font Verdana = FontFactory.GetFont("Verdana", 10F, Font.NORMAL, BaseColor.BLACK);
 
PdfPTable table = new PdfPTable(5);
 
PdfPCell cell = new PdfPCell(new Phrase("Header spanning 3 columns"));

//float[] widths = new float[] { 1f, 1f };
 
//table.SetWidths(widths);
 
cell.Colspan = 5;
cell.BackgroundColor = BaseColor.GRAY;
cell.HorizontalAlignment = 1; //0=Left, 1=Centre, 2=Right
table.AddCell(cell);
 
table.AddCell("Sr.No");
table.AddCell("Name");
table.AddCell("J1");
table.AddCell("J2");
table.AddCell("J3");

for (int i = 0; i < 10; i++)
{
if (i == 5)
{
cell = new PdfPCell(new Phrase("Header spanning 3 columns"));
cell.BackgroundColor = BaseColor.GRAY;
cell.Colspan = 5;
cell.HorizontalAlignment = 1; //0=Left, 1=Centre, 2=Right
table.AddCell(cell);
}
 
cell = new PdfPCell(new Phrase(i.ToString()));
Rectangle _r = new Rectangle(2, 2, 2, 2, 180);
cell.FixedHeight = 20.0f;
 
//cell.Width = _r.Width;
 
table.AddCell(cell);
table.AddCell("Name" + i.ToString());
table.AddCell(i.ToString());
table.AddCell((i + 1).ToString());
table.AddCell((i + 2).ToString());
}
 
doc.Add(jpg);
doc.Add(table);
doc.Close();
 

 
}
private static void AddImageInCell(PdfPCell cell, iTextSharp.text.Image image, float fitWidth, float fitHight, int Alignment)
{
image.ScaleToFit(fitWidth, fitHight);
image.Alignment = Alignment;
cell.AddElement(image);
}
private void AddtextCell(PdfPTable table, PdfPCell cell)
{
cell.Colspan = 3;
cell.HorizontalAlignment = 1; //0=Left, 1=Centre, 2=Right
cell.Border = 0;
table.AddCell(cell);
}
private void AddtextCell(PdfPTable table, PdfPCell cell, float paddingLeft, float paddingRight)
{
cell.Colspan = 3;
cell.HorizontalAlignment = 0; //0=Left, 1=Centre, 2=Right
cell.PaddingLeft = paddingLeft;
cell.PaddingRight = paddingRight;
cell.Border = 0;
table.AddCell(cell);
}
private void AddtextCell(PdfPTable table, PdfPCell cell, float paddingLeft, float paddingRight, int hAlign)
{
cell.Colspan = 3;
cell.HorizontalAlignment = hAlign; //0=Left, 1=Centre, 2=Right
cell.PaddingLeft = paddingLeft;
cell.PaddingRight = paddingRight;
cell.Border = 0;
table.AddCell(cell);
}
private static void AddtextCell(PdfPTable table, PdfPCell cell, int Colspan, int HorizontalAlignment, int Border)
{
cell.Colspan = Colspan;
cell.HorizontalAlignment = HorizontalAlignment; //0=Left, 1=Centre, 2=Right
cell.Border = Border;
table.AddCell(cell);
}
#endregion
Posted 26-Oct-12 2:10am

1 solution

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 6,165
1 DamithSL 4,658
2 Maciej Los 4,107
3 Kornfeld Eliyahu Peter 3,649
4 Sergey Alexandrovich Kryukov 3,382


Advertise | Privacy | Mobile
Web04 | 2.8.141220.1 | Last Updated 26 Oct 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100