Itextsharp In C#

Definition 

Itextsharp is an advanced tool library which is used for creating complex pdf repors. itext is used by different techonologies --  Android , .NET, Java and GAE developer use it to enhance their applications with PDF functionality. It creates documents and reports based on data from databases or xml files and Merge or split pages from existing PDF files.

How to use-

Step 1

Download itextsharp.dll

Here is the link for download.

N
amespace
  1. using iTextSharp.text;  
  2.   
  3. using iTextSharp.text.pdf;

First We use

  1. Document doc = new Document(PageSize.A4, 7f, 5f, 5f, 0f);  

For set font style

  1. iTextSharp.text.Font mainFont = FontFactory.GetFont("Segoe UI",22, new iTextSharp.text.BaseColor(System.Drawing.ColorTranslator.FromHtml("#999")));  

For Paragraph

  1. Phrase mainPharse = new Phrase();  

For image

  1. iTextSharp.text.Image mobileImage = iTextSharp.text.Image.GetInstance(HttpContext.Current.Server.MapPath("~/goodmorning.jpg"));  
  2.   
  3. mobileImage.ScaleToFit(10, 10);  
  4.   
  5. Chunk cmobImg = new Chunk(mobileImage, 0, -2);  

Add References
  • iTextSharp.text.dll
  • iTextSharp.text.pdf.dll


Add a aspx page with name profile name,


Add table in sql server, 


Bind grid view from database.
---Source code for ProfileResume.aspx,
  1. <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="ProfileResume.aspx.cs"  
  2.   
  3. Inherits="architecture.ProfileResume" %>  
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.Web.UI;  
  6. using System.Web.UI.WebControls;  
  7. using System.Data;  
  8. using System.Configuration;  
  9. using System.Data.SqlClient;  
  10. using System.IO;  
  11. using System.Collections;  
  12. using System.Linq.Expressions;  
  13. namespace architecture  
  14. {  
  15.     public partial class ProfileResume: System.Web.UI.Page   
  16.     {  
  17.         public static string constr = ConfigurationManager.ConnectionStrings["ConString"].ConnectionString;  
  18.         protected void Page_Load(object sender, EventArgs e)  
  19.         {  
  20.                 if (!IsPostBack)   
  21.                 {  
  22.                     ShowData();  
  23.                 }  
  24.             }  
  25.             //method for Displaying Data in Gridview  
  26.         protected void ShowData()  
  27.         {  
  28.             SqlConnection con = new SqlConnection(constr);  
  29.             string str = "Select * from TbDemo; select * from TbDemo";  
  30.             con.Open();  
  31.             SqlCommand cmd = new SqlCommand(str, con);  
  32.             DataTable dt = new DataTable();  
  33.             SqlDataReader reader = cmd.ExecuteReader();  
  34.             dt.Load(reader);  
  35.             DataView dv = dt.DefaultView;  
  36.             gvlist.DataSource = dv;  
  37.             gvlist.DataBind();  
  38.             int i = 0;  
  39.             con.Close();  
  40.         }  
  41.         protected void Submit(object sender, EventArgs e)  
  42.         {  
  43.             using(SqlConnection connection = new SqlConnection(constr))   
  44.             {  
  45.                 connection.Open();  
  46.                 SqlCommand command = new SqlCommand("SELECT FirstName,LastName,Email FROM TbDemo", connection);  
  47.                 SqlDataAdapter adapter = new SqlDataAdapter(command);  
  48.                 SqlCommandBuilder builder = new SqlCommandBuilder(adapter);  
  49.                 DataSet dataset = new DataSet();  
  50.                 adapter.Fill(dataset);  
  51.                 string line = new string('-', 40) + Environment.NewLine;  
  52.                 Console.WriteLine(builder.GetUpdateCommand().CommandText);  
  53.                 Console.WriteLine(line);  
  54.                 Console.WriteLine(builder.GetDeleteCommand().CommandText);  
  55.                 Console.WriteLine(line);  
  56.                 Console.WriteLine(builder.GetInsertCommand().CommandText);  
  57.                 Console.WriteLine(line);  
  58.                 Console.ReadLine();  
  59.                 // insert a row  
  60.                 SqlCommand insert = builder.GetInsertCommand();  
  61.                 insert.Parameters["@P1"].Value = "Ak";  
  62.                 insert.Parameters["@P2"].Value = "KK";  
  63.                 insert.Parameters["@P3"].Value = "Paul Kimmel";  
  64.                 insert.ExecuteNonQuery();  
  65.                 adapter.Fill(dataset);  
  66.                 DataRow[] rows = dataset.Tables[0].Select("ID = '4'");  
  67.                 if (rows.Length == 1) Console.WriteLine(rows[0]["FirstName"]);  
  68.                 Console.ReadLine();  
  69.             }  
  70.         }  
  71.         protected void OnCommandPdf_click(object sender, GridViewCommandEventArgs e)  
  72.         {  
  73.             GridViewRow gvr = (GridViewRow)(((Button) e.CommandSource).NamingContainer);  
  74.             string id = e.CommandArgument.ToString();  
  75.             Response.Redirect("PdfProfile.aspx?Id=" + id);  
  76.         }  
  77.     }  
  78. }  
