为什么边界不会出现在itextsharp创建的pdf页面上?

时间:2015-10-27 07:40:00

标签: c# pdf itextsharp

这是我用来创建pdf文件的代码。这很好用,pdf文件在多个页面中。

我确实在pdf文件中有表格,表格从第1页开始并转到最后一页(即第3页)。因此页面边框在第2页关闭。

如果我在第2页上有表格边框会很好。任何人都可以帮助我如何在所有页面上都有边框?

非常感谢。

 public void Masterpdffile(DataTable dtpdfvalues, InvoiceSchema     _InvoiceSchema, Companyschema _Companyschema)
    {
        string Filename = string.Empty;
        string FilePath = string.Empty;
       try
        {



         MemoryStream PDFData = new MemoryStream();
            Document doc = new Document(PageSize.A4, 25, 25, 40, 40);
            string ApplicationPath = Application.StartupPath + "\\" + "Logo.png";
            PdfWriter writer = PdfWriter.GetInstance(doc, PDFData);


            var titleFont = FontFactory.GetFont("Times New Roman", 16, iTextSharp.text.Font.BOLD);
            var titleFont1 = FontFactory.GetFont("Times New Roman", 10, iTextSharp.text.Font.NORMAL);
            var boldTableFont = FontFactory.GetFont("Times New Roman", 10, iTextSharp.text.Font.NORMAL);
            var boldTableFont1 = FontFactory.GetFont("Times New Roman", 10, iTextSharp.text.Font.NORMAL);
            var boldTableFont2 = FontFactory.GetFont("Times New Roman", 10, iTextSharp.text.Font.NORMAL);
            var Total = FontFactory.GetFont("Times New Roman", 10, iTextSharp.text.Font.BOLD);
            var boldTableFont3 = FontFactory.GetFont("Times New Roman", 10, iTextSharp.text.Font.NORMAL);
            var bodyFont = FontFactory.GetFont("Times New Roman", 10, iTextSharp.text.Font.NORMAL);
            var bodyFont1 = FontFactory.GetFont("Times New Roman", 10, iTextSharp.text.Font.NORMAL);
            var italicFont = FontFactory.GetFont("Times New Roman", 12, iTextSharp.text.Font.NORMAL);
           var Font =FontFactory.GetFont("Times New Roman", 12,BaseColor.WHITE);

               var border =(PdfPCell.BOTTOM_BORDER);

            iTextSharp.text.Rectangle pageSize = writer.PageSize;

            Filename = _InvoiceSchema.InvoiceNumber+"-"+"Masterpackingslip"+"-" + DateTime.Now.Year + DateTime.Now.Month + DateTime.Now.Day + DateTime.Now.Hour + DateTime.Now.Minute + DateTime.Now.Second + ".pdf";
            FilePath = Constant.Folderpath + "\\" + Filename;
            PdfWriter.GetInstance(doc, new System.IO.FileStream(FilePath, FileMode.Create));

            doc.Open();

            #region Image and Title
            float[] phWidth = new float[] { 50f, 30f, 20f };
            PdfPTable Pheader = new PdfPTable(3);
            Pheader.WidthPercentage = 100f;
            Pheader.DefaultCell.Border = iTextSharp.text.Rectangle.NO_BORDER;


            string Companyaddress = string.Empty;

            if (!string.IsNullOrEmpty(_Companyschema.Qbcompanyaddress.Address1))
                Companyaddress += _Companyschema.Qbcompanyaddress.Address1 + Environment.NewLine;
            if (!string.IsNullOrEmpty(_Companyschema.Qbcompanyaddress.Address2))
                Companyaddress += _Companyschema.Qbcompanyaddress.Address2 + Environment.NewLine;
            if (!string.IsNullOrEmpty(_Companyschema.Qbcompanyaddress.Address3))
                Companyaddress += _Companyschema.Qbcompanyaddress.Address3 + Environment.NewLine;
            if (!string.IsNullOrEmpty(_Companyschema.Qbcompanyaddress.Address4))
                Companyaddress += _Companyschema.Qbcompanyaddress.Address4 + Environment.NewLine;
            if (!string.IsNullOrEmpty(_Companyschema.Qbcompanyaddress.Address5))
                Companyaddress += _Companyschema.Qbcompanyaddress.Address5 + Environment.NewLine;
            if (!string.IsNullOrEmpty(_Companyschema.Qbcompanyaddress.City))
                Companyaddress += _Companyschema.Qbcompanyaddress.City + ",";
            if (!string.IsNullOrEmpty(_Companyschema.Qbcompanyaddress.State))
                Companyaddress += _Companyschema.Qbcompanyaddress.State + " ";

            if (!string.IsNullOrEmpty(_Companyschema.Qbcompanyaddress.PostalCode))
                Companyaddress += _Companyschema.Qbcompanyaddress.PostalCode + Environment.NewLine;
            if (!string.IsNullOrEmpty(_Companyschema.Qbcompanyaddress.Country))
                Companyaddress += _Companyschema.Qbcompanyaddress.Country + Environment.NewLine;




            PdfPCell pcellsContect = new PdfPCell(new Phrase(_Companyschema.CompanyName+ Environment.NewLine + Environment.NewLine + Companyaddress, bodyFont1));


            pcellsContect.PaddingLeft = 50;
            pcellsContect.VerticalAlignment = Element.ALIGN_MIDDLE;
            pcellsContect.Border = iTextSharp.text.Rectangle.NO_BORDER;
            pcellsContect.UseAscender = true;


            Pheader.AddCell(pcellsContect);






            PdfPCell pspac = new PdfPCell();
            pspac.Border = iTextSharp.text.Rectangle.NO_BORDER;
            pspac.VerticalAlignment = 1;

            PdfPCell pInvoicez = new PdfPCell();
            pInvoicez.Border = iTextSharp.text.Rectangle.NO_BORDER;
            pInvoicez.VerticalAlignment = 1;

            PdfPCell pcellsforspace = new PdfPCell(new Phrase("     Master Packing Slip", titleFont));
            pcellsforspace.HorizontalAlignment = Element.ALIGN_LEFT;
            pcellsforspace.VerticalAlignment = Element.ALIGN_BOTTOM;
            pcellsforspace.Border = iTextSharp.text.Rectangle.NO_BORDER;

            pcellsforspace.UseDescender = true;

            Paragraph p1 = new Paragraph("Packing Slip", titleFont);

            p1.Alignment = Element.ALIGN_RIGHT;
            p1.Font = boldTableFont;
            pInvoicez.AddElement(p1);




            Pheader.AddCell(pspac);

            Pheader.AddCell(pcellsforspace);

            doc.Add(Pheader);
            #endregion











            #region Top
            float[] outwodth = new float[] { 50f, 20f, 30f };

            PdfPTable Outer = new PdfPTable(3);
            Outer.WidthPercentage = 100;
            Outer.DefaultCell.Border = iTextSharp.text.Rectangle.NO_BORDER;
            Outer.SetWidths(outwodth);

            float[] wid = new float[] { 50f, 50f };


            PdfPTable pdf = new PdfPTable(2);
            pdf.SetWidths(wid);
            pdf.DefaultCell.Border = iTextSharp.text.Rectangle.NO_BORDER;
            pdf.HorizontalAlignment = Element.ALIGN_LEFT;
            pdf.WidthPercentage = 90.0f;
            PdfPCell pcel = new PdfPCell();
            float[] widtSp = new float[] { 10f };
            PdfPTable pdfSpace = new PdfPTable(1);
            pdfSpace.DefaultCell.Border = iTextSharp.text.Rectangle.NO_BORDER;
            pdfSpace.HorizontalAlignment = Element.ALIGN_RIGHT;
            pdfSpace.WidthPercentage = 5.0f;
            pdfSpace.SetWidths(widtSp);


            float[] widt = new float[] { 10f, 10f };
            PdfPTable ptable = new PdfPTable(2);
            ptable.HorizontalAlignment = Element.ALIGN_RIGHT;
            ptable.WidthPercentage = 5.0f;
            ptable.SetWidths(widt);
           PdfPCell pcellsDate = new PdfPCell(new Phrase("Date", boldTableFont1));
            pcellsDate.HorizontalAlignment = Element.ALIGN_CENTER;
            pcellsDate.VerticalAlignment = Element.ALIGN_MIDDLE;

            pcellsDate.UseAscender = true;
            pcellsDate.FixedHeight = 20f;
            ptable.AddCell(pcellsDate);
            PdfPCell pcellsInvoice = new PdfPCell(new Phrase("Invoice #", boldTableFont1));
            pcellsInvoice.HorizontalAlignment = Element.ALIGN_CENTER;
            pcellsInvoice.VerticalAlignment = Element.ALIGN_MIDDLE;

            pcellsInvoice.UseAscender = true;
            pcellsInvoice.FixedHeight = 20f;


            ptable.AddCell(pcellsInvoice);

            PdfPCell pcellsDateValue = new PdfPCell(new Phrase(_InvoiceSchema.InvoiceDate.ToShortDateString(), bodyFont));
            pcellsDateValue.HorizontalAlignment = Element.ALIGN_CENTER;
            pcellsDateValue.VerticalAlignment = Element.ALIGN_MIDDLE;
            pcellsDateValue.PaddingLeft=10;
            pcellsDateValue.UseAscender = true;
            pcellsDateValue.FixedHeight = 20f;
            ptable.AddCell(pcellsDateValue);






            PdfPCell pcellsInvValue = new PdfPCell(new Phrase(_InvoiceSchema.InvoiceNumber, bodyFont));
            pcellsInvValue.HorizontalAlignment = Element.ALIGN_CENTER;
            pcellsInvValue.VerticalAlignment = Element.ALIGN_MIDDLE;

            pcellsInvValue.UseAscender = true;
            pcellsInvValue.FixedHeight = 20f;
            ptable.AddCell(pcellsInvValue);




            Outer.AddCell(pdf);
            Outer.AddCell(pdfSpace);
            Outer.AddCell(ptable);
            Outer.SpacingAfter = 10f;
            doc.Add(Outer);
            #endregion

            #region Bill Mid table
            float[] widthBP = new float[] { 40f, 20f, 40f };
            PdfPTable tableBP = new PdfPTable(3);

            tableBP.WidthPercentage = 100;
            tableBP.SetWidths(widthBP);

            tableBP.DefaultCell.Border = iTextSharp.text.Rectangle.NO_BORDER;


            float[] widp = new float[] { 20f };
            PdfPTable pdfpSp = new PdfPTable(1);
            pdfpSp.DefaultCell.Border = iTextSharp.text.Rectangle.NO_BORDER;
            pdfpSp.HorizontalAlignment = Element.ALIGN_RIGHT;
            pdfpSp.WidthPercentage = 5.0f;
            pdfpSp.SetWidths(widp);


            string billaddress = string.Empty;
            if (!string.IsNullOrEmpty(_InvoiceSchema.BillAddress.Address1))
                billaddress += _InvoiceSchema.BillAddress.Address1 + Environment.NewLine;
            if (!string.IsNullOrEmpty(_InvoiceSchema.BillAddress.Address2))
                billaddress += _InvoiceSchema.BillAddress.Address2 + Environment.NewLine;
            if (!string.IsNullOrEmpty(_InvoiceSchema.BillAddress.Address3))
                billaddress += _InvoiceSchema.BillAddress.Address3 + Environment.NewLine;
            if (!string.IsNullOrEmpty(_InvoiceSchema.BillAddress.Address4))
                billaddress += _InvoiceSchema.BillAddress.Address4 + Environment.NewLine;
            if (!string.IsNullOrEmpty(_InvoiceSchema.BillAddress.Address5))
                billaddress += _InvoiceSchema.BillAddress.Address5 + Environment.NewLine;
            if (!string.IsNullOrEmpty(_InvoiceSchema.BillAddress.City))
                billaddress += _InvoiceSchema.BillAddress.City + ",";
            if (!string.IsNullOrEmpty(_InvoiceSchema.BillAddress.State))
                billaddress += _InvoiceSchema.BillAddress.State + " ";

            if (!string.IsNullOrEmpty(_InvoiceSchema.BillAddress.PostalCode))
                billaddress += _InvoiceSchema.BillAddress.PostalCode + Environment.NewLine;
            if (!string.IsNullOrEmpty(_InvoiceSchema.BillAddress.Country))
                billaddress += _InvoiceSchema.BillAddress.Country + Environment.NewLine;

            float[] width = new float[] { 5f };
            PdfPTable table = new PdfPTable(1);
            table.HorizontalAlignment = Element.ALIGN_LEFT;
            table.WidthPercentage = 5.0f;
            table.SetWidths(width);

            PdfPCell cell = new PdfPCell();
            cell.FixedHeight = 15f;

            PdfPCell pcellsBillTO = new PdfPCell(new Phrase("   Bill To", boldTableFont1));
            pcellsBillTO.Colspan = 2;
            pcellsBillTO.HorizontalAlignment = Element.ALIGN_LEFT;

            pcellsBillTO.VerticalAlignment = Element.ALIGN_MIDDLE;

            pcellsBillTO.UseAscender = true;
            pcellsBillTO.FixedHeight = 22f;
            table.AddCell(pcellsBillTO);


            PdfPCell pcellsBillTOvalue = new PdfPCell(new Phrase(billaddress, bodyFont));
            pcellsBillTOvalue.HorizontalAlignment = Element.ALIGN_LEFT;
            pcellsBillTOvalue.VerticalAlignment = Element.ALIGN_TOP;

            pcellsBillTOvalue.UseAscender = true;
            pcellsBillTOvalue.FixedHeight = 50f;
            //madhu
            table.AddCell(pcellsBillTOvalue);

            float[] customerwidth = new float[] { 10f, 10f };
            PdfPTable ptablecustomer = new PdfPTable(2);
            ptablecustomer.HorizontalAlignment = Element.ALIGN_LEFT;
            ptablecustomer.DefaultCell.Border = iTextSharp.text.Rectangle.BOX;
            ptablecustomer.WidthPercentage = 15f;
            ptablecustomer.SetWidths(customerwidth);





            PdfPCell pcellscustomervalue = new PdfPCell(new Phrase("Customer S.O", bodyFont));
            pcellscustomervalue.HorizontalAlignment = Element.ALIGN_CENTER;
            pcellscustomervalue.VerticalAlignment = Element.ALIGN_MIDDLE;

            pcellscustomervalue.UseAscender = true;
            pcellscustomervalue.FixedHeight = 20f;

            ptablecustomer.AddCell(pcellscustomervalue);


            PdfPCell pcellsship = new PdfPCell(new Phrase("Ship", bodyFont));
            pcellsship.HorizontalAlignment = Element.ALIGN_CENTER;
            pcellsship.VerticalAlignment = Element.ALIGN_MIDDLE;


            pcellsship.UseAscender = true;
            pcellsship.FixedHeight = 20f;
            ptablecustomer.AddCell(pcellsship);

            PdfPCell pcellcustomername = new PdfPCell(new Phrase(_InvoiceSchema.PONumber, bodyFont));
            pcellcustomername.HorizontalAlignment = Element.ALIGN_CENTER;
            pcellcustomername.VerticalAlignment = Element.ALIGN_MIDDLE;

            pcellcustomername.UseAscender = true;
            pcellcustomername.FixedHeight = 20f;
            ptablecustomer.AddCell(pcellcustomername);

            PdfPCell pcellsonumber = new PdfPCell(new Phrase(_InvoiceSchema.InvoiceDate.ToShortDateString(), bodyFont));
            pcellsonumber.HorizontalAlignment = Element.ALIGN_CENTER;
            pcellsonumber.VerticalAlignment = Element.ALIGN_MIDDLE;


            pcellsonumber.UseAscender = true;
            pcellsonumber.FixedHeight = 20f;
            ptablecustomer.AddCell(pcellsonumber);








            tableBP.AddCell(table);



            tableBP.AddCell(pdfpSp);



            string ShipAddress = string.Empty;
            if (!string.IsNullOrEmpty(_InvoiceSchema.ShipAddress.Address1))
                ShipAddress += _InvoiceSchema.ShipAddress.Address1 + Environment.NewLine;
            if (!string.IsNullOrEmpty(_InvoiceSchema.ShipAddress.Address2))
                ShipAddress += _InvoiceSchema.ShipAddress.Address2 + Environment.NewLine;
            if (!string.IsNullOrEmpty(_InvoiceSchema.ShipAddress.Address3))
                ShipAddress += _InvoiceSchema.ShipAddress.Address3 + Environment.NewLine;
            if (!string.IsNullOrEmpty(_InvoiceSchema.ShipAddress.Address4))
                ShipAddress += _InvoiceSchema.ShipAddress.Address4 + Environment.NewLine;
            if (!string.IsNullOrEmpty(_InvoiceSchema.ShipAddress.Address5))
                ShipAddress += _InvoiceSchema.ShipAddress.Address5 + Environment.NewLine;
            if (!string.IsNullOrEmpty(_InvoiceSchema.ShipAddress.City))
                ShipAddress += _InvoiceSchema.ShipAddress.City + ",";
            if (!string.IsNullOrEmpty(_InvoiceSchema.ShipAddress.State))
                ShipAddress += _InvoiceSchema.ShipAddress.State + " ";

            if (!string.IsNullOrEmpty(_InvoiceSchema.ShipAddress.PostalCode))
                ShipAddress += _InvoiceSchema.ShipAddress.PostalCode + Environment.NewLine;
            if (!string.IsNullOrEmpty(_InvoiceSchema.ShipAddress.Country))
                ShipAddress += _InvoiceSchema.ShipAddress.Country + Environment.NewLine;

            float[] widthP = new float[] { 5f };
            PdfPTable tableP = new PdfPTable(1);
            tableP.WidthPercentage = 30f;
            tableP.HorizontalAlignment = Element.ALIGN_RIGHT;
            tableP.SetWidths(widthP);


            PdfPCell pcellsProperty = new PdfPCell(new Phrase("   Ship To", boldTableFont1));
            pcellsProperty.HorizontalAlignment = Element.ALIGN_LEFT;
            pcellsProperty.VerticalAlignment = Element.ALIGN_MIDDLE;

            pcellsProperty.UseAscender = true;
            pcellsProperty.FixedHeight = 22f;
            tableP.AddCell(pcellsProperty);


            PdfPCell pcellsPropertyvalue = new PdfPCell(new Phrase(ShipAddress, bodyFont));
            pcellsPropertyvalue.HorizontalAlignment = Element.ALIGN_LEFT;
            pcellsPropertyvalue.VerticalAlignment = Element.ALIGN_TOP;

            pcellsPropertyvalue.UseAscender = true;
            pcellsPropertyvalue.FixedHeight = 50f;
            tableP.AddCell(pcellsPropertyvalue);
            tableBP.AddCell(tableP);




            float[] widpf = new float[] { 20f };
            PdfPTable pdgpSp = new PdfPTable(1);
            pdgpSp.DefaultCell.Border = iTextSharp.text.Rectangle.NO_BORDER;
            pdgpSp.HorizontalAlignment = Element.ALIGN_RIGHT;
            pdgpSp.WidthPercentage = 5.0f;
            pdgpSp.SetWidths(widpf);

            float[] wigdp = new float[] { 20f };
            PdfPTable pdfpghSp = new PdfPTable(1);
            pdfpghSp.DefaultCell.Border = iTextSharp.text.Rectangle.NO_BORDER;
            pdfpghSp.HorizontalAlignment = Element.ALIGN_RIGHT;
            pdfpghSp.WidthPercentage = 5.0f;
            pdfpghSp.SetWidths(wigdp);


            tableBP.AddCell(ptablecustomer);

            tableBP.AddCell(pdgpSp);
            tableBP.AddCell(pdfpghSp);
            tableBP.SpacingAfter = 12f;



            doc.Add(tableBP);
            #endregion

            float[] widthM = new float[] { 2f, 2.3F, 8.3f };
            PdfPTable tableM = new PdfPTable(3);
            tableM.SetWidths(widthM);
            //tableM.DefaultCell.FixedHeight = 20f;
            tableM.DefaultCell.Border = 0;

            tableM.WidthPercentage = 100.0f;
            //tableM.h

            PdfPCell clHeader = new PdfPCell();
            clHeader.FixedHeight = 20f;

            PdfPCell pcellsitemname = new PdfPCell(new Phrase("Qty", boldTableFont1));
            pcellsitemname.HorizontalAlignment = Element.ALIGN_CENTER;
            pcellsitemname.VerticalAlignment = Element.ALIGN_RIGHT;

            pcellsitemname.UseAscender = true;
            pcellsitemname.FixedHeight = 20f;
            tableM.AddCell(pcellsitemname);


            PdfPCell pcellsDescription = new PdfPCell(new Phrase("Item Code", boldTableFont1));
            pcellsDescription.HorizontalAlignment = Element.ALIGN_CENTER;
            pcellsDescription.VerticalAlignment = Element.ALIGN_MIDDLE;
            // pcellsProperty.Border = iTextSharp.text.Rectangle.NO_BORDER;
            pcellsDescription.UseAscender = true;
            pcellsDescription.FixedHeight = 20f;
            tableM.AddCell(pcellsDescription);

            PdfPCell pcellsQty = new PdfPCell(new Phrase("Description", boldTableFont1));
            pcellsQty.HorizontalAlignment = Element.ALIGN_CENTER;
            pcellsQty.VerticalAlignment = Element.ALIGN_MIDDLE;

            pcellsQty.UseAscender = true;
            pcellsQty.FixedHeight = 20f;
            tableM.AddCell(pcellsQty);



            PdfPCell amtCell = new PdfPCell();

            int i = 0;
            foreach (DataRow dr in dtpdfvalues.Rows)
            {
                if (!string.IsNullOrEmpty(Convert.ToString(dr["Parent"])))
                {
                    i++;
                }
            }

            int k = 0;
            foreach (DataRow dr in dtpdfvalues.Rows)
            {
                int count = Convert.ToInt16(dtpdfvalues.Rows.Count);

                if (!string.IsNullOrEmpty(Convert.ToString(dr["Parent"])))
                {
                    k++;
                    PdfPCell descCell = new PdfPCell();

                    PdfPCell qtyCell = new PdfPCell();
                    PdfPCell rateCell = new PdfPCell();



                    descCell.FixedHeight = 78;
                    qtyCell.FixedHeight = 78;
                    rateCell.FixedHeight = 78;
                    amtCell.FixedHeight = 78;




                    if (i != k)
                    {
                        descCell.MinimumHeight = 335 / i;
                        qtyCell.MinimumHeight = 335 / i;
                        rateCell.MinimumHeight = 335 / i;
                        amtCell.MinimumHeight = 335 / i;

                        descCell.FixedHeight = 78;
                        qtyCell.FixedHeight = 78;
                        rateCell.FixedHeight = 78;
                        amtCell.FixedHeight = 78;



                    }
                    else
                    {

                        descCell.MinimumHeight = 335;
                        qtyCell.MinimumHeight = 335;
                        rateCell.MinimumHeight = 335;
                        amtCell.MinimumHeight = 335;

                        descCell.FixedHeight = 78;
                        qtyCell.FixedHeight = 78;
                        rateCell.FixedHeight = 78;
                        amtCell.FixedHeight = 78;


                    }

                    rateCell.Border = 0;
                    amtCell.Border = 0;
                    rateCell.Border = 0;
                    descCell.Border = 0;
                    qtyCell.Border = 0;









                    rateCell.BorderWidthLeft = 0.1f;
                    amtCell.BorderWidthLeft = 0.1f;
                    descCell.BorderWidthLeft = 0.1f;
                    qtyCell.BorderWidthLeft = 0.1f;
                    qtyCell.BorderWidthRight = 0.1f;

0 个答案:

没有答案