locked
Export dynamic values to PDF using itextsharp c# RRS feed

  • Question

  • User1042252450 posted
    Hi,
    I am working on a project where a requirement is to export to pdf for multiple application using itextsharp in c#/asp.net. I have a screen with textbox controls, chart and grid and i am able to export to PDF for single application passing application ID to DB and values will be assigned in appropriate controls and i have used HtmlTextWriter, XMLWorkerHelper for PDF export. Now i want to export these values in PDF for multiple application. But I am getting PDF damaged error when i try to export more than one application. But its working fine when i have another set of new controls.(Tested for two applications But it won't workout for multiple application just tested for R&D). How well do they work for single set of controls changing values dynamically and append in PDF?

    For example:
    for each application
    {
    Step 1: Assigning dynamic values to controls
    Assigning values to textbox,chart,grid (It will hit DB and assign values for each application)
    Step2: (Export to PDF)
    XMLWorkerHelper.GetInstance().ParseXHtml(pdfWriter, pdfDoc, sr1);
    }
    Response.ClearHeaders();
    Response.ContentType = "application/pdf";
    Response.AddHeader("content-disposition", "attachment;filename='" + lblapp.Text + "'.pdf");
    Response.Write(pdfDoc);
    Response.End();
    Wednesday, December 16, 2015 3:07 PM

All replies

  • User1042252450 posted
    Any Update
    Saturday, December 19, 2015 6:57 AM
  • User954927490 posted

    Hi,

    can you send the code on vishwaraj.malik2000@gmail.com so that I can check there.

    Saturday, December 19, 2015 8:52 AM
  • User1042252450 posted
    Hi,

    I have sent to sample working code to your gmail id.
    Tuesday, December 22, 2015 6:19 AM
  • User954927490 posted

    Hi, I checked the code
    Response.ClearContent();
    this line is causing the issue.

    Tuesday, December 22, 2015 7:50 AM
  • User954927490 posted

    somthing like belwo code will work

     Document pdfDoc = new Document(PageSize.A4.Rotate(), 40f, 40f, 40f, 40f);
            HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
            PdfWriter pdfWriter = PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
            StringWriter sw1;
            StringWriter sw2 = new StringWriter();
    
            string s = "sample";
            //string[] eai_Cd = { "8911","1740" }; Not working for two codes
            string[] eai_Cd = { "8911", "8911" }; //working fine for single code
            int k = 0;
            StringBuilder sb = new StringBuilder();
            Response.ClearContent();
            for (int p = 0; p < eai_Cd.Length; p++)
            {
                k = p;
               
                txtEaiCode1.Text = eai_Cd[p] + "line one " + p.ToString();
                txtEaiCode2.Text = eai_Cd[p] + "line two " + p.ToString();
                ClearControls(App);
                //base.OnPreRender(e);
                sw1 = new StringWriter();
                HtmlTextWriter hw = new HtmlTextWriter(sw1);
    
                App.Style.Add("width", "95%");
                App.Style.Add("font-family", "Arial");
                App.Style.Add("font-size", "10px");
                exprt.Style.Add("padding-left", "50px");
                exprt.RenderControl(hw);
                if (!pdfDoc.IsOpen())
                    pdfDoc.Open();
                sb.Append(sw1.ToString());
               
            }
    
            StringReader sr1 = new StringReader(sb.ToString());
            XMLWorkerHelper.GetInstance().ParseXHtml(pdfWriter, pdfDoc, sr1);
    
    
            pdfDoc.Close();
            Response.ClearHeaders();
            Response.ContentType = "application/pdf";
            Response.AddHeader("content-disposition", "attachment;filename='" + s + "'.pdf");
            Response.Write(pdfDoc);



    Tuesday, December 22, 2015 8:03 AM
  • User1042252450 posted
    Hi,

    I already tried lot of scenario's to change control values dynamically in PDF. But still issue exists.
    Your code has duplicate values - string[] eai_Cd = { "8911","8911" };

    I tried for different codes but still issues exists.
    string[] eai_Cd = { "8911","1740" };
    Tuesday, December 22, 2015 11:07 AM