Code behind for ProfileResume.aspx.cs
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.Web.UI;  
  6. using System.Web.UI.WebControls;  
  7. using System.Data;  
  8. using System.Configuration;  
  9. using System.Data.SqlClient;  
  10. using System.IO;  
  11. using System.Collections;  
  12. using System.Linq.Expressions;  
  13. namespace architecture   
  14. {  
  15.     public partial class ProfileResume: System.Web.UI.Page   
  16.     {  
  17.         public static string constr = ConfigurationManager.ConnectionStrings["ConString"].ConnectionString;  
  18.         protected void Page_Load(object sender, EventArgs e)   
  19.         {  
  20.                 if (!IsPostBack)   
  21.                 {  
  22.                     ShowData();  
  23.                 }  
  24.             }  
  25.             //method for Displaying Data in Gridview  
  26.         protected void ShowData()  
  27.         {  
  28.             SqlConnection con = new SqlConnection(constr);  
  29.             string str = "Select * from TbDemo; select * from TbDemo";  
  30.             con.Open();  
  31.             SqlCommand cmd = new SqlCommand(str, con);  
  32.             DataTable dt = new DataTable();  
  33.             SqlDataReader reader = cmd.ExecuteReader();  
  34.             dt.Load(reader);  
  35.             DataView dv = dt.DefaultView;  
  36.             gvlist.DataSource = dv;  
  37.             gvlist.DataBind();  
  38.             int i = 0;  
  39.             con.Close();  
  40.         }  
  41.         protected void Submit(object sender, EventArgs e)   
  42.         {  
  43.             using(SqlConnection connection = new SqlConnection(constr))   
  44.             {  
  45.                 connection.Open();  
  46.                 SqlCommand command = new SqlCommand("SELECT FirstName,LastName,Email FROM TbDemo", connection);  
  47.                 SqlDataAdapter adapter = new SqlDataAdapter(command);  
  48.                 SqlCommandBuilder builder = new SqlCommandBuilder(adapter);  
  49.                 DataSet dataset = new DataSet();  
  50.                 adapter.Fill(dataset);  
  51.                 string line = new string('-', 40) + Environment.NewLine;  
  52.                 Console.WriteLine(builder.GetUpdateCommand().CommandText);  
  53.                 Console.WriteLine(line);  
  54.                 Console.WriteLine(builder.GetDeleteCommand().CommandText);  
  55.                 Console.WriteLine(line);  
  56.                 Console.WriteLine(builder.GetInsertCommand().CommandText);  
  57.                 Console.WriteLine(line);  
  58.                 Console.ReadLine();  
  59.                 // insert a row  
  60.                 SqlCommand insert = builder.GetInsertCommand();  
  61.                 insert.Parameters["@P1"].Value = "Ak";  
  62.                 insert.Parameters["@P2"].Value = "KK";  
  63.                 insert.Parameters["@P3"].Value = "Paul Kimmel";  
  64.                 insert.ExecuteNonQuery();  
  65.                 adapter.Fill(dataset);  
  66.                 DataRow[] rows = dataset.Tables[0].Select("ID = '4'");  
  67.                 if (rows.Length == 1) Console.WriteLine(rows[0]["FirstName"]);  
  68.                 Console.ReadLine();  
  69.             }  
  70.         }  
  71.         protected void OnCommandPdf_click(object sender, GridViewCommandEventArgs e)   
  72.         {  
  73.             GridViewRow gvr = (GridViewRow)(((Button) e.CommandSource).NamingContainer);  
  74.             string id = e.CommandArgument.ToString();  
  75.             Response.Redirect("PdfProfile.aspx?Id=" + id);  
  76.         }  
  77.     }  
  78. }  
