Create Pdf Documents using IText#

2020-03-03 05:57发布

How can I create a pdf document with tables, which looks like this.

enter image description here

标签: c# itext
5条回答
别忘想泡老子
2楼-- · 2020-03-03 06:08

Add Namespace:

using iTextSharp.text;
using iTextSharp.text.pdf;

code using c#:

    Document doc = new Document(PageSize.A4);
    var output = new FileStream(Server.MapPath("MyFirstPDF.pdf"), FileMode.Create);
    var writer = PdfWriter.GetInstance(doc, output);


    doc.Open();


    var logo = iTextSharp.text.Image.GetInstance(Server.MapPath("~/ABsIS_Logo.jpg"));
    logo.SetAbsolutePosition(430, 770);
    logo.ScaleAbsoluteHeight(30);
    logo.ScaleAbsoluteWidth(70);
    doc.Add(logo);

    PdfPTable table1 = new PdfPTable(2);
    table1.DefaultCell.Border = 0;
    table1.WidthPercentage = 80;

    var titleFont = new Font(Font.FontFamily.UNDEFINED, 24);
    var subTitleFont = new Font(Font.FontFamily.UNDEFINED, 16);

    PdfPCell cell11 = new PdfPCell();
    cell11.Colspan = 1;
    cell11.AddElement(new Paragraph("ABC Traders Receipt", titleFont));

    cell11.AddElement(new Paragraph("Thankyou for shoping at ABC traders,your order details are below", subTitleFont));


    cell11.VerticalAlignment = Element.ALIGN_LEFT;

    PdfPCell cell12 = new PdfPCell();


    cell12.VerticalAlignment = Element.ALIGN_CENTER;


    table1.AddCell(cell11);

    table1.AddCell(cell12);


    PdfPTable table2 = new PdfPTable(3);

    //One row added

    PdfPCell cell21 = new PdfPCell();

    cell21.AddElement(new Paragraph("Photo Type"));

    PdfPCell cell22 = new PdfPCell();

    cell22.AddElement(new Paragraph("No. of Copies"));

    PdfPCell cell23 = new PdfPCell();

    cell23.AddElement(new Paragraph("Amount"));


    table2.AddCell(cell21);

    table2.AddCell(cell22);

    table2.AddCell(cell23);


    //New Row Added

    PdfPCell cell31 = new PdfPCell();

    cell31.AddElement(new Paragraph("Safe"));

    cell31.FixedHeight = 300.0f;

    PdfPCell cell32 = new PdfPCell();

    cell32.AddElement(new Paragraph("2"));

    cell32.FixedHeight = 300.0f;

    PdfPCell cell33 = new PdfPCell();

    cell33.AddElement(new Paragraph("20.00 * " + "2" + " = " + (20 * Convert.ToInt32("2")) + ".00"));

    cell33.FixedHeight = 300.0f;



    table2.AddCell(cell31);

    table2.AddCell(cell32);

    table2.AddCell(cell33);


    PdfPCell cell2A = new PdfPCell(table2);

    cell2A.Colspan = 2;

    table1.AddCell(cell2A);

    PdfPCell cell41 = new PdfPCell();

    cell41.AddElement(new Paragraph("Name : " + "ABC"));

    cell41.AddElement(new Paragraph("Advance : " + "advance"));

    cell41.VerticalAlignment = Element.ALIGN_LEFT;

    PdfPCell cell42 = new PdfPCell();

    cell42.AddElement(new Paragraph("Customer ID : " + "011"));

    cell42.AddElement(new Paragraph("Balance : " + "3993"));

    cell42.VerticalAlignment = Element.ALIGN_RIGHT;


    table1.AddCell(cell41);

    table1.AddCell(cell42);


    doc.Add(table1);

    doc.Close();
查看更多
Deceive 欺骗
3楼-- · 2020-03-03 06:11
          byte[] bPDF = null;
