locked
create a itextsharp pdf on aspx page. RRS feed

  • Question

  • User-1651858287 posted
    I am using a Wizard control I would like to create a pdf before the user
    goes to ActiveStep 5.
    I was wondering what I have to add to the page directive
    <%@ Page Language="C#" ..../>

    I am not using the code behind only on the aspx page.
    void OnNextButtonClick(object sender, WizardNavigationEventArgs e) 
    {
     
     
         if(Wizard1.ActiveStepIndex == 4)  
          {     
            //create pdf
          }
    }

    thanks.

    Saturday, June 27, 2020 5:47 PM

All replies

  • User-2054057000 posted

    You can create button and button click event and inside this event you can generate pdf using iTextSharp. See this article on iTextSharp to understand.

    Sunday, June 28, 2020 4:40 AM
  • User288213138 posted

    Hi sweetSteal,

    I am not using the code behind only on the aspx page.

     if(Wizard1.ActiveStepIndex == 4)   {     //create pdf     }

    According to your description, I couldn’t understand your requirement clearly.

    Do you mean you want to create a itextsharp pdf in code behind?

    If this is your requirement, I suggest you could refer to this code about how to create a pdf file by itextsharp.

    <asp:GridView ID="GridView1" runat="server"></asp:GridView>
    <asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
    
     protected void Page_Load(object sender, EventArgs e)
            {
                if (!IsPostBack)
                {
                    DataTable dt = new DataTable();
                    dt.Columns.AddRange(new DataColumn[3] { new DataColumn("Id"), new DataColumn("Name"), new DataColumn("Country") });
                    dt.Rows.Add(1, "n1", "c1");
                    dt.Rows.Add(2, "n2", "c2");
                    dt.Rows.Add(3, "n3", "c3");
                    GridView1.DataSource = dt;
                    GridView1.DataBind();
                }
            }
    
            protected void Button1_Click(object sender, EventArgs e)
            {
                Rectangle rect = new Rectangle(500, 300);
                Document pdfDoc = new Document(rect, 10F, 10F, 10F, 0F);
    
                using (StringWriter sw = new StringWriter())
                {
                    using (HtmlTextWriter hw = new HtmlTextWriter(sw))
                    {
                        GridView1.RenderControl(hw);
                        HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
                        StringReader sr = new StringReader(sw.ToString());
                        PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
                        pdfDoc.Open();
                        htmlparser.Parse(sr);
                    }
                }
    
                pdfDoc.Close();
                Response.ContentType = "application/pdf";
                Response.AddHeader("content-disposition", "attachment;filename=GridViewExport.pdf");
                Response.Cache.SetCacheability(HttpCacheability.NoCache);
                Response.Write(pdfDoc);
                Response.End();
            }
            public override void VerifyRenderingInServerForm(Control control)
            {
            }

    If I misunderstand your requirement, please post more details information about your requirement.

    Best regards,

    Sam

    Monday, June 29, 2020 2:34 AM