Wednesday, November 18, 2015

PDF Display


   Document mydoc = new Document(PageSize.A4);
   PdfPTable table1 = new PdfPTable(5);
   Paragraph emptyparagraph = new Paragraph(" ");
   BaseFont bf = BaseFont.CreateFont("c:/windows/fonts/Tahoma.ttf", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
 
    protected void Page_Load(object sender, EventArgs e)
    {
        HttpContext context = HttpContext.Current;
        HttpResponse response = context.Response;
        HttpSessionState Session = HttpContext.Current.Session;
     
     
        string destinationfilename = string.Empty;
            try
            {
                destinationfilename = HttpContext.Current.Request.ServerVariables["APPL_PHYSICAL_PATH"] + "documents\\" + Guid.NewGuid().ToString() + ".pdf";
         

                    printitem(destinationfilename);
                 //   StampPageNo(destinationfilename);
                    byte[] bytearray = File.ReadAllBytes(destinationfilename);

                    response.ClearHeaders();
                    Response.Buffer = true;
                    response.AppendHeader("Content-disposition", string.Format("attachment;filename={0}", "studentmarks.pdf"));
                    Response.ContentType = "application/pdf";

                    response.OutputStream.Write(bytearray, 0, bytearray.Length);

                   // File.Delete(destinationfilename);

                    Response.Flush();
                    Response.Close();
            }
            catch (Exception ex)
            {
             
            }
            finally
            {
             
            }
    }
 
    public void printitem(string destinationfilename)
    {
        Document mydoc = new Document(PageSize.A4);
        FileStream indexfile = null;
        PdfWriter wr1 = null;

        indexfile = new FileStream(destinationfilename, FileMode.Create, FileAccess.ReadWrite, FileShare.ReadWrite);
        wr1 = PdfWriter.GetInstance(mydoc, indexfile);

        string currentdate = DateTime.Now.ToString("dd/MM/yyyy");
        Phrase footPower = new Phrase("Report Run Date : " + HttpUtility.HtmlDecode(currentdate) + new String(' ', 125), FontFactory.GetFont("Tahoma", 7));
        HeaderFooter footerPower = new HeaderFooter(footPower, false);
        footerPower.Border = iTextSharp.text.Rectangle.TOP_BORDER;
        footerPower.Alignment = Element.ALIGN_CENTER;

        mydoc.Footer = footerPower;

        mydoc.Open();

     
     


        // Add Header
     

        DataTable dt;
        DataSet ds=new DataSet();
        SqlConnection SCCON = new SqlConnection(HttpContext.Current.Session["constr"].ToString());
        SCCON.Open();
        //  string staffcode = Guid.NewGuid().ToString();
        SqlCommand SC = new SqlCommand("SELECT FLDMARK1,FLDMARK2,FLDMARK3,FLDMARK4,FLDMARK5 FROM SM.TBLERMARKDETAILS where FLDSTUDENTID='" + Session["SID"].ToString() + "' AND FLDSEMESTER=" + int.Parse(Request.Form["cmbsemester"]), SCCON);
        SqlDataAdapter da = new SqlDataAdapter(SC);
        da.Fill(ds);
        dt = ds.Tables[0];
        SCCON.Close();

        PdfPTable pltable = new PdfPTable(2);
        float[] widths1 = { 10,10 };
        pltable.SetWidths(widths1);
        //table.SetTotalWidth(widths1);

        pltable.AddCell(Generatecell(1, "Name : ", 9, true, 1,false,0,true));

        pltable.AddCell(Generatecell(1,Session["SNAME"].ToString(), 9, true, 1));

        pltable.AddCell(Generatecell(1, "Semester : ", 9, true, 1,false,0,true));

        pltable.AddCell(Generatecell(1, Request.Form["cmbsemester"], 9, true, 1));
     
            pltable.AddCell(Generatecell(1,"Semester " + Request.Form["cmbsemester"]+" - Mark 1", 9, true, 1));
     
            pltable.AddCell(Generatecell(1, dt.Rows[0]["FLDMARK1"].ToString(), 9, true, 1));
            pltable.AddCell(Generatecell(1, "Semester " + Request.Form["cmbsemester"] + " - Mark 2", 9, true, 1));
            pltable.AddCell(Generatecell(1, dt.Rows[0]["FLDMARK2"].ToString(), 9, true, 1));
            pltable.AddCell(Generatecell(1, "Semester " + Request.Form["cmbsemester"] + " - Mark 3", 9, true, 1));
               pltable.AddCell(Generatecell(1, dt.Rows[0]["FLDMARK3"].ToString(), 9, true, 1));
               pltable.AddCell(Generatecell(1, "Semester " + Request.Form["cmbsemester"] + " - Mark 4", 9, true, 1));
        pltable.AddCell(Generatecell(1, dt.Rows[0]["FLDMARK4"].ToString(), 9, true, 1));
        pltable.AddCell(Generatecell(1, "Semester " + Request.Form["cmbsemester"] + " - Mark 5", 9, true, 1));
            pltable.AddCell(Generatecell(1, dt.Rows[0]["FLDMARK5"].ToString(), 9, true, 1));
     
     
           
         
            mydoc.Add(pltable);

//Attachment Start Here
       
     
    mydoc.Close();
    wr1.Close();
    indexfile.Close();
    }

    public PdfPCell Generatecell(int align, string text, int fontsize, bool bold, int border)
    {
        PdfPCell returncell = new PdfPCell();
        returncell.AddElement(Generateparagraph(align, text, fontsize, bold));
        if (border == 0)
        {
            returncell.Border = 0;
        }

        return returncell;
    }


    public PdfPCell Generatecell(int align, string text, int fontsize, bool bold, int border, bool colspan, int noofcols, bool header)
    {
        PdfPCell returncell = new PdfPCell();
        returncell.AddElement(Generateparagraph(align, text, fontsize, bold));
        if (border == 0)
        {
            returncell.Border = 0;

        }
        if (colspan == true)
        {
            returncell.Colspan = noofcols;

        }
        if (header == true)
        {
            returncell.BackgroundColor = Color.LIGHT_GRAY;
        }
        return returncell;
    }

    public Paragraph Generateparagraph(int align, string text, int fontsize, bool bold)
    {
        Paragraph para = new Paragraph();



        //  BaseFont bf = BaseFont.CreateFont(BaseFont.TIMES_ROMAN,BaseFont.CP1252,BaseFont.NOT_EMBEDDED);

        if (bold == false)
            para = new Paragraph(string.IsNullOrEmpty(text) ? " " : StripTagsRegex(HttpUtility.HtmlDecode(text)), new Font(bf, fontsize));
        else if (bold == true)
            para = new Paragraph(string.IsNullOrEmpty(text) ? " " : StripTagsRegex(HttpUtility.HtmlDecode(text)), new Font(bf, fontsize, Font.BOLD));
        if (align == 1)
        {
            para.Alignment = Element.ALIGN_LEFT;
        }
        else if (align == 2)
        {
            para.Alignment = Element.ALIGN_CENTER;
        }
        else if (align == 3)
        {
            para.Alignment = Element.ALIGN_RIGHT;
        }

        return para;
    }

    public Paragraph AddEmptyParagraph()
    {
        Paragraph para = new Paragraph(" ");
        return para;
    }

    public PdfPTable Generatetable(int columns, float[] widths)
    {
        PdfPTable returntable = new PdfPTable(columns);
        returntable.WidthPercentage = 100f;
        returntable.SetTotalWidth(widths);
        return returntable;
    }
    private static string StripTagsRegex(string source)
    {
        return Regex.Replace(source, "<.*?>", string.Empty);
    }


No comments:

Post a Comment