如何定制我自己的P.D.使用ITextsharper F形式?

时间:2022-10-11 21:07:43

如何定制我自己的P.D.使用ITextsharper F形式?

Here i had enclosed the code to generate the P.D.F document using this code i can get the P.D.F stucture but i want to design the form here like(salary slip)...

这里我附上了生成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));

    }

Actually i got the P.D.F form but here i want to customize my own form from the database datas using itextsharper..

实际上我拿到了医生证明但是在这里,我想使用itextsharper从数据库数据定制我自己的表单。

Thanks in Advance...

提前谢谢…

1 个解决方案

#1


1  

I solved this issue with use of Html template.I designed the template and am binding the template with the database datas..

我使用Html模板解决了这个问题。我设计了模板,并将模板与数据库数据绑定在一起。

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("~/yourpath/");

            //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));
            //Open Document to write
            doc.Open();

            Font font8 = FontFactory.GetFont("ARIAL", 7);
            var titleFont = FontFactory.GetFont("Arial", 18, Font.BOLD);
            var subTitleFont = FontFactory.GetFont("Arial", 14, Font.BOLD);
            var boldTableFont = FontFactory.GetFont("Arial", 12, Font.BOLD);
            var endingMessageFont = FontFactory.GetFont("Arial", 10, Font.ITALIC);
            var bodyFont = FontFactory.GetFont("Arial", 12, Font.NORMAL);

            var imageFilePath = iTextSharp.text.Image.GetInstance(Server.MapPath("~/Images/8.jpg"));
            iTextSharp.text.Image jpg = iTextSharp.text.Image.GetInstance(imageFilePath);

            //Resize image depend upon your need
            jpg.ScaleToFit(40f, 56f);

            //Give space before image
            jpg.SpacingBefore = 30f;

            //Give some space after the image
            jpg.SpacingAfter = 1f;
            jpg.Alignment = Element.ALIGN_LEFT;
            doc.Add(jpg);


            //this is for get the month name and the year name frm the sql based on the passed date
            var dateresult = new SalaryDAL().Getmonthyear(fromdate);

            //getting the single property form the collection in c#//
            string contents = System.IO.File.ReadAllText(Server.MapPath("~/templates/salarypdf.htm"));

            var result = new SalaryDAL().GetSalaryReport(employeenumber, fromdate, todate);
            contents = contents.Replace("${CompanyName}", Convert.ToString(result.GetType().GetProperty("CompanyName").GetValue(result, null)));
            contents = contents.Replace("${month}", Convert.ToString(dateresult.GetType().GetProperty("Month").GetValue(dateresult, null)));
            contents = contents.Replace("${year}", Convert.ToString(dateresult.GetType().GetProperty("Year").GetValue(dateresult, null)));
            contents = contents.Replace("${EmployeeName}", Convert.ToString(result.GetType().GetProperty("EmployeeName").GetValue(result, null)));
            contents = contents.Replace("${AccNo}", Convert.ToString(result.GetType().GetProperty("BankAccNo").GetValue(result, null)));
            contents = contents.Replace("${empno}", Convert.ToString(result.GetType().GetProperty("employeeNo").GetValue(result, null)));
            contents = contents.Replace("${dptname}", Convert.ToString(result.GetType().GetProperty("Department").GetValue(result, null)));
            contents = contents.Replace("${bankname}", Convert.ToString(result.GetType().GetProperty("BankName").GetValue(result, null)));
            contents = contents.Replace("${pfno}", Convert.ToString(result.GetType().GetProperty("PFNo").GetValue(result, null)));
            contents = contents.Replace("${basic}", Convert.ToString(result.GetType().GetProperty("BasicAdd").GetValue(result, null)));
            contents = contents.Replace("${basic1}", Convert.ToString(result.GetType().GetProperty("BasicSub").GetValue(result, null)));
            contents = contents.Replace("${hra}", Convert.ToString(result.GetType().GetProperty("HRAAdd").GetValue(result, null)));
            contents = contents.Replace("${hra1}", Convert.ToString(result.GetType().GetProperty("HRASub").GetValue(result, null)));
            contents = contents.Replace("${lta}", Convert.ToString(result.GetType().GetProperty("LTAAdd").GetValue(result, null)));
            contents = contents.Replace("${lta1}", Convert.ToString(result.GetType().GetProperty("LTASub").GetValue(result, null)));
            contents = contents.Replace("${ta}", Convert.ToString(result.GetType().GetProperty("TAAdd").GetValue(result, null)));
            contents = contents.Replace("${ta1}", Convert.ToString(result.GetType().GetProperty("LTASub").GetValue(result, null)));
            contents = contents.Replace("${da}", Convert.ToString(result.GetType().GetProperty("DAAdd").GetValue(result, null)));
            contents = contents.Replace("${da1}", Convert.ToString(result.GetType().GetProperty("DASub").GetValue(result, null)));
            contents = contents.Replace("${bonus}", Convert.ToString(result.GetType().GetProperty("BonusAdd").GetValue(result, null)));
            contents = contents.Replace("${bonus1}", Convert.ToString(result.GetType().GetProperty("BonusSub").GetValue(result, null)));
            contents = contents.Replace("${pt}", Convert.ToString(result.GetType().GetProperty("PTAdd").GetValue(result, null)));
            contents = contents.Replace("${pt1}", Convert.ToString(result.GetType().GetProperty("PTSub").GetValue(result, null)));
            contents = contents.Replace("${pf}", Convert.ToString(result.GetType().GetProperty("PFAdd").GetValue(result, null)));
            contents = contents.Replace("${pf1}", Convert.ToString(result.GetType().GetProperty("PFSub").GetValue(result, null)));
            contents = contents.Replace("${other}", Convert.ToString(result.GetType().GetProperty("OtherAdd").GetValue(result, null)));
            contents = contents.Replace("${other1}", Convert.ToString(result.GetType().GetProperty("OtherSub").GetValue(result, null)));
            contents = contents.Replace("${ge}", Convert.ToString(result.GetType().GetProperty("GrossEarning").GetValue(result, null)));
            contents = contents.Replace("${gd}", Convert.ToString(result.GetType().GetProperty("GrossDeduction").GetValue(result, null)));
            contents = contents.Replace("${netslry}", Convert.ToString(result.GetType().GetProperty("NetSalary").GetValue(result, null)));


            var parsedHtmlElements = HTMLWorker.ParseToList(new StringReader(contents), null);

            foreach (var htmlElement in parsedHtmlElements)
            {
                doc.Add(htmlElement as IElement);
            }

        }
        catch (DocumentException docEx)
        {

        }
        catch (IOException ioEx)
        {

        }
        catch (Exception ex)
        {

        }
        finally
        {
            doc.Close();
            Response.ClearHeaders();
            Response.ContentType = "application/pdf";
            Response.AddHeader("Content-Disposition", "attachment; filename=" + pdfFilePath + "");
            Response.WriteFile(pdfFilePath);
            Response.End();
        }

    }

