Subin Thomas

Subin Thomas

  • 462
  • 4.8k
  • 80k

how to add text in pdf programatically ?

Sep 26 2019 1:25 AM
i have writter a code to export pdf from a gridview and its working fine i had total 39 columns in gridview in that i needed few columns so i hided rest of the unwanted columns from the grid.
 
its exporting pdf but i wanted to add some text in that pdf but i dont knw how to below is my code.
 
i want to add some text as disclaimer fixed text. 
 
  1. public void exportPDFfile()  
  2.        {  
  3.            Response.ContentType = "application/pdf";  
  4.            Response.AddHeader("content-disposition""attachment;filename=ForwardingLetter.pdf");  
  5.            Response.Cache.SetCacheability(HttpCacheability.NoCache);  
  6.            StringWriter sw = new StringWriter();  
  7.            HtmlTextWriter hw = new HtmlTextWriter(sw);  
  8.   
  9.            //hide the link button column  
  10.            gridallemploye.Columns[0].Visible = false;  
  11.            gridallemploye.Columns[2].Visible = false;  
  12.   
  13.            gridallemploye.Columns[3].Visible = false;  
  14.            gridallemploye.Columns[4].Visible = false;  
  15.            gridallemploye.Columns[5].Visible = false;  
  16.            gridallemploye.Columns[6].Visible = false;  
  17.            gridallemploye.Columns[7].Visible = false;  
  18.            gridallemploye.Columns[8].Visible = false;  
  19.            gridallemploye.Columns[9].Visible = false;  
  20.   
  21.            gridallemploye.Columns[11].Visible = false;  
  22.            gridallemploye.Columns[12].Visible = false;  
  23.            gridallemploye.Columns[13].Visible = false;  
  24.            gridallemploye.Columns[14].Visible = false;  
  25.            gridallemploye.Columns[15].Visible = false;  
  26.            gridallemploye.Columns[16].Visible = false;  
  27.            gridallemploye.Columns[17].Visible = false;  
  28.            gridallemploye.Columns[18].Visible = false;  
  29.            gridallemploye.Columns[19].Visible = false;  
  30.            gridallemploye.Columns[20].Visible = false;  
  31.            gridallemploye.Columns[21].Visible = false;  
  32.            gridallemploye.Columns[22].Visible = false;  
  33.            gridallemploye.Columns[23].Visible = false;  
  34.            gridallemploye.Columns[24].Visible = false;  
  35.            gridallemploye.Columns[25].Visible = false;  
  36.            gridallemploye.Columns[26].Visible = false;  
  37.            gridallemploye.Columns[27].Visible = false;  
  38.            gridallemploye.Columns[28].Visible = false;  
  39.            gridallemploye.Columns[29].Visible = false;  
  40.   
  41.            gridallemploye.Columns[30].Visible = false;  
  42.            gridallemploye.Columns[31].Visible = false;  
  43.            gridallemploye.Columns[32].Visible = false;  
  44.            gridallemploye.Columns[33].Visible = false;  
  45.            gridallemploye.Columns[34].Visible = false;  
  46.            
  47.   
  48.            gridallemploye.Columns[38].Visible = false;  
  49.   
  50.   
  51.            //Outputs server control content to a provided System.Web.UI.HtmlTextWriter  
  52.            gridallemploye.RenderControl(hw);  
  53.   
  54.            //load the html content to the string reader  
  55.            StringReader sr = new StringReader(sw.ToString());  
  56.   
  57.            //HTMLDocument  
  58.            //Document(Rectangle pageSize, float marginLeft, float marginRight, float marginTop, float marginBottom)  
  59.            Document document = new Document(PageSize.A4, 10f, 10f, 10f, 0f);  
  60.   
  61.            //iText class that allows you to convert HTML to PDF  
  62.            HTMLWorker htmlWorker = new HTMLWorker(document);  
  63.   
  64.            //When this PdfWriter is added to a certain PdfDocument,  
  65.            //the PDF representation of every Element added to this Document will be written to the outputstream.  
  66.            PdfWriter.GetInstance(document, Response.OutputStream);  
  67.   
  68.            //open the document  
  69.            document.Open();  
  70.   
  71.            htmlWorker.Parse(sr);  
  72.   
  73.            //close the document stream  
  74.            document.Close();  
  75.   
  76.            //write the content to the response stream  
  77.            Response.Write(document);  
  78.            Response.End();  
  79.        }  
 

Answers (2)