如何使用ITextsharp自定义我自己的P.D.F表单?

时间:2014-03-10 09:25:51

标签: c# asp.net-mvc pdf-generation

enter image description here

这里我附上了使用此代码生成P.D.F文档的代码我可以获得P.D.F结构但我想在这里设计表格(工资单)...

  public void Add(int employeenumber, string fromdate, string todate)
    {
        string pdfFilePath = null;
        Document doc = new Document(iTextSharp.text.PageSize.LETTER, 10, 10, 42, 35);
        try
        {
            pdfFilePath = Server.MapPath("~/Uploads/BANKformedited.pdf");

            //string pdfFilePath = Server.MapPath(".") + "/pdf/myPdf.pdf";

            //Create Document class object and set its size to letter and give space left, right, Top, Bottom Margin
            PdfWriter wri = PdfWriter.GetInstance(doc, new FileStream(pdfFilePath, FileMode.Create));

            doc.Open();//Open Document to write

            Font font8 = FontFactory.GetFont("ARIAL", 7);

            //Write some content
            Paragraph paragraph = new Paragraph("Monday Ventures Private Ltd...");


            //this is for the Image Placing to the P.D.F Screen

            //<img src="../Images/logo%20175%20x%2040.png">
            //string clientLogo = Server.MapPath("../Images/logo%20175%20x%2040.png");
            //string imageFilePath = Server.MapPath("../Images/logo%20175%20x%2040.png");
            //iTextSharp.text.Image jpg = iTextSharp.text.Image.GetInstance(imageFilePath);
            ////Resize image depend upon your need  
            //jpg.ScaleToFit(80f, 60f);
            ////Give space before image  
            //jpg.SpacingBefore = 0f;
            ////Give some space after the image  
            //jpg.SpacingAfter = 1f;
            //jpg.Alignment = Element.HEADER;
            //doc.Add(jpg);

            //DataTable dt = GetDataTable(employeenumber, fromdate, todate);
            DataTable dt = new DataTable();
            dt = GetDataTable(employeenumber, fromdate, todate);

            if (dt != null)
            {
                //Craete instance of the pdf table and set the number of column in that table
                PdfPTable PdfTable = new PdfPTable(dt.Columns.Count);
                PdfPCell PdfPCell = null;


                //Add Header of the pdf table

                PdfPCell = new PdfPCell(new Phrase(new Chunk("Monday Ventures Private Ltd", font8)));
                PdfTable.AddCell(PdfPCell);

                PdfPCell = new PdfPCell(new Phrase(new Chunk("ID", font8)));
                PdfTable.AddCell(PdfPCell);

                PdfPCell = new PdfPCell(new Phrase(new Chunk("Dated", font8)));
                PdfTable.AddCell(PdfPCell);

                PdfPCell = new PdfPCell(new Phrase(new Chunk("EmployeeNo", font8)));
                PdfTable.AddCell(PdfPCell);

                PdfPCell = new PdfPCell(new Phrase(new Chunk("Head", font8)));
                PdfTable.AddCell(PdfPCell);


                PdfPCell = new PdfPCell(new Phrase(new Chunk("Sequence", font8)));
                PdfTable.AddCell(PdfPCell);

                PdfPCell = new PdfPCell(new Phrase(new Chunk("Added", font8)));
                PdfTable.AddCell(PdfPCell);

                PdfPCell = new PdfPCell(new Phrase(new Chunk("Substracted", font8)));
                PdfTable.AddCell(PdfPCell);

                //Dated  EmployeeNo Head  Sequence Added Substracted
                //How add the data from datatable to pdf table
                for (int rows = 0; rows < dt.Rows.Count; rows++)
                {
                    for (int column = 0; column < dt.Columns.Count; column++)
                    {
                        PdfPCell = new PdfPCell(new Phrase(new Chunk(dt.Rows[rows][column].ToString(), font8)));
                        PdfTable.AddCell(PdfPCell);
                    }
                }

                PdfTable.SpacingBefore = 15f; // Give some space after the text or it may overlap the table

                doc.Add(paragraph);
                doc.Add(PdfTable);

            }


        }
        catch (DocumentException docEx)
        {

        }
        catch (IOException ioEx)
        {

        }
        catch (Exception ex)
        {

        }
        finally
        {
            //doc.Close();
            //Response.Write(pdfFilePath);
            //Response.ClearContent();
            doc.Close();
            Response.ClearHeaders();
            Response.ContentType = "application/pdf";
            Response.AddHeader("Content-Disposition", "attachment; filename=" + pdfFilePath + "");
            Response.WriteFile(pdfFilePath);
            Response.End();
        }
        //return File(pdfFilePath, "application/pdf", string.Format("Report{0}.pdf", 0));

    }

实际上我得到了P.D.F表格,但在这里我想使用itextsharper从数据库数据中自定义我自己的表单..

提前致谢...

0 个答案:

没有答案