#1


1  

I solved this issue with use of Html template.I designed the template and am binding the template with the database datas..

我使用Html模板解决了这个问题。我设计了模板,并将模板与数据库数据绑定在一起。

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("~/yourpath/");

            //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));
            //Open Document to write
            doc.Open();

            Font font8 = FontFactory.GetFont("ARIAL", 7);
            var titleFont = FontFactory.GetFont("Arial", 18, Font.BOLD);
            var subTitleFont = FontFactory.GetFont("Arial", 14, Font.BOLD);
            var boldTableFont = FontFactory.GetFont("Arial", 12, Font.BOLD);
            var endingMessageFont = FontFactory.GetFont("Arial", 10, Font.ITALIC);
            var bodyFont = FontFactory.GetFont("Arial", 12, Font.NORMAL);

            var imageFilePath = iTextSharp.text.Image.GetInstance(Server.MapPath("~/Images/8.jpg"));
            iTextSharp.text.Image jpg = iTextSharp.text.Image.GetInstance(imageFilePath);

            //Resize image depend upon your need
            jpg.ScaleToFit(40f, 56f);

            //Give space before image
            jpg.SpacingBefore = 30f;

            //Give some space after the image
            jpg.SpacingAfter = 1f;
            jpg.Alignment = Element.ALIGN_LEFT;
            doc.Add(jpg);


            //this is for get the month name and the year name frm the sql based on the passed date
            var dateresult = new SalaryDAL().Getmonthyear(fromdate);

            //getting the single property form the collection in c#//
            string contents = System.IO.File.ReadAllText(Server.MapPath("~/templates/salarypdf.htm"));

            var result = new SalaryDAL().GetSalaryReport(employeenumber, fromdate, todate);
            contents = contents.Replace("${CompanyName}", Convert.ToString(result.GetType().GetProperty("CompanyName").GetValue(result, null)));
            contents = contents.Replace("${month}", Convert.ToString(dateresult.GetType().GetProperty("Month").GetValue(dateresult, null)));
            contents = contents.Replace("${year}", Convert.ToString(dateresult.GetType().GetProperty("Year").GetValue(dateresult, null)));
            contents = contents.Replace("${EmployeeName}", Convert.ToString(result.GetType().GetProperty("EmployeeName").GetValue(result, null)));
            contents = contents.Replace("${AccNo}", Convert.ToString(result.GetType().GetProperty("BankAccNo").GetValue(result, null)));
            contents = contents.Replace("${empno}", Convert.ToString(result.GetType().GetProperty("employeeNo").GetValue(result, null)));
            contents = contents.Replace("${dptname}", Convert.ToString(result.GetType().GetProperty("Department").GetValue(result, null)));
            contents = contents.Replace("${bankname}", Convert.ToString(result.GetType().GetProperty("BankName").GetValue(result, null)));
            contents = contents.Replace("${pfno}", Convert.ToString(result.GetType().GetProperty("PFNo").GetValue(result, null)));
            contents = contents.Replace("${basic}", Convert.ToString(result.GetType().GetProperty("BasicAdd").GetValue(result, null)));
            contents = contents.Replace("${basic1}", Convert.ToString(result.GetType().GetProperty("BasicSub").GetValue(result, null)));
            contents = contents.Replace("${hra}", Convert.ToString(result.GetType().GetProperty("HRAAdd").GetValue(result, null)));
            contents = contents.Replace("${hra1}", Convert.ToString(result.GetType().GetProperty("HRASub").GetValue(result, null)));
            contents = contents.Replace("${lta}", Convert.ToString(result.GetType().GetProperty("LTAAdd").GetValue(result, null)));
            contents = contents.Replace("${lta1}", Convert.ToString(result.GetType().GetProperty("LTASub").GetValue(result, null)));
            contents = contents.Replace("${ta}", Convert.ToString(result.GetType().GetProperty("TAAdd").GetValue(result, null)));
            contents = contents.Replace("${ta1}", Convert.ToString(result.GetType().GetProperty("LTASub").GetValue(result, null)));
            contents = contents.Replace("${da}", Convert.ToString(result.GetType().GetProperty("DAAdd").GetValue(result, null)));
            contents = contents.Replace("${da1}", Convert.ToString(result.GetType().GetProperty("DASub").GetValue(result, null)));
            contents = contents.Replace("${bonus}", Convert.ToString(result.GetType().GetProperty("BonusAdd").GetValue(result, null)));
            contents = contents.Replace("${bonus1}", Convert.ToString(result.GetType().GetProperty("BonusSub").GetValue(result, null)));
            contents = contents.Replace("${pt}", Convert.ToString(result.GetType().GetProperty("PTAdd").GetValue(result, null)));
            contents = contents.Replace("${pt1}", Convert.ToString(result.GetType().GetProperty("PTSub").GetValue(result, null)));
            contents = contents.Replace("${pf}", Convert.ToString(result.GetType().GetProperty("PFAdd").GetValue(result, null)));
            contents = contents.Replace("${pf1}", Convert.ToString(result.GetType().GetProperty("PFSub").GetValue(result, null)));
            contents = contents.Replace("${other}", Convert.ToString(result.GetType().GetProperty("OtherAdd").GetValue(result, null)));
            contents = contents.Replace("${other1}", Convert.ToString(result.GetType().GetProperty("OtherSub").GetValue(result, null)));
            contents = contents.Replace("${ge}", Convert.ToString(result.GetType().GetProperty("GrossEarning").GetValue(result, null)));
            contents = contents.Replace("${gd}", Convert.ToString(result.GetType().GetProperty("GrossDeduction").GetValue(result, null)));
            contents = contents.Replace("${netslry}", Convert.ToString(result.GetType().GetProperty("NetSalary").GetValue(result, null)));


            var parsedHtmlElements = HTMLWorker.ParseToList(new StringReader(contents), null);

            foreach (var htmlElement in parsedHtmlElements)
            {
                doc.Add(htmlElement as IElement);
            }

        }
        catch (DocumentException docEx)
        {

        }
        catch (IOException ioEx)
        {

        }
        catch (Exception ex)
        {

        }
        finally
        {
            doc.Close();
            Response.ClearHeaders();
            Response.ContentType = "application/pdf";
            Response.AddHeader("Content-Disposition", "attachment; filename=" + pdfFilePath + "");
            Response.WriteFile(pdfFilePath);
            Response.End();
        }

    }