-2

I'm currently building a small website that takes data from 6 different web forms written in ASP.NET and C#. I need all of the information to be written to a PDF at the end of Page 6. I currently have all fields mapped, but each time the page changes, the information gets wiped. i have the information set to pass the values using a query string, but it seems to keep getting lost. Any advice?

EDIT Sorry for not posting the code the first time, this is my first question i ever posted here. Also, I know my code isn't very efficient, I'm more or less trying to get the grasp of iTextSharp and WebForms. Thanks

Page 1
            AcroFields af = ps.AcroFields;
        af.SetField("Name", name.Text);

        af.SetField("Email", email.Text);
        af.SetField("state", state.Text);
        af.SetField("city", city.Text);
        af.SetField("Address", address.Text);
        af.SetField("Phone", phone.Text);
        af.SetField("zip", zip.Text);
        Response.Redirect("Default.aspx?name=" + this.name.Text + "&Email=" + this.email.Text 
        + "&state=" + this.state.Text + "&city=" + this.city.Text + "&Address=" + this.address.Text + 
        "&Phone=" + this.phone.Text + "&zip=" + this.zip.Text);
        Response.Redirect("Page2.aspx");

Page 2
AcroFields af = ps.AcroFields;
            af.SetField("Degree", degree.Text);
            af.SetField("Grad", gradDate.Text);
            af.SetField("Obj", objective.Text);
            Response.Redirect("Default.aspx?Degree=" + this.degree.Text + "&Grad=" + this.gradDate.Text
        + "&Obj=" + this.objective.Text);
            Response.Redirect("Page3.aspx");
            //ps.FormFlattening = true;
Page 3
AcroFields af = ps.AcroFields;
            af.SetField("jobStart1", jobStart1.Text);
            af.SetField("jobEnd1", jobEnd1.Text);
            af.SetField("jobTitle1", jobTitle1.Text);
            af.SetField("coName1", coName1.Text);
            af.SetField("coAdd1", coAdd1.Text);
            af.SetField("Details1", details1.Text);
            Response.Redirect("Default.aspx?jobStart1" + this.jobStart1.Text + "&jobEnd1=" + this.jobEnd1.Text
        + "&jobTitle1=" + this.jobTitle1.Text + "&coName1=" + this.coName1.Text + "&coAdd1=" + this.coAdd1.Text +
        "&Details1=" + this.details1.Text);
            Response.Redirect("Page4.aspx");
Page 4
AcroFields af = ps.AcroFields;
            af.SetField("jobStart2", jobStart2.Text);
            af.SetField("jobEnd2", jobEnd2.Text);
            af.SetField("jobTitle2", jobTitle2.Text);
            af.SetField("coName2", coName2.Text);
            af.SetField("coAdd2", coAdd2.Text);
            af.SetField("Details2", details2.Text);
            Response.Redirect("Default.aspx?jobStart2" + this.jobStart2.Text + "&jobEnd2=" + this.jobEnd2.Text
        + "&jobTitle2=" + this.jobTitle2.Text + "&coName2=" + this.coName2.Text + "&coAdd2=" + this.coAdd2.Text +
        "&Details2=" + this.details2.Text);
            Response.Redirect("Page5.aspx");
Page 5
 AcroFields af = ps.AcroFields;
            af.SetField("jobStart3", jobStart3.Text);
            af.SetField("jobEnd3", jobEnd3.Text);
            af.SetField("jobTitle3", jobTitle3.Text);
            af.SetField("coName3", coName3.Text);
            af.SetField("coAdd3", coAdd3.Text);
            af.SetField("Details3", details3.Text);
            Response.Redirect("Default.aspx?jobStart3" + this.jobStart3.Text + "&jobEnd3=" + this.jobEnd3.Text
        + "&jobTitle3=" + this.jobTitle3.Text + "&coName3=" + this.coName3.Text + "&coAdd3=" + this.coAdd3.Text +
        "&Details3=" + this.details3.Text);
            Response.Redirect("Page6.aspx");
Page 6
 string name = Request.QueryString["Name"];
            string address = Request.QueryString["Address"];
            string phone = Request.QueryString["Phone"];
            string email = Request.QueryString["Email"];
            string city = Request.QueryString["city"];
            string state = Request.QueryString["state"];
            string zip = Request.QueryString["zip"];
            string degree = Request.QueryString["Degree"];
            string gradDate = Request.QueryString["Grad"];
            string objective = Request.QueryString["Obj"];
            string jobStart1 = Request.QueryString["jobStart1"];
            string jobEnd1 = Request.QueryString["jobEnd1"];
            string jobTitle1 = Request.QueryString["jobTitle1"];
            string coName1 = Request.QueryString["coName1"];
            string coAdd1 = Request.QueryString["coAdd1"];
            string details1 = Request.QueryString["Details1"];
            string jobStart2 = Request.QueryString["jobStart2"];
            string jobEnd2 = Request.QueryString["jobEnd2"];
            string jobTitle2 = Request.QueryString["jobTitle2"];
            string coName2 = Request.QueryString["coName2"];
            string coAdd2 = Request.QueryString["coAdd2"];
            string details2 = Request.QueryString["Details2"];
            string jobStart3 = Request.QueryString["jobStart3"];
            string jobEnd3 = Request.QueryString["jobEnd3"];
            string jobTitle3 = Request.QueryString["jobTitle3"];
            string coName3 = Request.QueryString["coName3"];
            string coAdd3 = Request.QueryString["coAdd3"];
            string details3 = Request.QueryString["Details3"];


            AcroFields af = ps.AcroFields;
            af.SetField("Name", name);
            af.SetField("Email", email);
            af.SetField("state", state);
            af.SetField("city", city);
            af.SetField("Address", address);
            af.SetField("Phone", phone);
            af.SetField("zip", zip);
            af.SetField("Degree", degree);
            af.SetField("Grad", gradDate);
            af.SetField("Obj", objective);
            af.SetField("jobStart1", jobStart1);
            af.SetField("jobEnd1", jobEnd1);
            af.SetField("jobTitle1", jobTitle1);
            af.SetField("coName1", coName1);
            af.SetField("coAdd1", coAdd1);
            af.SetField("Details1", details1);
            af.SetField("jobStart2", jobStart2);
            af.SetField("jobEnd2", jobEnd2);
            af.SetField("jobTitle2", jobTitle2);
            af.SetField("coName2", coName2);
            af.SetField("coAdd2", coAdd2);
            af.SetField("Details2", details2);
            af.SetField("jobStart3", jobStart3);
            af.SetField("jobEnd3", jobEnd3);
            af.SetField("jobTitle3", jobTitle3);
            af.SetField("coName3", coName3);
            af.SetField("coAdd3", coAdd3);
            af.SetField("Details3", details3);
            af.SetField("Skills", skills.Text);
            ps.FormFlattening = true;
        }
        Response.End();
