Thursday, March 7, 2024

How to convert div to PDF with Itextsharp without font size change

 DataTable dt = new DataTable();

          

            dt = ObjBAL.GetBudgetCompilationRptDetails(ddlfinancialyr.SelectedValue);


            iTextSharp.text.Document pdfDoc = new iTextSharp.text.Document(PageSize.A4_LANDSCAPE, 10f, 10f, 10, 0f);

            //PdfPTable table1 = new PdfPTable(3);

            //table1.HorizontalAlignment = 0;

            //table1.WidthPercentage = 100;

            //table1.SetWidths(new float[] { 5, 15, 15 });  // then set the column's __relative__ widths

            //table1.SpacingAfter = 40f;

            //table1.SpacingBefore = 20f;


            //table1.AddCell("S.No");

            //table1.AddCell("Name");

            //table1.AddCell("PNR No");


            //foreach (DataRow row in dt.Rows)

            //{

            //    table1.AddCell(new Phrase(row["RowNumber"].ToString()));

            //    table1.AddCell(new Phrase(row["PassengerName"].ToString()));

            //    table1.AddCell(new Phrase(row["PnrNo"].ToString()));

            //}


            if (dt.Rows.Count > 0)

            {

                //Create a dummy GridView

                GridView GridView1 = new GridView();

                GridView1.AllowPaging = false;

                GridView1.DataSource = dt;

                GridView1.DataBind();


                Response.ContentType = "application/pdf";

                Response.AddHeader("content-disposition", "attachment;filename=BudgetCompilationRpt.pdf");

                Response.Cache.SetCacheability(HttpCacheability.NoCache);

                StringWriter sw = new StringWriter();

                HtmlTextWriter hw = new HtmlTextWriter(sw);

                GridView1.RenderControl(hw);

                StringReader sr = new StringReader(sw.ToString());

                // Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 10f, 0f);



                HTMLWorker htmlparser = new HTMLWorker(pdfDoc);

                PdfWriter.GetInstance(pdfDoc, Response.OutputStream);

                pdfDoc.Open();

                htmlparser.Parse(sr);

                pdfDoc.Close();

                Response.Write(pdfDoc);

                Response.End();

            }

            else

            {

                //string strSuccessMsg = "Data Not Saved. Some error occured while saving the data.";

                //ScriptManager.RegisterStartupScript(formUpdatePanel, formUpdatePanel.GetType(), Guid.NewGuid().ToString(), String.Format("OpenMessageNotification('" + strSuccessMsg + "'); "), true);

            }



reference:-https://www.c-sharpcorner.com/UploadFile/raj1979/export-div-content-to-pdf-using-itextsharp/

No comments:

Post a Comment