MemoryStream ms = new MemoryStream();
Document document = new Document(); //pdf document to write
   var originalpath = HostingEnvironment.MapPath("~/PDFs/");
                if (!System.IO.Directory.Exists(originalpath))
                    Directory.CreateDirectory(originalpath);
                // Create a new PdfWriter object, specifying the outputstream

                var pdfwriter = PdfWriter.GetInstance(document, ms);

                // Open the Document for writing
                document.Open();
                PdfPTable ParentTable = new PdfPTable(1);
                 ParentTable.TotalWidth = 500f;
                             ParentTable.LockedWidth = true;
                ParentTable.HorizontalAlignment = 0;
                ParentTable.ExtendLastRow = false;
                PdfPCell heading = new PdfPCell(new Phrase("", HeaderFont));
                heading.PaddingBottom = 0f;
                heading.PaddingTop = 0f;
                heading.Border = 1;
                ParentTable.AddCell(heading);
                PdfPTable dataTableCellHeaderTable = new PdfPTable(3);
                dataTableCellHeaderTable.HorizontalAlignment = 0;
                float[] widths = new float[] { 2f, 2f, 5f };
                dataTableCellHeaderTable.SetWidths(widths);
              PdfPCell cellSerialNumber = new PdfPCell(new Phrase(ScoringColoringModel.pdfFirstCellHeading, tableHeaderCellFont)) { Border = 0 };
                cellSerialNumber.PaddingTop = 7.5f;
                cellSerialNumber.PaddingBottom = 7.5f;
                cellSerialNumber.BorderColor = BaseColor.WHITE;
                cellSerialNumber.BackgroundColor = new BaseColor(System.Drawing.ColorTranslator.FromHtml("#D3D3D3").ToArgb());
                dataTableCellHeaderTable.AddCell(cellSerialNumber);


                PdfPCell cellRegistration = new PdfPCell(new Phrase(ScoringColoringModel.pdfSecondCellHeading, tableHeaderCellFont)) { Border = PdfPCell.LEFT_BORDER };
                cellRegistration.PaddingTop = 7.5f;
                cellRegistration.PaddingBottom = 7.5f;
                cellRegistration.BorderColor = BaseColor.WHITE;
                cellRegistration.BackgroundColor = new BaseColor(System.Drawing.ColorTranslator.FromHtml("#D3D3D3").ToArgb());
                dataTableCellHeaderTable.AddCell(cellRegistration);

                PdfPCell cellwordMark = new PdfPCell(new Phrase(ScoringColoringModel.pdfThirdCellHeading, tableHeaderCellFont)) { Border = PdfPCell.LEFT_BORDER };
                cellwordMark.PaddingTop = 7.5f;
                cellwordMark.PaddingBottom = 7.5f;
                cellwordMark.BackgroundColor = new BaseColor(System.Drawing.ColorTranslator.FromHtml("#D3D3D3").ToArgb());
                cellwordMark.BorderColor = BaseColor.WHITE;
                dataTableCellHeaderTable.AddCell(cellwordMark);
                // to append more data create one table
                PdfPTable datatable = new PdfPTable(3);

                Font cellColor = new Font();

                foreach (DataRow dr in objDataTable.Rows)
                {

                    if (dr.ItemArray[5].ToString() == "Yellow")
                    {

                        cellColor = FontFactory.GetFont(ScoringColoringModel.pdfFontSet, 12, new BaseColor(System.Drawing.ColorTranslator.FromHtml("#ffa800").ToArgb())); ;

                    }
                    else
                    {
                        cellColor = FontFactory.GetFont(ScoringColoringModel.pdfFontSet, 12, new BaseColor(System.Drawing.ColorTranslator.FromHtml(dr.ItemArray[5].ToString()).ToArgb())); ;
                    }

                    dataTableCellHeaderTable.AddCell(new PdfPCell(new Phrase(dr.ItemArray[0].ToString(), cellColor)) { PaddingBottom = 5, Border = 1, PaddingTop = 5 });



         dataTableCellHeaderTable.AddCell(new PdfPCell(new Phrase(dr.ItemArray[1].ToString(), cellColor)) { PaddingBottom = 5, Border = 1, PaddingTop = 5 });

                        dataTableCellHeaderTable.AddCell(new PdfPCell(new Phrase(dr.ItemArray[3].ToString(), cellColor)) { PaddingBottom = 5, Border = 1, PaddingTop = 5 });

                    }
    //Here you can add multiple table 





                  document.Add(ParentTable);
    //document.Add(ParentTable1); and add table one by one to the document 

                    document.Close();
                    bPDF = ms.ToArray();
                    // Close the writer instance

                    Response.ContentType = "application/pdf";
                    Response.AddHeader("content-disposition", "attachment;filename=TrademarkSearchResult.pdf");
                    Response.BinaryWrite(bPDF);
                    Response.End();
                }`enter code here`
查看更多
爱情/是我丢掉的垃圾
4楼-- · 2020-03-03 06:22

You can have look also at http://www.mikesdotnetting.com/Category/20, some handy samples of stuff people are often after it

PS: AbhiRoczz... personally I do avoid roseindia as they tend to steal plenty of resources, meaning they will copy&paste without giving credit to original owner. Plus site is badly organized and have one to many adverts

查看更多
家丑人穷心不美
5楼-- · 2020-03-03 06:22

