locked
HTML to PDF RRS feed

  • Question

  • User126547376 posted

    Hi Friends help me in converting HTML file to PDF.

    My requirement is i have an HTML file in D://Security.HTM, using iTextSharp/PDFIZER/ any open source Dll i have to convert that file in to PDF.

    Help me with Sample.


    Tuesday, September 7, 2010 12:37 AM

All replies

  • User-1802908944 posted

    do as like

     

       private String UploadPDFData()
            {
                String PDFfilename = String.Empty;

                try
                {
                    #region Labels

                    Label lblPlacementRef = (Label)FormView1.FindControl("lblPlacementRef");
                    Label lblClient = (Label)FormView1.FindControl("lblClient");
                    Label lblAccountNo = (Label)FormView1.FindControl("lblAccountNo");
                    Label lblMrNo = (Label)FormView1.FindControl("lblMrNo");
                    Label lblHandOverAmount = (Label)FormView1.FindControl("lblHandOverAmount");

                    Label lblDebtorTitle = (Label)FormView1.FindControl("lblDebtorTitle");
                    Label lblDebtorFirstName = (Label)FormView1.FindControl("lblDebtorFirstName");
                    Label lblDebtorLastName = (Label)FormView1.FindControl("lblDebtorLastName");
                    Label lblDebtorIDNum = (Label)FormView1.FindControl("lblDebtorIDNum");
                    Label lblDebtorGender = (Label)FormView1.FindControl("lblDebtorGender");
                    Label lblDebtorDOB = (Label)FormView1.FindControl("lblDebtorDOB");
                    Label lblDebtorTelHome = (Label)FormView1.FindControl("lblDebtorTelHome");
                    Label lblDebtorTelWork = (Label)FormView1.FindControl("lblDebtorTelWork");
                    Label lblDebtorTelCell = (Label)FormView1.FindControl("lblDebtorTelCell");
                    TextBox txtComments = (TextBox)FormView1.FindControl("txtComments");
                    Label lblGurantorFirstName = (Label)FormView1.FindControl("lblGurantorFirstName");
                    Label lblGLastName = (Label)FormView1.FindControl("lblGLastName");
                    Label lblSubjectIsAccountGuarantor = (Label)FormView1.FindControl("lblSubjectIsAccountGuarantor");
                    Label lblGtype = (Label)FormView1.FindControl("lblGtype");
                    #endregion

                    #region ParagraphDeclare

                    Paragraph opara1 = new Paragraph();
                    Paragraph opara2 = new Paragraph();
                    Paragraph opara3 = new Paragraph();
                    Paragraph opara4 = new Paragraph();
                    Paragraph opara5 = new Paragraph();

                    Paragraph opara6 = new Paragraph();
                    Paragraph opara7 = new Paragraph();
                    Paragraph opara8 = new Paragraph();
                    Paragraph opara9 = new Paragraph();
                    Paragraph opara10 = new Paragraph();

                    Paragraph opara11 = new Paragraph();
                    Paragraph opara12 = new Paragraph();
                    Paragraph opara13 = new Paragraph();
                    #endregion

                    //String iCreatePDFdir = "C:\\iCreatePDF\\PDFTest";
                    //String iCreatePDFTest = iCreatePDFdir + "\\PDFTest.pdf";

                    // String iCreatePDFdir = Server.MapPath("~//secure_store//Reports//"); 
                    String BasePath = ConfigurationManager.AppSettings.Get("ReportWebPath");
                    String iCreatePDFdir = Server.MapPath(BasePath);
                    String PDFDirPath = iCreatePDFdir + lblClient.Text + "\\New Placements";

                    if (Directory.Exists(PDFDirPath))
                    {

                    }
                    else
                    {
                        Directory.CreateDirectory(PDFDirPath);
                    }
                    //  String concatenateDate = System.DateTime.Now.ToShortDateString()+ Convert.ToString(DateTime.Now.ToUniversalTime());
                    String concatenateDate = DateTime.Now.ToString("MM/dd/yyyy HH:mm:ss");
                    concatenateDate = concatenateDate.Replace('/', '-');
                    concatenateDate = concatenateDate.Replace(' ', '_');
                    concatenateDate = concatenateDate.Replace(':', '-');
                    String iCreatePDF = PDFDirPath + "\\" + lblDebtorFirstName.Text.Trim() + "_" + lblDebtorLastName.Text.Trim() + "_" + concatenateDate + ".pdf";

                    Document pdfDocCreatePDF = new Document();
                    PdfWriter.GetInstance(pdfDocCreatePDF, new FileStream(iCreatePDF, FileMode.Create));
                    pdfDocCreatePDF.Open();

                    PDFfilename = iCreatePDF;
                    #region Paragraph
                    opara1.Add("Client : " + lblClient.Text);
                    opara1.Font.Size = 9;
                    opara1.Alignment = Element.ALIGN_CENTER;

                    opara2.Add("Account Number : " + lblAccountNo.Text);
                    opara2.Font.Size = 9;
                    opara2.Alignment = Element.ALIGN_CENTER;

                    opara3.Add("MR Number : " + lblMrNo.Text);
                    opara3.Font.Size = 9;
                    opara3.Alignment = Element.ALIGN_CENTER;

                    opara4.Add("Handover Amount : " + lblHandOverAmount.Text);
                    opara4.Font.Size = 9;
                    opara4.Alignment = Element.ALIGN_CENTER;

                    opara5.Add("Title : " + lblDebtorTitle.Text);
                    opara5.Font.Size = 9;
                    opara5.Alignment = Element.ALIGN_CENTER;

                    opara6.Add("First Name : " + lblDebtorFirstName.Text);
                    opara6.Font.Size = 9;
                    opara6.Alignment = Element.ALIGN_CENTER;

                    opara7.Add("Last Name : " + lblDebtorLastName.Text);
                    opara7.Font.Size = 9;
                    opara7.Alignment = Element.ALIGN_CENTER;

                    opara8.Add("ID Number : " + lblDebtorIDNum.Text);
                    opara8.Font.Size = 9;
                    opara8.Alignment = Element.ALIGN_CENTER;

                    opara9.Add("Gender : " + lblDebtorGender.Text);
                    opara9.Font.Size = 9;
                    opara9.Alignment = Element.ALIGN_CENTER;

                    opara10.Add("Date of Birth : " + lblDebtorDOB.Text);
                    opara10.Font.Size = 9;
                    opara10.Alignment = Element.ALIGN_CENTER;

                    opara11.Add("Home Phone : " + lblDebtorTelHome.Text);
                    opara11.Font.Size = 8;
                    opara11.Alignment = Element.ALIGN_CENTER;

                    opara12.Add("Work Phone : " + lblDebtorTelWork.Text);
                    opara12.Font.Size = 9;
                    opara12.Alignment = Element.ALIGN_CENTER;

                    opara13.Add("Cell Phone : " + lblDebtorTelCell.Text);
                    opara13.Font.Size = 9;
                    opara13.Alignment = Element.ALIGN_CENTER;
                    #endregion

                    iTextSharp.text.Image logo = iTextSharp.text.Image.GetInstance(Server.MapPath("~\\Images\\logosmall.jpg"));
                    logo.ScaleAbsoluteHeight(60f);
                    logo.ScaleAbsoluteWidth(90f);
                    //logo.SpacingBefore = 80;
                    //logo.SpacingAfter = 20;
                    //logo.SetAbsolutePosition(100f, 0f);
                    logo.Alignment = Element.ALIGN_RIGHT;
                    logo.IndentationRight = 350f;
                    pdfDocCreatePDF.Add(logo);

                    #region Table Settings

                    PdfPTable table = new PdfPTable(2);
                    table.TotalWidth = 340f; //245
                    table.DefaultCell.Border = 1;
                    //fix the absolute width of the table
                    table.LockedWidth = true;

                    //relative col widths in proportions - 1/3 and 2/3
                    float[] widths = new float[] { 1f, 2f };

                    table.SetWidths(widths);
                    table.HorizontalAlignment = Element.ALIGN_CENTER;

                    //leave a gap before and after the table
                    table.SpacingBefore = 20f;
                    table.SpacingAfter = 30f;

                    PdfPCell cell = new PdfPCell(new Phrase("Basic Account Information", FontFactory.GetFont(FontFactory.HELVETICA, 9, iTextSharp.text.Font.BOLD)));
                    cell.Colspan = 2;
                    cell.Border = 1;
                    cell.HorizontalAlignment = Element.ALIGN_LEFT;
                    table.AddCell(cell);
                    #endregion

                    #region TableCells

                    PdfPCell cell27 = new PdfPCell(new Phrase("Placement Ref  ", FontFactory.GetFont(FontFactory.HELVETICA, 8)));
                    cell27.Border = 1;
                    cell27.HorizontalAlignment = Element.ALIGN_LEFT;
                    table.AddCell(cell27);

                    PdfPCell cell28 = new PdfPCell(new Phrase(" : " + lblPlacementRef.Text, FontFactory.GetFont(FontFactory.HELVETICA, 8)));
                    cell28.Border = 1;
                    cell28.HorizontalAlignment = Element.ALIGN_LEFT;
                    table.AddCell(cell28);


                    PdfPCell cell1 = new PdfPCell(new Phrase("Client  ", FontFactory.GetFont(FontFactory.HELVETICA, 8)));
                    cell1.Border = 1;
                    cell1.HorizontalAlignment = Element.ALIGN_LEFT;
                    table.AddCell(cell1);

                    PdfPCell cell2 = new PdfPCell(new Phrase(" : " + lblClient.Text, FontFactory.GetFont(FontFactory.HELVETICA, 8)));
                    cell2.Border = 1;
                    cell2.HorizontalAlignment = Element.ALIGN_LEFT;
                    table.AddCell(cell2);


                    PdfPCell cell3 = new PdfPCell(new Phrase("Account no  ", FontFactory.GetFont(FontFactory.HELVETICA, 8)));
                    cell3.Border = 1;
                    cell3.HorizontalAlignment = Element.ALIGN_LEFT;
                    table.AddCell(cell3);

                    PdfPCell cell4 = new PdfPCell(new Phrase(" : " + lblAccountNo.Text, FontFactory.GetFont(FontFactory.HELVETICA, 8)));
                    cell4.Border = 1;
                    cell4.HorizontalAlignment = Element.ALIGN_LEFT;
                    table.AddCell(cell4);

                    PdfPCell cell5 = new PdfPCell(new Phrase("MR Number  ", FontFactory.GetFont(FontFactory.HELVETICA, 8)));
                    cell5.Border = 1;
                    cell5.HorizontalAlignment = Element.ALIGN_LEFT;
                    table.AddCell(cell5);

                    PdfPCell cell6 = new PdfPCell(new Phrase(" : " + lblMrNo.Text, FontFactory.GetFont(FontFactory.HELVETICA, 8)));
                    cell6.Border = 1;
                    cell6.HorizontalAlignment = Element.ALIGN_LEFT;
                    table.AddCell(cell6);


                    PdfPCell cell7 = new PdfPCell(new Phrase("Handover Amount   ", FontFactory.GetFont(FontFactory.HELVETICA, 8)));
                    cell7.Border = 1;
                    cell7.HorizontalAlignment = Element.ALIGN_LEFT;
                    table.AddCell(cell7);

                    PdfPCell cell8 = new PdfPCell(new Phrase(" : " + lblHandOverAmount.Text, FontFactory.GetFont(FontFactory.HELVETICA, 8)));
                    cell8.Border = 1;
                    cell8.HorizontalAlignment = Element.ALIGN_LEFT;
                    table.AddCell(cell8);

                    PdfPCell cell29 = new PdfPCell(new Phrase("  ", FontFactory.GetFont(FontFactory.HELVETICA, 8)));
                    cell29.Colspan = 2;
                    cell29.Border = 1;
                    cell29.HorizontalAlignment = Element.ALIGN_LEFT;
                    table.AddCell(cell29);

                    PdfPCell cell30 = new PdfPCell(new Phrase("Subject Details  ", FontFactory.GetFont(FontFactory.HELVETICA, 9, iTextSharp.text.Font.BOLD)));
                    cell30.Colspan = 2;
                    cell30.Border = 1;
                    cell30.HorizontalAlignment = Element.ALIGN_LEFT;
                    table.AddCell(cell30);


                    PdfPCell cell9 = new PdfPCell(new Phrase("Title  ", FontFactory.GetFont(FontFactory.HELVETICA, 8)));
                    cell9.Border = 1;
                    cell9.HorizontalAlignment = Element.ALIGN_LEFT;
                    table.AddCell(cell9);

                    PdfPCell cell10 = new PdfPCell(new Phrase(" : " + lblDebtorTitle.Text, FontFactory.GetFont(FontFactory.HELVETICA, 8)));
                    cell10.Border = 1;
                    cell10.HorizontalAlignment = Element.ALIGN_LEFT;
                    table.AddCell(cell10);

                    PdfPCell cell11 = new PdfPCell(new Phrase("First Name  ", FontFactory.GetFont(FontFactory.HELVETICA, 8)));
                    cell11.Border = 1;
                    cell11.HorizontalAlignment = Element.ALIGN_LEFT;
                    table.AddCell(cell11);

                    PdfPCell cell12 = new PdfPCell(new Phrase(" : " + lblDebtorFirstName.Text, FontFactory.GetFont(FontFactory.HELVETICA, 8)));
                    cell12.Border = 1;
                    cell12.HorizontalAlignment = Element.ALIGN_LEFT;
                    table.AddCell(cell12);

                    PdfPCell cell13 = new PdfPCell(new Phrase("Last Name  ", FontFactory.GetFont(FontFactory.HELVETICA, 8)));
                    cell13.Border = 1;
                    cell13.HorizontalAlignment = Element.ALIGN_LEFT;
                    table.AddCell(cell13);

                    PdfPCell cell14 = new PdfPCell(new Phrase(" : " + lblDebtorLastName.Text, FontFactory.GetFont(FontFactory.HELVETICA, 8)));
                    cell14.Border = 1;
                    cell14.HorizontalAlignment = Element.ALIGN_LEFT;
                    table.AddCell(cell14);

                    PdfPCell cell15 = new PdfPCell(new Phrase("Gender  ", FontFactory.GetFont(FontFactory.HELVETICA, 8)));
                    cell15.Border = 1;
                    cell15.HorizontalAlignment = Element.ALIGN_LEFT;
                    table.AddCell(cell15);

                    PdfPCell cell16 = new PdfPCell(new Phrase(" : " + lblDebtorGender.Text, FontFactory.GetFont(FontFactory.HELVETICA, 8)));
                    cell16.Border = 1;
                    cell16.HorizontalAlignment = Element.ALIGN_LEFT;
                    table.AddCell(cell16);


                    PdfPCell cell17 = new PdfPCell(new Phrase("ID Number  ", FontFactory.GetFont(FontFactory.HELVETICA, 8)));
                    cell17.Border = 1;
                    cell17.HorizontalAlignment = Element.ALIGN_LEFT;
                    table.AddCell(cell17);

                    PdfPCell cell18 = new PdfPCell(new Phrase(" : " + lblDebtorIDNum.Text, FontFactory.GetFont(FontFactory.HELVETICA, 8)));
                    cell18.Border = 1;
                    cell18.HorizontalAlignment = Element.ALIGN_LEFT;
                    table.AddCell(cell18);

                    PdfPCell cell19 = new PdfPCell(new Phrase("Date of Birth  ", FontFactory.GetFont(FontFactory.HELVETICA, 8)));
                    cell19.Border = 1;
                    cell19.HorizontalAlignment = Element.ALIGN_LEFT;
                    table.AddCell(cell19);

                    PdfPCell cell20 = new PdfPCell(new Phrase(" : " + lblDebtorDOB.Text, FontFactory.GetFont(FontFactory.HELVETICA, 8)));
                    cell20.Border = 1;
                    cell20.HorizontalAlignment = Element.ALIGN_LEFT;
                    table.AddCell(cell20);

                    PdfPCell cell21 = new PdfPCell(new Phrase("Home Phone  ", FontFactory.GetFont(FontFactory.HELVETICA, 8)));
                    cell21.Border = 1;
                    cell21.HorizontalAlignment = Element.ALIGN_LEFT;
                    table.AddCell(cell21);

                    PdfPCell cell22 = new PdfPCell(new Phrase(" : " + lblDebtorTelHome.Text, FontFactory.GetFont(FontFactory.HELVETICA, 8)));
                    cell22.Border = 1;
                    cell22.HorizontalAlignment = Element.ALIGN_LEFT;
                    table.AddCell(cell22);


                    PdfPCell cell23 = new PdfPCell(new Phrase("Work Phone  ", FontFactory.GetFont(FontFactory.HELVETICA, 8)));
                    cell23.Border = 1;
                    cell23.HorizontalAlignment = Element.ALIGN_LEFT;
                    table.AddCell(cell23);

                    PdfPCell cell24 = new PdfPCell(new Phrase(" : " + lblDebtorTelWork.Text, FontFactory.GetFont(FontFactory.HELVETICA, 8)));
                    cell24.Border = 1;
                    cell24.HorizontalAlignment = Element.ALIGN_LEFT;
                    table.AddCell(cell24);

                    PdfPCell cell25 = new PdfPCell(new Phrase("Cell Phone  ", FontFactory.GetFont(FontFactory.HELVETICA, 8)));
                    cell25.Border = 1;
                    cell25.HorizontalAlignment = Element.ALIGN_LEFT;
                    table.AddCell(cell25);

                    PdfPCell cell26 = new PdfPCell(new Phrase(" : " + lblDebtorTelCell.Text, FontFactory.GetFont(FontFactory.HELVETICA, 8)));
                    cell26.Border = 1;
                    cell26.HorizontalAlignment = Element.ALIGN_LEFT;
                    table.AddCell(cell26);

                    PdfPCell cell31 = new PdfPCell(new Phrase("  ", FontFactory.GetFont(FontFactory.HELVETICA, 8)));
                    cell31.Colspan = 2;
                    cell31.Border = 1;
                    cell31.HorizontalAlignment = Element.ALIGN_LEFT;
                    table.AddCell(cell31);


                    PdfPCell cell41 = new PdfPCell(new Phrase("Subject is Account Guarantor  ", FontFactory.GetFont(FontFactory.HELVETICA, 8)));
                    //cell41.Colspan = 2;
                    cell41.Border = 1;
                    cell41.HorizontalAlignment = Element.ALIGN_LEFT;
                    table.AddCell(cell41);

                    PdfPCell cell42 = new PdfPCell(new Phrase(" : " + lblSubjectIsAccountGuarantor.Text, FontFactory.GetFont(FontFactory.HELVETICA, 8)));
                    cell42.Border = 1;
                    cell42.HorizontalAlignment = Element.ALIGN_LEFT;
                    table.AddCell(cell42);

                    PdfPCell cell43 = new PdfPCell(new Phrase("  ", FontFactory.GetFont(FontFactory.HELVETICA, 8)));
                    cell43.Colspan = 2;
                    cell43.Border = 1;
                    cell43.HorizontalAlignment = Element.ALIGN_LEFT;
                    table.AddCell(cell43);


                    #region "Guranator"

                    if (lblSubjectIsAccountGuarantor.Text.ToLower() == "no")
                    {
                        PdfPCell cell36 = new PdfPCell(new Phrase(" Guarantor Details ", FontFactory.GetFont(FontFactory.HELVETICA, 9, iTextSharp.text.Font.BOLD)));
                        cell36.Colspan = 2;
                        cell36.Border = 1;
                        cell36.HorizontalAlignment = Element.ALIGN_LEFT;
                        table.AddCell(cell36);

                        if (lblGtype.Text.ToLower() == "private")
                        {
                            PdfPCell cell37 = new PdfPCell(new Phrase(" Guarantor FirstName ", FontFactory.GetFont(FontFactory.HELVETICA, 8)));
                            cell37.Border = 1;
                            cell37.HorizontalAlignment = Element.ALIGN_LEFT;
                            table.AddCell(cell37);

                            PdfPCell cell38 = new PdfPCell(new Phrase(" : " + lblGurantorFirstName.Text, FontFactory.GetFont(FontFactory.HELVETICA, 8)));
                            cell38.Border = 1;
                            cell38.HorizontalAlignment = Element.ALIGN_LEFT;
                            table.AddCell(cell38);

                            PdfPCell cell39 = new PdfPCell(new Phrase(" Guarantor LastName ", FontFactory.GetFont(FontFactory.HELVETICA, 8)));
                            cell39.Border = 1;
                            cell39.HorizontalAlignment = Element.ALIGN_LEFT;
                            table.AddCell(cell39);

                            PdfPCell cell40 = new PdfPCell(new Phrase(" : " + lblGLastName.Text, FontFactory.GetFont(FontFactory.HELVETICA, 8)));
                            cell40.Border = 1;
                            cell40.HorizontalAlignment = Element.ALIGN_LEFT;
                            table.AddCell(cell40);
                        }
                        else
                            if (lblGtype.Text.ToLower() == "company")
                            {
                                PdfPCell cell44 = new PdfPCell(new Phrase("Company Name  ", FontFactory.GetFont(FontFactory.HELVETICA, 8)));

                                cell44.Border = 1;
                                cell44.HorizontalAlignment = Element.ALIGN_LEFT;
                                table.AddCell(cell44);

                                PdfPCell cell45 = new PdfPCell(new Phrase(" : " + lblGurantorFirstName.Text, FontFactory.GetFont(FontFactory.HELVETICA, 8)));
                                cell45.Border = 1;
                                cell45.HorizontalAlignment = Element.ALIGN_LEFT;
                                table.AddCell(cell45);

                            }
                    }

                    #endregion


                    PdfPCell cell35 = new PdfPCell(new Phrase("  ", FontFactory.GetFont(FontFactory.HELVETICA, 8)));
                    cell35.Colspan = 2;
                    cell35.Border = 1;
                    cell35.HorizontalAlignment = Element.ALIGN_LEFT;
                    table.AddCell(cell35);

                    PdfPCell cell32 = new PdfPCell(new Phrase(" Additional Comments ", FontFactory.GetFont(FontFactory.HELVETICA, 9, iTextSharp.text.Font.BOLD)));
                    cell32.Colspan = 2;
                    cell32.Border = 1;
                    cell32.HorizontalAlignment = Element.ALIGN_LEFT;
                    table.AddCell(cell32);

                    PdfPCell cell33 = new PdfPCell(new Phrase(" Comments ", FontFactory.GetFont(FontFactory.HELVETICA, 8)));
                    cell33.Border = 1;
                    cell33.HorizontalAlignment = Element.ALIGN_LEFT;
                    table.AddCell(cell33);

                    PdfPCell cell34 = new PdfPCell(new Phrase(" : " + txtComments.Text, FontFactory.GetFont(FontFactory.HELVETICA, 8)));
                    cell34.Border = 1;
                    cell34.HorizontalAlignment = Element.ALIGN_LEFT;
                    table.AddCell(cell34);

                    #endregion
                    pdfDocCreatePDF.Add(table);

                    #region ParagraphEnd

                    //pdfDocCreatePDF.Add(opara1);
                    //pdfDocCreatePDF.Add(opara2);
                    //pdfDocCreatePDF.Add(opara3);
                    //pdfDocCreatePDF.Add(opara4);

                    //pdfDocCreatePDF.Add(opara5);
                    //pdfDocCreatePDF.Add(opara6);
                    //pdfDocCreatePDF.Add(opara7);
                    //pdfDocCreatePDF.Add(opara8);

                    //pdfDocCreatePDF.Add(opara9);
                    //pdfDocCreatePDF.Add(opara10);
                    //pdfDocCreatePDF.Add(opara11);
                    //pdfDocCreatePDF.Add(opara12);
                    //pdfDocCreatePDF.Add(opara13);
                    #endregion

                    pdfDocCreatePDF.Close();
                    Session["PLacementRef"] = lblPlacementRef.Text;

                }
                catch (Exception ex)
                {
                    EmailSettings.SendErrorEmail(ex.Message, ex.Source, Convert.ToString(ex.InnerException), "_*_UploadPDFData_*_");
                }
                return PDFfilename;

            }

    Tuesday, September 7, 2010 12:58 AM
  • User-863835478 posted

    Please check here:

    http://www.go2pdf.com/product.html

     

     

    Tuesday, September 7, 2010 1:01 AM
  • User126547376 posted

    Thanks for your reply.

    But i dont understand what to do with the source code given by You.

    Ex: i will have 'n' no of HTML Files in the Specific folder. when i click button one by one HTML Files should generate as PDF Files.

    U try like this. Keep one html file in D:// or C://  then through coding call this HTML FIle & then convert it into PDF.


    Thanks in Advance


    Tuesday, September 7, 2010 1:46 AM