output

Click On make resume button for pdf
Step a – add page PdfProfile.aspx,
  1. <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="PdfProfile.aspx.cs" Inherits="architecture.PdfProfile" %>  
  2.     <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
  3.     <html xmlns="http://www.w3.org/1999/xhtml">  
  4.   
  5.     <head runat="server">  
  6.         <title></title>  
  7.     </head>  
  8.   
  9.     <body>  
  10.         <form id="form1" runat="server">  
  11.             <div> </div>  
  12.         </form>  
  13.     </body>  
  14.   
  15.     </html>  
And code behind
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.Web.UI;  
  6. using System.Web.UI.WebControls;  
  7. using System.Data;  
  8. using System.Configuration;  
  9. using System.Data.SqlClient;  
  10. using System.IO;  
  11. using System.Collections;  
  12. using iTextSharp.text;  
  13. using iTextSharp.text.pdf;  
  14. using System.Text;  
  15. using System.Drawing;  
  16. namespace architecture  
  17. {  
  18.     public partial class PdfProfile: System.Web.UI.Page   
  19.     {  
  20.         public static string constr = ConfigurationManager.ConnectionStrings["ConString"].ConnectionString;  
  21.         protected void Page_Load(object sender, EventArgs e)   
  22.         {  
  23.             if (Request.QueryString["Id"] != null)  
  24.             {  
  25.                 SqlConnection con = new SqlConnection(constr);  
  26.                 Document doc = new Document(PageSize.A4, 7 f, 5 f, 5 f, 0 f);  
  27.                 string str1 = "Select * from TbDemo where id='" + Request.QueryString["Id"] + "'";  
  28.                 con.Open();  
  29.                 SqlCommand cmd = new SqlCommand(str1, con);  
  30.                 DataTable dt = new DataTable();  
  31.                 SqlDataReader reader = cmd.ExecuteReader();  
  32.                 dt.Load(reader);  
  33.                 con.Close();  
  34.                 //DataView dv = dt.DefaultView;  
  35.                 string filePath = HttpContext.Current.Server.MapPath("~/Resume/" + Request.QueryString["Id"]);  
  36.                 if (File.Exists(filePath + "/" + "UserProfile.pdf"))  
  37.                 {  
  38.                     Response.Redirect("~/ProfileReport.aspx?ID=" + Request.QueryString["Id"]);  
  39.                 } else   
  40.                 {  
  41.                     try  
  42.                     {  
  43.                         //Create [Portfolio] directory If does not exist  
  44.                         if (!Directory.Exists(filePath))   
  45.                           
  46.                         {  
  47.                             Directory.CreateDirectory(filePath);  
  48.                         }  
  49.                         string newFilePath = filePath + "\\" + "UserProfile.pdf"; //GetFileName(filePath, "UserProfile"); //  
  50.                         PdfWriter writer = PdfWriter.GetInstance(doc, new FileStream(newFilePath, FileMode.Create));  
  51.                         doc.Open();  
  52.                         //Get the user photos to be show in pdf  
  53.                         Int64 _userProfileID = Int64.Parse(Request.QueryString["Id"]);  
  54.                         DataTable dtPhotos = dt;  
  55.                         iTextSharp.text.Font mainFont = FontFactory.GetFont("Segoe UI", 22, new iTextSharp.text.BaseColor(System.Drawing.ColorTranslator.FromHtml("#999")));  
  56.                         iTextSharp.text.Font infoFont1 = FontFactory.GetFont("Kalinga", 10, new iTextSharp.text.BaseColor(System.Drawing.ColorTranslator.FromHtml("#666")));  
  57.                         iTextSharp.text.Font expHeadFond = FontFactory.GetFont("Calibri (Body)", 12, new iTextSharp.text.BaseColor(System.Drawing.ColorTranslator.FromHtml("#666")));  
  58.                         PdfContentByte contentByte = writer.DirectContent;  
  59.                         DataTable objDataTable = dt;  
  60.                         ColumnText ct = new ColumnText(contentByte);  
  61.                         //Create the font for show the name of user  
  62.                         doc.Open();  
  63.                         PdfPTable modelInfoTable = new PdfPTable(1);  
  64.                         modelInfoTable.TotalWidth = 100 f;  
  65.                         modelInfoTable.HorizontalAlignment = Element.ALIGN_LEFT;  
  66.                         PdfPCell modelInfoCell1 = new PdfPCell()  
  67.                         {  
  68.                             BorderWidthBottom = 0 f, BorderWidthTop = 0 f, BorderWidthLeft = 0 f, BorderWidthRight = 0 f  
  69.                         };  
  70.                         //Set right hand the first heading  
  71.                         Phrase mainPharse = new Phrase();  
  72.                         Chunk mChunk = new Chunk(dt.Rows[0]["FirstName"].ToString() + " " + dt.Rows[0]["LastName"].ToString(), mainFont);  
  73.                         mainPharse.Add(mChunk);  
  74.                         mainPharse.Add(new Chunk(Environment.NewLine));  
  75.                         //Set the user role  
  76.                         Chunk infoChunk1 = new Chunk("Profile - " + dt.Rows[0]["RoleName"].ToString(), infoFont1);  
  77.                         mainPharse.Add(infoChunk1);  
  78.                         mainPharse.Add(new Chunk(Environment.NewLine));  
  79.                         //Set the user Gender  
  80.                         Chunk infoChunk21 = new Chunk("Gender - " + dt.Rows[0]["Gender"].ToString(), infoFont1);  
  81.                         mainPharse.Add(infoChunk21);  
  82.                         mainPharse.Add(new Chunk(Environment.NewLine));  
  83.                         //Set the user age  
  84.                         Chunk infoChunk22 = new Chunk("Age - " + dt.Rows[0]["Age"].ToString(), infoFont1);  
  85.                         mainPharse.Add(infoChunk22);  
  86.                         mainPharse.Add(new Chunk(Environment.NewLine));  
  87.                         iTextSharp.text.Font infoFont2 = FontFactory.GetFont("Kalinga", 10, new iTextSharp.text.BaseColor(System.Drawing.ColorTranslator.FromHtml("#848282")));  
  88.                         string Location = dt.Rows[0]["Location"].ToString() == string.Empty ? string.Empty : dt.Rows[0]["Location"].ToString();  
  89.                         Chunk infoChunk2 = new Chunk("Address -" + Location, infoFont2);  
  90.                         mainPharse.Add(infoChunk2);  
  91.                         modelInfoCell1.AddElement(mainPharse);  
  92.                         //Set the mobile image and number  
  93.                         Phrase mobPhrase = new Phrase();  
  94.                         // iTextSharp.text.Image mobileImage = iTextSharp.text.Image.GetInstance(HttpContext.Current.Server.MapPath("~/goodmorning.jpg"));  
  95.                         // mobileImage.ScaleToFit(10, 10);  
  96.                         //Chunk cmobImg = new Chunk(mobileImage, 0, -2);  
  97.                         Chunk cmob = new Chunk("Contact " + dt.Rows[0]["Phone"].ToString(), infoFont2);  
  98.                         //mobPhrase.Add(cmobImg);  
  99.                         mobPhrase.Add(cmob);  
  100.                         modelInfoCell1.AddElement(mobPhrase);  
  101.                         //Set the message image and email id  
  102.                         Phrase msgPhrase = new Phrase();  
  103.                         // iTextSharp.text.Image msgImage = iTextSharp.text.Image.GetInstance(HttpContext.Current.Server.MapPath("~/goodmorning.jpg"));  
  104.                         //msgImage.ScaleToFit(10, 10);  
  105.                         //Chunk msgImg = new Chunk(msgImage, 0, -2);  
  106.                         iTextSharp.text.Font msgFont = FontFactory.GetFont("Kalinga", 10, new iTextSharp.text.BaseColor(System.Drawing.Color.Pink));  
  107.                         Chunk cmsg = new Chunk("EMail - " + dt.Rows[0]["Email"].ToString(), msgFont);  
  108.                         //msgPhrase.Add(msgImg);  
  109.                         msgPhrase.Add(cmsg);  
  110.                         //Set the line after the user small information  
  111.                         iTextSharp.text.Font lineFont = FontFactory.GetFont("Kalinga", 10, new iTextSharp.text.BaseColor(System.Drawing.ColorTranslator.FromHtml("#e8e8e8")));  
  112.                         Chunk lineChunk = new Chunk("____________________________________________________________________", lineFont);  
  113.                         msgPhrase.Add(new Chunk(Environment.NewLine));  
  114.                         msgPhrase.Add(lineChunk);  
  115.                         modelInfoCell1.AddElement(msgPhrase);  
  116.                         modelInfoTable.AddCell(modelInfoCell1);  
  117.                         //Set the biography  
  118.                         PdfPCell cell1 = new PdfPCell() {  
  119.                             BorderWidthBottom = 0 f, BorderWidthTop = 0 f, BorderWidthLeft = 0 f, BorderWidthRight = 0 f  
  120.                         };  
  121.                         cell1.PaddingTop = 5 f;  
  122.                         Phrase bioPhrase = new Phrase();  
  123.                         Chunk bioChunk = new Chunk("Biography", mainFont);  
  124.                         bioPhrase.Add(bioChunk);  
  125.                         bioPhrase.Add(new Chunk(Environment.NewLine));  
  126.                         Chunk bioInfoChunk = new Chunk(dt.Rows[0]["BriefBio"].ToString(), infoFont1);  
  127.                         bioPhrase.Add(bioInfoChunk);  
  128.                         bioPhrase.Add(new Chunk(Environment.NewLine));  
  129.                         bioPhrase.Add(lineChunk);  
  130.                         cell1.AddElement(bioPhrase);  
  131.                         modelInfoTable.AddCell(cell1);  
  132.                         PdfPCell cellExp = new PdfPCell()  
  133.                         {  
  134.                             BorderWidthBottom = 0 f, BorderWidthTop = 0 f, BorderWidthLeft = 0 f, BorderWidthRight = 0 f  
  135.                         };  
  136.                         cellExp.PaddingTop = 5 f;  
  137.                         Phrase ExperiencePhrase = new Phrase();  
  138.                         Chunk ExperienceChunk = new Chunk("Experience", mainFont);  
  139.                         ExperiencePhrase.Add(ExperienceChunk);  
  140.                         cellExp.AddElement(ExperiencePhrase);  
  141.                         modelInfoTable.AddCell(cellExp);  
  142.                         if (dt.Rows.Count > 0)   
  143.                         {  
  144.                             for (int i = 0; i < dt.Rows.Count; i++)  
  145.                             {  
  146.                                 //Set the experience  
  147.                                 PdfPCell expcell = new PdfPCell()   
  148.                                 {  
  149.                                     BorderWidthBottom = 0 f, BorderWidthTop = 0 f, BorderWidthLeft = 0 f, BorderWidthRight = 0 f  
  150.                                 };  
  151.                                 expcell.PaddingTop = 5 f;  
  152.                                 Phrase expPhrase = new Phrase();  
  153.                                 StringBuilder expStringBuilder = new StringBuilder();  
  154.                                 StringBuilder expStringBuilder1 = new StringBuilder();  
  155.                                 //Set the experience details  
  156.                                 expStringBuilder.Append(dt.Rows[i]["Title"].ToString() + Environment.NewLine);  
  157.                                 expStringBuilder.Append(dt.Rows[i]["CompanyName"].ToString() + Environment.NewLine);  
  158.                                 expStringBuilder.Append(dt.Rows[i]["ComanyAddress"].ToString() + Environment.NewLine);  
  159.                                 expStringBuilder1.Append("From " + dt.Rows[i]["From"].ToString() + " To " + dt.Rows[i]["to"].ToString() + Environment.NewLine);  
  160.                                 // expPhrase.Add(new Chunk(Environment.NewLine));  
  161.                                 Chunk expDetailChunk = new Chunk(expStringBuilder.ToString(), expHeadFond);  
  162.                                 expPhrase.Add(expDetailChunk);  
  163.                                 expPhrase.Add(new Chunk(expStringBuilder1.ToString(), infoFont2));  
  164.                                 expcell.AddElement(expPhrase);  
  165.                                 modelInfoTable.AddCell(expcell);  
  166.                                 if (dt.Rows[i]["Description"].ToString().Length > 600) {  
  167.                                     PdfPCell pCell1 = new PdfPCell()  
  168.                                     {  
  169.                                         BorderWidth = 0 f  
  170.                                     };  
  171.                                     PdfPCell pCell2 = new PdfPCell()   
  172.                                     {  
  173.                                         BorderWidth = 0 f  
  174.                                     };  
  175.                                     Phrase ph1 = new Phrase();  
  176.                                     Phrase ph2 = new Phrase();  
  177.                                     string experience1 = dt.Rows[i]["Description"].ToString().Substring(0, 599);  
  178.                                     string experience2 = dt.Rows[i]["Description"].ToString().Substring(599, dt.Rows[i]["Description"].ToString().Length - 600);  
  179.                                     ph1.Add(new Chunk(experience1, infoFont1));  
  180.                                     ph2.Add(new Chunk(experience2, infoFont1));  
  181.                                     pCell1.AddElement(ph1);  
  182.                                     pCell2.AddElement(ph2);  
  183.                                     modelInfoTable.AddCell(pCell1);  
  184.                                     modelInfoTable.AddCell(pCell2);  
  185.                                 } else {  
  186.                                     PdfPCell pCell1 = new PdfPCell()   
  187.                                     {  
  188.                                         BorderWidth = 0 f  
  189.                                     };  
  190.                                     Phrase ph1 = new Phrase();  
  191.                                     string experience1 = dt.Rows[i]["Description"].ToString();  
  192.                                     ph1.Add(new Chunk(experience1, infoFont1));  
  193.                                     pCell1.AddElement(ph1);  
  194.                                     modelInfoTable.AddCell(pCell1);  
  195.                                 }  
  196.                             }  
  197.                         }  
  198.                         doc.Add(modelInfoTable);  
  199.                         //Set the footer  
  200.                         PdfPTable footerTable = new PdfPTable(1);  
  201.                         footerTable.TotalWidth = 644 f;  
  202.                         footerTable.LockedWidth = true;  
  203.                         PdfPCell footerCell = new PdfPCell(new Phrase("Resume"));  
  204.                         footerCell.BackgroundColor = new iTextSharp.text.BaseColor(Color.Black);  
  205.                         iTextSharp.text.Image footerImage = iTextSharp.text.Image.GetInstance(HttpContext.Current.Server.MapPath("~/goodmorning.jpg"));  
  206.                         footerImage.SpacingBefore = 5 f;  
  207.                         footerImage.SpacingAfter = 5 f;  
  208.                         footerImage.ScaleToFit(100 f, 22 f);  
  209.                         footerCell.AddElement(footerImage);  
  210.                         footerCell.MinimumHeight = 30 f;  
  211.                         iTextSharp.text.Font newFont = FontFactory.GetFont("Segoe UI, Lucida Grande, Lucida Grande", 8, new iTextSharp.text.BaseColor(Color.White));  
  212.                         Paragraph rightReservedLabel = new Paragraph("© " + DateTime.Now.Year + " Resume. All rights reserved.", newFont);  
  213.                         footerCell.AddElement(rightReservedLabel);  
  214.                         footerCell.PaddingLeft = 430 f;  
  215.                         footerTable.AddCell(footerCell);  
  216.                         footerTable.WriteSelectedRows(0, -1, 0, doc.PageSize.Height - 795, writer.DirectContent);  
  217.                     } catch (Exception)  
  218.                     {  
  219.                         throw;  
  220.                     } finally   
  221.                     {  
  222.                         doc.Close();  
  223.                     }  
  224.                     Response.Redirect("~/ProfileReport.aspx?ID=" + Request.QueryString["Id"]);  
  225.                 }  
  226.             }  
  227.         }  
  228.     }  
  229. }  