Create PDF Letter Using Itext Sharp

    BaseFont bf = BaseFont.CreateFont("c:/windows/fonts/arial.ttf", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
    Font fontRupee = new Font(bf, 8, Font.ITALIC);
    Font fontRupee1 = new Font(bf, 10, Font.BOLDITALIC);


    var Smallspace = FontFactory.GetFont("Calibri", 1, iTextSharp.text.Color.BLACK);
    var boldHeadFont = FontFactory.GetFont("Calibri", 13, iTextSharp.text.Color.RED);
    var boldTableFont = FontFactory.GetFont("Calibri", 11, iTextSharp.text.Color.BLACK);
    var TableFontSmall = FontFactory.GetFont("Calibri", 8, iTextSharp.text.Color.BLACK);

    var TableFontmini_ARBold8Sub = FontFactory.GetFont("Arial", 11, Font.BOLD, iTextSharp.text.Color.BLACK);
    var TableFontmini_ARBoldCom = FontFactory.GetFont("Calibri", 16, Font.BOLD, iTextSharp.text.Color.BLACK);
    var TableFontmini_ARBoldComAdd = FontFactory.GetFont("Calibri", 10, Font.NORMAL, iTextSharp.text.Color.BLACK);
    var TableFontmini_ARBold82 = FontFactory.GetFont("Tahoma", 7, Font.BOLDITALIC, iTextSharp.text.Color.BLACK);
    var TableFontmini_ARBold81 = FontFactory.GetFont("Tahoma", 7, Font.BOLDITALIC, iTextSharp.text.Color.BLACK);
    var TableFontmini_Ver = FontFactory.GetFont("Arial", 7, Font.ITALIC, iTextSharp.text.Color.BLACK);
    var TableFontmini_VerBold = FontFactory.GetFont("Arial", 8, Font.BOLDITALIC, iTextSharp.text.Color.BLACK);
    var TableFontmini_ARBoldWef8 = FontFactory.GetFont("Calibri", 9, Font.BOLDITALIC, iTextSharp.text.Color.BLACK);
    var TableFontmini_ARBold8 = FontFactory.GetFont("Calibri", 8, Font.BOLDITALIC, iTextSharp.text.Color.BLACK);
    var TableFontmini_ARBold8Nor = FontFactory.GetFont("Arial", 8.5f, Font.ITALIC, iTextSharp.text.Color.BLACK);
    //var TableFontmini_ARBold8Nor = FontFactory.GetFont("Calibri", 7, Font.ITALIC, iTextSharp.text.Color.BLACK);
    var TableFontmini_ARBold8inc = FontFactory.GetFont("Calibri", 8.5f, Font.BOLDITALIC, iTextSharp.text.Color.BLACK);
    var TableFontmini_ARBoldRef = FontFactory.GetFont("Calibri", 9, Font.BOLDITALIC, iTextSharp.text.Color.BLACK);
    var boldFont = FontFactory.GetFont(FontFactory.HELVETICA_BOLDOBLIQUE, 10);
    var boldFont1 = FontFactory.GetFont(FontFactory.HELVETICA_BOLDOBLIQUE, 8, Font.UNDERLINE);
    var boldFontm = FontFactory.GetFont(FontFactory.TIMES_BOLDITALIC, 9);
    //var boldFontm = FontFactory.GetFont(FontFactory.TIMES_ROMAN, 10, iTextSharp.text.Font.BOLD | iTextSharp.text.Font.UNDERLINE);
    //var boldFontm= FontFactory.GetFont(FontFactory.TIMES_BOLD, 10, iTextSharp.text.Font.UNDERLINE);
    var TableFontmini_Ar = FontFactory.GetFont("Calibri", 8, iTextSharp.text.Color.BLACK);
    BaseFont bfTimes = BaseFont.CreateFont(BaseFont.TIMES_ROMAN, BaseFont.CP1252, false);
    iTextSharp.text.Font times = new iTextSharp.text.Font(bfTimes, 12, iTextSharp.text.Font.ITALIC, iTextSharp.text.Color.BLACK);
    iTextSharp.text.Font timessmall = new iTextSharp.text.Font(bfTimes, 9, iTextSharp.text.Font.ITALIC, iTextSharp.text.Color.BLACK);


    var normalFont = FontFactory.GetFont(FontFactory.HELVETICA, 12);
    var boldFonts = FontFactory.GetFont(FontFactory.HELVETICA_BOLD, 12);

    var blackListTextFont = FontFactory.GetFont("Arial", 28, Color.BLACK);
    var redListTextFont = FontFactory.GetFont("Arial", 28, Color.RED);






    rnPL.Id = Id.SelectedValue.Trim();
    rnPL.Code = Code;
    rnPL.CodeNo = CodeNo;

    DataSet ds = rnBL.GetDetilForPDF(rnPL);
    if (ds.Tables.Count > 0)
    {
        DataTable dt = ds.Tables["tbl_Basic"];
        iTextSharp.text.Document doc = new Document(iTextSharp.text.PageSize.A4, 0, 0, 0, 0);
       // lblHidId.Value = dt.Rows[0]["Id"].ToString();

        if (dt.Rows[0]["Id"].ToString() == "4")
        {
            FilePath = Server.MapPath("images") + "\\1.jpg";
            FilePathstamplogo = Server.MapPath("images") + "\\6.png";
        }



        if (dt.Rows[0]["Id"].ToString() == "1")
        {
            FilePath = Server.MapPath("images") + "\\2.jpg";
            FilePathslogo = Server.MapPath("images") + "\\5.png";
        }






            //iTextSharp.text.Image stamplogo = iTextSharp.text.Image.GetInstance(FilePathstamplogo);
            //stamplogo.ScalePercent(75f);

            ////stamplogo.SetAbsolutePosition(doc.PageSize.Width - 36f - 140f, doc.PageSize.Height - 36f - 640f);/*ByAbhishek*/
            //stamplogo.SetAbsolutePosition(doc.PageSize.Width - 38f - 160f, doc.PageSize.Height - 38f - 700f);
            //doc.Add(stamplogo);




        iTextSharp.text.Image jpg = iTextSharp.text.Image.GetInstance(FilePath);
        jpg.ScaleAbsoluteHeight(830);
        jpg.ScaleAbsoluteWidth(600);
        jpg.Alignment = iTextSharp.text.Image.UNDERLYING;
        fofile = "";
        fofile = Server.MapPath("PDFComRNew");
        string crefilename;
        crefilename = Convert.ToInt32(Code.ToString()).ToString() + Convert.ToInt32(CodeNo.ToString()).ToString() + ".Pdf";
        string newPathfile = System.IO.Path.Combine(fofile, crefilename);
        PdfWriter pdfwrite = PdfWriter.GetInstance(doc, new FileStream(newPathfile, FileMode.Create));

        doc.Open();
        doc.Add(jpg);

        PdfPTable tableHeader = new PdfPTable(1);
        tableHeader.WidthPercentage = 50;
        PdfPCell Headspace;

        Headspace = new PdfPCell(new Phrase(" ", TableFontSmall));
        Headspace.BorderWidth = 0;
        Headspace.HorizontalAlignment = 0;/**Left=0,Centre=1,Right=2**/
        tableHeader.AddCell(Headspace);

        Headspace = new PdfPCell(new Phrase(" ", TableFontSmall));
        Headspace.BorderWidth = 0;
        Headspace.HorizontalAlignment = 0;/**Left=0,Centre=1,Right=2**/
        tableHeader.AddCell(Headspace);



        Headspace = new PdfPCell(new Phrase(" ", TableFontSmall));
        Headspace.BorderWidth = 0;
        Headspace.HorizontalAlignment = 0;/**Left=0,Centre=1,Right=2**/
        tableHeader.AddCell(Headspace);
        doc.Add(tableHeader);
        #endregion



        PdfPTable tblAcNo = new PdfPTable(1);
        float[] colWidthsaccingo = { 1000 };
        tblAcNo.SetWidths(colWidthsaccingo);
        PdfPCell celladdingo;

        celladdingo = new PdfPCell(new Phrase("  ", Smallspace));
        celladdingo.HorizontalAlignment = 1;
        celladdingo.BorderWidth = 0;
        celladdingo.Colspan = 2;
        tblAcNo.AddCell(celladdingo);
        celladdingo = new PdfPCell(new Phrase("  ", Smallspace));
        celladdingo.HorizontalAlignment = 1;
        celladdingo.BorderWidth = 0;
        celladdingo.Colspan = 2;
        tblAcNo.AddCell(celladdingo);
        celladdingo = new PdfPCell(new Phrase("  ", Smallspace));
        celladdingo.HorizontalAlignment = 1;
        celladdingo.BorderWidth = 0;
        celladdingo.Colspan = 2;
        tblAcNo.AddCell(celladdingo);
        celladdingo = new PdfPCell(new Phrase("  ", Smallspace));
        celladdingo.HorizontalAlignment = 1;
        celladdingo.BorderWidth = 0;
        celladdingo.Colspan = 2;
        tblAcNo.AddCell(celladdingo);
        celladdingo = new PdfPCell(new Phrase("  ", Smallspace));
        celladdingo.HorizontalAlignment = 1;
        celladdingo.BorderWidth = 0;
        celladdingo.Colspan = 2;
        tblAcNo.AddCell(celladdingo);
        celladdingo = new PdfPCell(new Phrase("  ", Smallspace));
        celladdingo.HorizontalAlignment = 1;
        celladdingo.BorderWidth = 0;
        celladdingo.Colspan = 2;
        tblAcNo.AddCell(celladdingo);
        celladdingo = new PdfPCell(new Phrase("  ", Smallspace));
        celladdingo.HorizontalAlignment = 1;
        celladdingo.BorderWidth = 0;
        celladdingo.Colspan = 2;
        tblAcNo.AddCell(celladdingo);

        celladdingo = new PdfPCell(new Phrase("  ", Smallspace));
        celladdingo.HorizontalAlignment = 1;
        celladdingo.BorderWidth = 0;
        celladdingo.Colspan = 2;
        tblAcNo.AddCell(celladdingo);

        celladdingo = new PdfPCell(new Phrase("  ", Smallspace));
        celladdingo.HorizontalAlignment = 1;
        celladdingo.BorderWidth = 0;
        celladdingo.Colspan = 2;
        tblAcNo.AddCell(celladdingo);


        celladdingo = new PdfPCell(new Phrase("  ", Smallspace));
        celladdingo.HorizontalAlignment = 1;
        celladdingo.BorderWidth = 0;
        celladdingo.Colspan = 2;
        tblAcNo.AddCell(celladdingo);

        celladdingo = new PdfPCell(new Phrase("  ", Smallspace));
        celladdingo.HorizontalAlignment = 1;
        celladdingo.BorderWidth = 0;
        celladdingo.Colspan = 2;
        tblAcNo.AddCell(celladdingo);

        celladdingo = new PdfPCell(new Phrase(" ", TableFontmini_ARBold8));
        celladdingo.HorizontalAlignment = 0;
        celladdingo.BorderWidth = 0;
        celladdingo.Colspan = 1;
        tblAcNo.AddCell(celladdingo);

        //Chunk c111 = new Chunk("Ref No : ", TableFontmini_ARBoldRef);
        //Chunk c211 = new Chunk((dt.Rows[0]["RefrenceNo"]).ToString(), TableFontmini_ARBold8Nor);
        //Phrase p211 = new Phrase();
        //p211.Add(c111);
        //p211.Add(c211);
        Paragraph pS = new Paragraph();
        //pS.Add(p211);
        /*For gst*/
        /*For space*/
        Chunk cspc = new Chunk("                                                                                                    ", TableFontmini_ARBold8);
        Phrase pcspc = new Phrase();
        pcspc.Add(cspc);
        pS.Add(pcspc);
        /*For space*/

        /*For statecode*/
        Chunk c1111 = new Chunk("Date : ", TableFontmini_ARBoldRef);
        Chunk c2111 = new Chunk((dt.Rows[0]["GenearteDate"]).ToString(), TableFontmini_ARBold8Nor);
        Phrase p2111 = new Phrase();
        p2111.Add(c1111);
        p2111.Add(c2111);

        pS.Add(p2111);
        /*For statecode*/

        /*For finally add*/
        PdfPCell cellDet_4 = new PdfPCell(pS);
        cellDet_4.HorizontalAlignment = 0; /**Left=0,Centre=1,Right=2**/
        cellDet_4.BorderWidth = 0;
        cellDet_4.Colspan = 2;
        tblAcNo.AddCell(cellDet_4);

        doc.Add(tblAcNo);



        PdfPTable tblto = new PdfPTable(1);
        float[] colWidthTo = { 1000 };
        tblto.SetWidths(colWidthTo);
        PdfPCell cellTo;

        cellTo = new PdfPCell(new Phrase("  ", Smallspace));
        cellTo.HorizontalAlignment = 1;
        cellTo.BorderWidth = 0;
        cellTo.Colspan = 2;
        tblto.AddCell(cellTo);

        cellTo = new PdfPCell(new Phrase("  ", Smallspace));
        cellTo.HorizontalAlignment = 1;
        cellTo.BorderWidth = 0;
        cellTo.Colspan = 2;
        tblto.AddCell(cellTo);

        cellTo = new PdfPCell(new Phrase("  ", Smallspace));
        cellTo.HorizontalAlignment = 1;
        cellTo.BorderWidth = 0;
        cellTo.Colspan = 2;
        tblto.AddCell(cellTo);

        cellTo = new PdfPCell(new Phrase("  ", Smallspace));
        cellTo.HorizontalAlignment = 1;
        cellTo.BorderWidth = 0;
        cellTo.Colspan = 2;
        tblto.AddCell(cellTo);

        cellTo = new PdfPCell(new Phrase("  ", Smallspace));
        cellTo.HorizontalAlignment = 1;
        cellTo.BorderWidth = 0;
        cellTo.Colspan = 2;
        tblto.AddCell(cellTo);

        cellTo = new PdfPCell(new Phrase("To, ", TableFontmini_ARBold8Nor));
        cellTo.HorizontalAlignment = 0;
        cellTo.BorderWidth = 0;
        cellTo.Colspan = 1;
        tblto.AddCell(cellTo);

        doc.Add(tblto);



        PdfPTable tblToManager = new PdfPTable(1);
        float[] colWidthToManager = { 1000 };
        tblToManager.SetWidths(colWidthToManager);
        PdfPCell cellToManager;

        cellToManager = new PdfPCell(new Phrase("  ", Smallspace));
        cellToManager.HorizontalAlignment = 1;
        cellToManager.BorderWidth = 0;
        cellToManager.Colspan = 2;
        tblToManager.AddCell(cellToManager);

        cellToManager = new PdfPCell(new Phrase(" ", TableFontmini_ARBold8Nor));
        cellToManager.HorizontalAlignment = 0;
        cellToManager.BorderWidth = 0;
        cellToManager.Colspan = 1;
        tblToManager.AddCell(cellToManager);
        doc.Add(tblToManager);

        PdfPTable tblBillHead = new PdfPTable(1);
        float[] colWidthBillHead = { 1000 };
        tblBillHead.SetWidths(colWidthBillHead);

        PdfPCell celltblBillHead = new PdfPCell(new Paragraph(dt.Rows[0]["Header"].ToString(), TableFontmini_ARBold8));
        celltblBillHead.HorizontalAlignment = 0;
        celltblBillHead.BorderWidth = 0;
        celltblBillHead.Colspan = 1;
        tblBillHead.AddCell(celltblBillHead);

        doc.Add(tblBillHead);

        PdfPTable tblSiteAdd = new PdfPTable(1);
        float[] colWidthSiteAdd = { 1000 };
        tblSiteAdd.SetWidths(colWidthSiteAdd);

        PdfPCell celltblSiteAdd = new PdfPCell(new Paragraph(dt.Rows[0]["Address"].ToString(), TableFontmini_ARBold8Nor));
        celltblSiteAdd.HorizontalAlignment = 0;
        celltblSiteAdd.BorderWidth = 0;
        celltblSiteAdd.Colspan = 1;
        tblSiteAdd.AddCell(celltblSiteAdd);

        doc.Add(tblSiteAdd);

        PdfPTable tblSiteCity = new PdfPTable(1);
        float[] colWidthSiteCity = { 1000 };
        tblSiteCity.SetWidths(colWidthSiteCity);

        PdfPCell celltblSiteCity = new PdfPCell(new Paragraph(dt.Rows[0]["City"].ToString(), TableFontmini_ARBold8));
        celltblSiteCity.HorizontalAlignment = 0;
        celltblSiteCity.BorderWidth = 0;
        celltblSiteCity.Colspan = 1;
        tblSiteCity.AddCell(celltblSiteCity);
        doc.Add(tblSiteCity);



        PdfPTable tblSubject = new PdfPTable(1);
        float[] colWidthSubject = { 1000 };
        tblSubject.SetWidths(colWidthSubject);
        PdfPCell cellSubject;

        cellSubject = new PdfPCell(new Phrase("  ", Smallspace));
        cellSubject.HorizontalAlignment = 1;
        cellSubject.BorderWidth = 0;
        cellSubject.Colspan = 2;
        tblSubject.AddCell(cellSubject);

        cellSubject = new PdfPCell(new Phrase("  ", Smallspace));
        cellSubject.HorizontalAlignment = 1;
        cellSubject.BorderWidth = 0;
        cellSubject.Colspan = 2;
        tblSubject.AddCell(cellSubject);


        cellSubject = new PdfPCell(new Phrase("  ", Smallspace));
        cellSubject.HorizontalAlignment = 1;
        cellSubject.BorderWidth = 0;
        cellSubject.Colspan = 2;
        tblSubject.AddCell(cellSubject);

        cellSubject = new PdfPCell(new Phrase("  ", Smallspace));
        cellSubject.HorizontalAlignment = 1;
        cellSubject.BorderWidth = 0;
        cellSubject.Colspan = 2;
        tblSubject.AddCell(cellSubject);

        cellSubject = new PdfPCell(new Phrase("   Sub.: Application For leave", TableFontmini_ARBold8Sub));
        cellSubject.HorizontalAlignment = 1;/**Left=0,Centre=1,Right=2**/
        cellSubject.BorderWidth = 0;
        cellSubject.Colspan = 1;
        tblSubject.AddCell(cellSubject);

        doc.Add(tblSubject);



        PdfPTable tblDEarSir = new PdfPTable(1);
        float[] colWidthDEarSir = { 1000 };
        tblDEarSir.SetWidths(colWidthDEarSir);
        PdfPCell cellDEarSir;

        cellDEarSir = new PdfPCell(new Phrase("  ", Smallspace));
        cellDEarSir.HorizontalAlignment = 1;
        cellDEarSir.BorderWidth = 0;
        cellDEarSir.Colspan = 2;
        tblDEarSir.AddCell(cellDEarSir);

        cellDEarSir = new PdfPCell(new Phrase("  ", Smallspace));
        cellDEarSir.HorizontalAlignment = 1;
        cellDEarSir.BorderWidth = 0;
        cellDEarSir.Colspan = 2;
        tblDEarSir.AddCell(cellDEarSir);

        cellDEarSir = new PdfPCell(new Phrase("  ", Smallspace));
        cellDEarSir.HorizontalAlignment = 1;
        cellDEarSir.BorderWidth = 0;
        cellDEarSir.Colspan = 2;
        tblDEarSir.AddCell(cellDEarSir);

        cellDEarSir = new PdfPCell(new Phrase("Dear Sir, ", TableFontmini_ARBold8));
        cellDEarSir.HorizontalAlignment = 0;
        cellDEarSir.BorderWidth = 0;
        cellDEarSir.Colspan = 1;
        tblDEarSir.AddCell(cellDEarSir);
        doc.Add(tblDEarSir);

        PdfPTable tblPara1 = new PdfPTable(1);
        float[] colWidthPara1 = { 1200 };
        tblPara1.SetWidths(colWidthPara1);
        PdfPCell cellPara1;

        cellPara1 = new PdfPCell(new Phrase("  ", Smallspace));
        cellPara1.HorizontalAlignment = 1;
        cellPara1.BorderWidth = 0;
        cellPara1.Colspan = 4;
        tblPara1.AddCell(cellPara1);

        cellPara1 = new PdfPCell(new Phrase("  ", Smallspace));
        cellPara1.HorizontalAlignment = 1;
        cellPara1.BorderWidth = 0;
        cellPara1.Colspan = 4;
        tblPara1.AddCell(cellPara1);

        cellPara1 = new PdfPCell(new Phrase("  ", Smallspace));
        cellPara1.HorizontalAlignment = 1;
        cellPara1.BorderWidth = 0;
        cellPara1.Colspan = 4;
        tblPara1.AddCell(cellPara1);

        cellPara1 = new PdfPCell(new Paragraph("i beg to say that i m feelling unwell", TableFontmini_ARBold8Nor));
        cellPara1.HorizontalAlignment = 3;
        cellPara1.BorderWidth = 0;
        cellPara1.Colspan = 1;
        tblPara1.AddCell(cellPara1);
        doc.Add(tblPara1);

        PdfPTable tblPara2 = new PdfPTable(1);
        float[] colWidthPara2 = { 1400 };
        tblPara2.SetWidths(colWidthPara2);
        PdfPCell cellPara2;

        cellPara2 = new PdfPCell(new Phrase("  ", Smallspace));
        cellPara2.HorizontalAlignment = 1;
        cellPara2.BorderWidth = 0;
        cellPara2.Colspan = 4;
        tblPara2.AddCell(cellPara2);
        cellPara2 = new PdfPCell(new Phrase("  ", Smallspace));
        cellPara2.HorizontalAlignment = 1;
        cellPara2.BorderWidth = 0;
        cellPara2.Colspan = 4;
        tblPara2.AddCell(cellPara2);

        cellPara2 = new PdfPCell(new Paragraph("Kindly give me leave for four days ", TableFontmini_ARBold8Nor));
        cellPara2.HorizontalAlignment = 3;
        cellPara2.BorderWidth = 0;
        cellPara2.Colspan = 1;

        tblPara2.AddCell(cellPara2);

        doc.Add(tblPara2);

        PdfPTable tblPara3 = new PdfPTable(1);
        float[] colWidthPara3 = { 1200 };
        tblPara3.SetWidths(colWidthPara3);
        PdfPCell cellPara3;
        cellPara3 = new PdfPCell(new Phrase("  ", Smallspace));
        cellPara3.HorizontalAlignment = 1;
        cellPara3.BorderWidth = 0;
        cellPara3.Colspan = 4;
        tblPara3.AddCell(cellPara3);

        cellPara3 = new PdfPCell(new Paragraph(" from Date" + dt.Rows[0]["Date"].ToString(), TableFontmini_ARBold8Nor));
        cellPara3.HorizontalAlignment = 3;
        cellPara3.BorderWidth = 0;
        cellPara3.Colspan = 1;
        tblPara3.AddCell(cellPara3);
        doc.Add(tblPara3);

        PdfPTable tblLastPara = new PdfPTable(1);
        float[] colWidthLastPara = { 1200 };
        tblPara1.SetWidths(colWidthLastPara);
        PdfPCell cellLastPara;

        cellLastPara = new PdfPCell(new Phrase("  ", Smallspace));
        cellLastPara.HorizontalAlignment = 1;
        cellLastPara.BorderWidth = 0;
        cellLastPara.Colspan = 2;
        tblLastPara.AddCell(cellLastPara);

        cellLastPara = new PdfPCell(new Phrase("  ", Smallspace));
        cellLastPara.HorizontalAlignment = 1;
        cellLastPara.BorderWidth = 0;
        cellLastPara.Colspan = 2;
        tblLastPara.AddCell(cellLastPara);

        cellLastPara = new PdfPCell(new Phrase("  ", Smallspace));
        cellLastPara.HorizontalAlignment = 1;
        cellLastPara.BorderWidth = 0;
        cellLastPara.Colspan = 2;
        tblLastPara.AddCell(cellLastPara);

        cellLastPara = new PdfPCell(new Paragraph("Thank you so much for giving me leave", TableFontmini_ARBold8Nor));
        cellLastPara.HorizontalAlignment = 3;
        cellLastPara.BorderWidth = 0;
        cellLastPara.Colspan = 1;
        tblLastPara.AddCell(cellLastPara);
        doc.Add(tblLastPara);

        PdfPTable tblThankingYou = new PdfPTable(1);
        float[] colWidthThankingYou = { 1000 };
        tblSiteCity.SetWidths(colWidthSiteCity);
        PdfPCell celltblThankingYou;

        celltblThankingYou = new PdfPCell(new Phrase("  ", Smallspace));
        celltblThankingYou.HorizontalAlignment = 1;
        celltblThankingYou.BorderWidth = 0;
        celltblThankingYou.Colspan = 2;
        tblThankingYou.AddCell(celltblThankingYou);

        celltblThankingYou = new PdfPCell(new Phrase("  ", Smallspace));
        celltblThankingYou.HorizontalAlignment = 1;
        celltblThankingYou.BorderWidth = 0;
        celltblThankingYou.Colspan = 2;
        tblThankingYou.AddCell(celltblThankingYou);

        celltblThankingYou = new PdfPCell(new Phrase("  ", Smallspace));
        celltblThankingYou.HorizontalAlignment = 1;
        celltblThankingYou.BorderWidth = 0;
        celltblThankingYou.Colspan = 2;
        tblThankingYou.AddCell(celltblThankingYou);

        celltblThankingYou = new PdfPCell(new Paragraph("Thanking You,", TableFontmini_ARBold8Nor));
        celltblThankingYou.HorizontalAlignment = 0;
        celltblThankingYou.BorderWidth = 0;
        celltblThankingYou.Colspan = 1;
        tblThankingYou.AddCell(celltblThankingYou);
        doc.Add(tblThankingYou);

        PdfPTable tblYorsSinc = new PdfPTable(1);
        float[] colWidthYorsSinc = { 1000 };
        tblYorsSinc.SetWidths(colWidthYorsSinc);
        PdfPCell cellYorsSinc;

        cellYorsSinc = new PdfPCell(new Phrase("  ", Smallspace));
        cellYorsSinc.HorizontalAlignment = 1;
        cellYorsSinc.BorderWidth = 0;
        cellYorsSinc.Colspan = 2;
        tblYorsSinc.AddCell(cellYorsSinc);

        cellYorsSinc = new PdfPCell(new Paragraph("Sincerely Yours,", TableFontmini_ARBold8Nor));
        cellYorsSinc.HorizontalAlignment = 0;
        cellYorsSinc.BorderWidth = 0;
        cellYorsSinc.Colspan = 1;
        tblYorsSinc.AddCell(cellYorsSinc);
        doc.Add(tblYorsSinc);

        PdfPTable tblAuthSignat = new PdfPTable(1);
        float[] colWidthAuthSignat = { 1000 };
        tblAuthSignat.SetWidths(colWidthAuthSignat);
        PdfPCell cellAuthSignat;

        cellAuthSignat = new PdfPCell(new Phrase("  ", Smallspace));
        cellAuthSignat.HorizontalAlignment = 1;
        cellAuthSignat.BorderWidth = 0;
        cellAuthSignat.Colspan = 2;
        tblAuthSignat.AddCell(cellAuthSignat);
        cellAuthSignat = new PdfPCell(new Phrase("  ", Smallspace));
        cellAuthSignat.HorizontalAlignment = 1;
        cellAuthSignat.BorderWidth = 0;
        cellAuthSignat.Colspan = 2;
        tblAuthSignat.AddCell(cellAuthSignat);

        cellAuthSignat = new PdfPCell(new Phrase("  ", Smallspace));
        cellAuthSignat.HorizontalAlignment = 1;
        cellAuthSignat.BorderWidth = 0;
        cellAuthSignat.Colspan = 2;
        tblAuthSignat.AddCell(cellAuthSignat);

        cellAuthSignat = new PdfPCell(new Phrase("  ", Smallspace));
        cellAuthSignat.HorizontalAlignment = 1;
        cellAuthSignat.BorderWidth = 0;
        cellAuthSignat.Colspan = 2;
        tblAuthSignat.AddCell(cellAuthSignat);

        cellAuthSignat = new PdfPCell(new Phrase("  ", Smallspace));
        cellAuthSignat.HorizontalAlignment = 1;
        cellAuthSignat.BorderWidth = 0;
        cellAuthSignat.Colspan = 2;
        tblAuthSignat.AddCell(cellAuthSignat);

        cellAuthSignat = new PdfPCell(new Phrase("  ", Smallspace));
        cellAuthSignat.HorizontalAlignment = 1;
        cellAuthSignat.BorderWidth = 0;
        cellAuthSignat.Colspan = 2;
        tblAuthSignat.AddCell(cellAuthSignat);

        cellAuthSignat = new PdfPCell(new Phrase("  ", Smallspace));
        cellAuthSignat.HorizontalAlignment = 1;
        cellAuthSignat.BorderWidth = 0;
        cellAuthSignat.Colspan = 2;
        tblAuthSignat.AddCell(cellAuthSignat);

        cellAuthSignat = new PdfPCell(new Phrase("  ", Smallspace));
        cellAuthSignat.HorizontalAlignment = 1;
        cellAuthSignat.BorderWidth = 0;
        cellAuthSignat.Colspan = 2;
        tblAuthSignat.AddCell(cellAuthSignat);

        cellAuthSignat = new PdfPCell(new Phrase("  ", Smallspace));
        cellAuthSignat.HorizontalAlignment = 1;
        cellAuthSignat.BorderWidth = 0;
        cellAuthSignat.Colspan = 2;
        tblAuthSignat.AddCell(cellAuthSignat);

        cellAuthSignat = new PdfPCell(new Phrase("  ", Smallspace));
        cellAuthSignat.HorizontalAlignment = 1;
        cellAuthSignat.BorderWidth = 0;
        cellAuthSignat.Colspan = 2;

        cellAuthSignat = new PdfPCell(new Phrase("  ", Smallspace));
        cellAuthSignat.HorizontalAlignment = 1;
        cellAuthSignat.BorderWidth = 0;
        cellAuthSignat.Colspan = 2;
        tblAuthSignat.AddCell(cellAuthSignat);
        tblAuthSignat.AddCell(cellAuthSignat);

        cellAuthSignat = new PdfPCell(new Paragraph("(Student Signatature)", TableFontmini_ARBold8));
        cellAuthSignat.HorizontalAlignment = 0;
        cellAuthSignat.BorderWidth = 0;
        cellAuthSignat.Colspan = 1;
        tblAuthSignat.AddCell(cellAuthSignat);
        doc.Add(tblAuthSignat);

        PdfPTable tblForCom = new PdfPTable(1);
        float[] colWidthForCom = { 1000 };
        tblYorsSinc.SetWidths(colWidthForCom);
        PdfPCell cellForCom;

        cellForCom = new PdfPCell(new Phrase("  ", Smallspace));
        cellForCom.HorizontalAlignment = 1;
        cellForCom.BorderWidth = 0;
        cellForCom.Colspan = 2;
        tblForCom.AddCell(cellForCom);

        cellForCom = new PdfPCell(new Paragraph("For  " + dt.Rows[0]["Name"].ToString(), TableFontmini_ARBold8));
        cellForCom.HorizontalAlignment = 0;
        cellForCom.BorderWidth = 0;
        cellForCom.Colspan = 1;
        tblForCom.AddCell(cellForCom);
        doc.Add(tblForCom);



            pdfwrite.PageEvent = new FooterRN(dt.Rows[0]["Address"].ToString(), Convert.ToInt32(Code.ToString()).ToString(), dt.Rows[0]["Id"].ToString(), dt.Rows[0]["Studentmail"].ToString(), dt.Rows[0]["PhoneNo1"].ToString(), dt.Rows[0]["StudentName"].ToString());


        doc.Close();
    }
查看更多
Lonely孤独者°
6楼-- · 2020-03-03 06:27

Check out following examples of using Itext.

Itext Examples for tables lists and images

You can further search for html to pdf converter. There are lot of free tools available. You need to pass your html containing the table and it will return the pdf document. I have developed one such application. Let me know if u need it.

查看更多
登录 后发表回答