Krytix
  • 67
  • 2
  • 8
  • 1
    `i have the information set to pass the values using a query string, but it seems to keep getting lost` Well, clearly you're not passing it properly, and need to show us your implementation of passing it via query string. We can't tell you what you're doing wrong if you don't show us what you're doing. Please edit that information into your question. I suggest you delete your question until you finish editing in, then undelete it after you've finished. That will avoid down votes and close votes. Also realize that you need that information *from the beginning* next time without being asked. – mason Feb 16 '15 at 18:20
  • Please share your code. – Hannes Feb 16 '15 at 18:22
  • @mason my apologies. – Krytix Feb 16 '15 at 19:05
  • I don't understand your code. If you have a PDF document with **a single form**, then why are you filling out **the same form** using six different `AcroFields` instances? I know that your document has six pages, but that is irrelevant in the context of PDF forms. – Bruno Lowagie Feb 16 '15 at 19:51
  • @BrunoLowagie my apologies for my rudimentary coding. Im still very new to iTextSharp, and this is actually my first multi-page project using iTextSharp on a web form. – Krytix Feb 16 '15 at 22:58

1 Answers1

2
 Response.Redirect("Default.aspx?name=" + this.name.Text + "&Email=" + this.email.Text 
        + "&state=" + this.state.Text + "&city=" + this.city.Text + "&Address=" + this.address.Text + 
        "&Phone=" + this.phone.Text + "&zip=" + this.zip.Text);
 Response.Redirect("Page2.aspx"); //this will never be reached because the line before it ends the current execution context

You're redirecting back to Default.aspx, but judging by your question the intention is to go to Page2.aspx. But Response.Redirect("Page2.aspx"); is never reached because the response ends after the first redirect. When you call response redirect (the standard overload) the current request context ends, the client is sent an HTTP redirect. So no further code will be executed.

Instead, replace both of those lines with this one:

Response.Redirect("Page2.aspx?name=" + this.name.Text + "&Email=" + this.email.Text 
            + "&state=" + this.state.Text + "&city=" + this.city.Text + "&Address=" + this.address.Text + 
            "&Phone=" + this.phone.Text + "&zip=" + this.zip.Text);

Then on Page 2, you'll need code in Page_Load to pull the values out of the request query string, and additional code so that when you leave the page you pass the values from page 1 and the new values from page 2. And so on.

You should UriEncode your values before putting them in the query string. And concatenating a bunch of strings is really ugly. Perhaps a format string would be appropriate here.

Also, if I were you, I'd abandon the query string approach entirely. That's really messy, passing them around like that because after several pages, you're going to have a ton of parameters and it's going to be a messy URL and a lot of boilerplate code. And neither of those is good. Instead, you should create a Model to represent all the information you want to gather, then store the model somewhere such as Session to pass it between pages. On the function where you build your PDF, have it accept the model as a parameter and generate the PDF according to the values in your model.

The model is simply a C# class that represents the values your PDF needs. For example, here's a starting point:

public class ApplicantInformationModel
{
    public string Name {get; set;}
    public string Email {get; set;}
    public List<Job> Jobs {get; set;}
    public string PhoneNumber {get; set;}
}

public class Job
{
    public DateTime StartDate {get; set;}
    public DateTime EndDate {get; set;}
    public string Title {get; set;}
    public string Company {get; set;}
}

Your PDF creation function will accept this:

public static Document GeneratePdfForApplicant(ApplicantInformationModel model)
{
    //use iTextSharp to generate and return PDF based on the model
}
Community
  • 1
  • 1
mason
  • 31,774
  • 10
  • 77
  • 121
  • This is working out perfectly. Thank you. the only question i have is, how do i go about calling the GeneratePdfForApplicant when I click a button? Sorry I am very new to this kind of coding – Krytix Feb 16 '15 at 22:57
  • @Krytix You'd call it and retrieve an instance of `Document` that is the PDF containing your filled out data. Then [convert it to a byte array](http://stackoverflow.com/questions/1192384/itextsharp-how-to-convert-document-to-byte), [set the response headers](http://stackoverflow.com/questions/8590543/force-browser-to-download-pdf-document-instead-of-opening-it) to indicate you're sending the client a PDF then [write it to the Response](https://msdn.microsoft.com/en-us/library/system.web.httpresponse.binarywrite(v=vs.110).aspx) and then call `Response.End();`. – mason Feb 16 '15 at 23:37