Post Json to Asp.net Page and Create PDF

I was in a pickle not to long ago and i wanted to simply take my html table and create a pdf out of it. In this first tutorial ill show you how to take the JSON and posted to a web page. Then from there you would be able to read that JSON using  a nifty library called JSON.net .  So without further ado lets get the show started. First things first  create your JSON perhaps your JSON might look something like this.

{"w_vendor":["914115361","914115361","914115361","914115361"]}

Well from here we need to figuered out how to post this to an asp.net page that would render the PDF for us. I thought and thought about this and decided well a web service would just not cut it so lets post the data over to another page using good old fashion Javacript.
Lets see what that might look like.

 

function GetTableValuesAndPost()
    {
        try {

            var tableValues =  JSON.stringify({ w_vendor: w_vendor });

			//create a form.
            var CreateForm = document.createElement('form');
		    //create attributes on the 	form.
		    CreateForm.setAttribute('method', 'post');
            CreateForm.setAttribute('target', '_blank');
            CreateForm.setAttribute('name', 'pdfgetter');
            CreateForm.setAttribute('id', 'pdfgetter');
            // set the posting action to the page im going to post to 
			// in this case i called the page PDFcreator.aspx
			CreateForm.action = "./PDFcreator.aspx";

			//create an input element and assign its value to JSON.
			var Fld2 = document.createElement('input');
            Fld2.setAttribute('type', 'hidden');
            Fld2.setAttribute('name', 'pdfvalue');
            Fld2.setAttribute('value',(tableValues));
            CreateForm.appendChild(Fld2);
            document.body.appendChild(CreateForm);
            CreateForm.submit();
        }

        catch (err) {
            alert(err);
        }

    }

The code is documented is pretty straight forward we are creating a form element assigning some form attributes like posting action and name and then submitting the form with the JSON as the value of the input element in the form.

After thats is done we need to intercept the data in the ASP.NET code behind page.  Lets tackle that now.

First things first make you are download JSON.net once you download it add a reference to the Newtonsoft.json.dll.

so far so good , now for the actual c#. Make sure you create another page in  your site and in the page load event we are going to apply the

following code.

 

using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Newtonsoft.Json;

public partial class finutils_PDFcreator : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

        NameValueCollection nvc = Request.Form;
        Response.Write(Request["pdfValue"]);

        var pdfValue = Request["pdfValue"];
        var Invoice = JsonConvert.DeserializeObject<Data>(pdfValue);
       // var invoice = JsonConvert.DeserializeObject<Data>(pdfValue);           

      }
}
class Data
{
    [JsonProperty(PropertyName = "w_vendor")]
    public List<string> vendor { get; set; }

}

Lets walk through this quickly. WE added reference to the JSON.net library. Then we made a DATA class that we assign a property the same as the JSON property. JSON.net then parses that into the data object for you. It really does all the heavy lifting for you.  Its quite remarkable.

That’s all  for now next post i will add the looping of the data class and then actually creating the pdf right in front of your eyes.

 

Leave a Reply

Your email address will not be published.