Alignment of Pdf document by using c# code
public partial class Admin_facultydet : System.Web.UI.Page
{
// SqlConnection cn = new
SqlConnection(ConfigurationManager.ConnectionStrings["ProjectConnectionString"].ToString());
int va;
protected void Page_Load(object sender, EventArgs e)
{
va = (int)Session["FID"];
if (Session["Username"] == null)
{
Response.Redirect(@"~\home.aspx");
}
}
private static PdfPCell PhraseCell(Phrase phrase, int align)
{
PdfPCell cell = new PdfPCell(phrase);
cell.BorderColor = BaseColor.WHITE;
//cell.VerticalAlignment = PdfCell.ALIGN_TOP;
cell.HorizontalAlignment = align;
cell.PaddingBottom = 2f;
cell.PaddingTop = 0f;
return cell;
}
private DataTable GetData(string query)
{
string conString =
ConfigurationManager.ConnectionStrings["ProjectConnectionString"].ConnectionString;
SqlCommand cmd = new SqlCommand(query);
using (SqlConnection cn = new SqlConnection(conString))
{
using (SqlDataAdapter da = new SqlDataAdapter())
{
cmd.Connection = cn;
da.SelectCommand = cmd;
using (DataTable dt = new DataTable())
{
da.Fill(dt);
return dt;
}
}
}
}
public void fdown()
{
DataRow dr = GetData("select * from Personal_det").Rows[0];
Document doc = new Document(PageSize.A4, 88f, 88f, 10f, 10f);
Font NormalFont = FontFactory.GetFont("Arial", 12, Font.NORMAL,
BaseColor.BLACK);
using (System.IO.MemoryStream m = new System.IO.MemoryStream())
{
PdfWriter w = PdfWriter.GetInstance(doc, m);
Phrase phrase = null;
PdfPCell cell = null;
PdfPTable table = null;
BaseColor color = null;
Paragraph para = null;
Font times = null;
BaseFont bfTimes = null;
doc.Open();
table = new PdfPTable(2);
cell = PhraseCell(new Phrase("Faculty Profile",
FontFactory.GetFont("Arial", 12, Font.UNDERLINE,
BaseColor.BLACK)), PdfPCell.ALIGN_CENTER);
//table.SpacingBefore = 20f;
cell.Colspan = 2;
table.AddCell(cell);
cell = PhraseCell(new Phrase(), PdfPCell.ALIGN_CENTER);
cell.Colspan = 2;
cell.PaddingBottom = 30f;
//FID
table.AddCell(PhraseCell(new Phrase("Faculty Code:",
FontFactory.GetFont("Arial", 8, Font.BOLD, BaseColor.BLACK)),
PdfPCell.ALIGN_LEFT));
table.AddCell(PhraseCell(new Phrase("mahe" + dr["FID"],
FontFactory.GetFont("Arial", 8, Font.NORMAL,
BaseColor.BLACK)), PdfPCell.ALIGN_LEFT));
cell = PhraseCell(new Phrase(), PdfPCell.ALIGN_CENTER);
cell.Colspan = 2;
cell.PaddingBottom = 10f;
table.AddCell(cell);
//Name
table.AddCell(PhraseCell(new Phrase("Name:",
FontFactory.GetFont("Arial", 8, Font.BOLD, BaseColor.BLACK)),
PdfPCell.ALIGN_LEFT));
table.AddCell(PhraseCell(new Phrase(dr["Name"].ToString(),
FontFactory.GetFont("Arial", 8, Font.BOLD, BaseColor.BLACK)),
PdfPCell.ALIGN_LEFT));
cell = PhraseCell(new Phrase(), PdfPCell.ALIGN_CENTER);
cell.Colspan = 2;
cell.PaddingBottom = 10f;
table.AddCell(cell);
//Date of Birth
table.AddCell(PhraseCell(new Phrase("Date of Birth:",
FontFactory.GetFont("Arial", 8, Font.BOLD, BaseColor.BLACK)),
PdfPCell.ALIGN_LEFT));
table.AddCell(PhraseCell(new
Phrase(Convert.ToDateTime(dr["DOB"]).ToString("dd MMMM,
yyyy"), FontFactory.GetFont("Arial", 8, Font.NORMAL,
BaseColor.BLACK)), PdfPCell.ALIGN_LEFT));
cell = PhraseCell(new Phrase(), PdfPCell.ALIGN_CENTER);
cell.Colspan = 2;
cell.PaddingBottom = 10f;
table.AddCell(cell);
//Phone Number
table.AddCell(PhraseCell(new Phrase("Phone Number:",
FontFactory.GetFont("Arial", 8, Font.BOLD, BaseColor.BLACK)),
PdfPCell.ALIGN_LEFT));
table.AddCell(PhraseCell(new
Phrase(Convert.ToInt64(dr["MobileNo"]).ToString(),
FontFactory.GetFont("Arial", 8, Font.NORMAL,
BaseColor.BLACK)), PdfPCell.ALIGN_LEFT));
cell = PhraseCell(new Phrase(), PdfPCell.ALIGN_CENTER);
cell.Colspan = 2;
cell.PaddingBottom = 10f;
table.AddCell(cell);
//Mail Id
table.AddCell(PhraseCell(new Phrase("Email ID:",
FontFactory.GetFont("Arial", 8, Font.BOLD, BaseColor.BLACK)),
PdfPCell.ALIGN_LEFT));
table.AddCell(PhraseCell(new Phrase(dr["EmailId"].ToString(),
FontFactory.GetFont("Arial", 8, Font.BOLD, BaseColor.BLACK)),
PdfPCell.ALIGN_LEFT));
cell = PhraseCell(new Phrase(), PdfPCell.ALIGN_CENTER);
cell.Colspan = 2;
cell.PaddingBottom = 10f;
table.AddCell(cell);
//Address
table.AddCell(PhraseCell(new Phrase("Res Address:",
FontFactory.GetFont("Arial", 8, Font.BOLD, BaseColor.BLACK)),
PdfPCell.ALIGN_LEFT));
phrase = new Phrase(new Chunk(dr["add1"] + "\n " + dr["add2"]
+ "\n " + dr["add3"] + "\n " + dr["Pincode"],
FontFactory.GetFont("Arial", 8, Font.NORMAL,
BaseColor.BLACK)));
table.AddCell(PhraseCell(phrase, PdfPCell.ALIGN_LEFT));
cell = PhraseCell(new Phrase(), PdfPCell.ALIGN_CENTER);
cell.Colspan = 2;
cell.PaddingBottom = 10f;
table.AddCell(cell);
table.SpacingAfter = 30f;
doc.Add(table);
}
Above code is used to display the information of the faculty in the PDF
document.But the alignment is not proper.I need the code to align the code
in a proper manner. left right alignment..ho w to do this using these
codes
No comments:
Post a Comment