Add One more aspx page for make pdf page a. Source code ProfileReport.aspx,
  1. <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="ProfileReport.aspx.cs" Inherits="architecture.ProfileReport" %>  
  2.     <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
  3.     <html xmlns="http://www.w3.org/1999/xhtml">  
  4.   
  5.     <head runat="server">  
  6.         <title></title>  
  7.     </head>  
  8.   
  9.     <body>  
  10.         <form id="form1" runat="server">  
  11.             <div> </div>  
  12.         </form>  
  13.     </body>  
  14.   
  15.     </html>  
Code behind page
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.Web.UI;  
  6. using System.Web.UI.WebControls;  
  7. using System.IO;  
  8. using System.Net;  
  9. namespace architecture   
  10. {  
  11.     public partial class ProfileReport: System.Web.UI.Page   
  12.     {  
  13.         protected void Page_Load(object sender, EventArgs e)   
  14.         {  
  15.             string filePath = Server.MapPath(string.Concat("~/Resume/", Request.QueryString["Id"]));  
  16.             try   
  17.             {  
  18.                 //Open pdf in web browser  
  19.                 WebClient client = new WebClient();  
  20.                 if (!string.IsNullOrEmpty(Request.QueryString["Id"]))   
  21.                 {  
  22.                     Byte[] buffer = client.DownloadData(filePath + "/" + "UserProfile.pdf");  
  23.                     if (buffer != null) {  
  24.                         HttpContext.Current.Response.ContentType = "application/pdf";  
  25.                         HttpContext.Current.Response.AddHeader("content-length", buffer.Length.ToString());  
  26.                         HttpContext.Current.Response.BinaryWrite(buffer);  
  27.                         // HttpContext.Current.Response.End();  
  28.                         HttpContext.Current.ApplicationInstance.CompleteRequest();  
  29.                     }  
  30.                 }  
  31.             } catch (Exception ex) {  
  32.                 throw ex;  
  33.             }  
  34.         }  
  35.     }  
  36. }  
 


Similar Articles