{"id":248,"date":"2013-07-20T02:13:50","date_gmt":"2013-07-20T02:13:50","guid":{"rendered":"http:\/\/miguelontheweb.com\/wordpress\/?p=248"},"modified":"2013-07-20T02:14:09","modified_gmt":"2013-07-20T02:14:09","slug":"post-json-asp-net-page-create-pdf","status":"publish","type":"post","link":"http:\/\/miguelontheweb.com\/wordpress\/?p=248","title":{"rendered":"Post Json to Asp.net Page and Create PDF"},"content":{"rendered":"<p>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 \u00a0a nifty library called JSON.net . \u00a0<!--more-->So\u00a0without further ado lets get the show started. First things first \u00a0create your JSON perhaps your JSON might look something like this.<\/p>\n<pre class=\"brush: javascript; gutter: true\">{&quot;w_vendor&quot;:[&quot;914115361&quot;,&quot;914115361&quot;,&quot;914115361&quot;,&quot;914115361&quot;]}<\/pre>\n<p>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.<br \/>\nLets see what that might look like.<\/p>\n<p>&nbsp;<\/p>\n<pre class=\"brush: javascript; gutter: true\">function GetTableValuesAndPost()\n    {\n        try {\n\n            var tableValues =  JSON.stringify({ w_vendor: w_vendor });\n\n\t\t\t\/\/create a form.\n            var CreateForm = document.createElement(&#039;form&#039;);\n\t\t    \/\/create attributes on the \tform.\n\t\t    CreateForm.setAttribute(&#039;method&#039;, &#039;post&#039;);\n            CreateForm.setAttribute(&#039;target&#039;, &#039;_blank&#039;);\n            CreateForm.setAttribute(&#039;name&#039;, &#039;pdfgetter&#039;);\n            CreateForm.setAttribute(&#039;id&#039;, &#039;pdfgetter&#039;);\n            \/\/ set the posting action to the page im going to post to \n\t\t\t\/\/ in this case i called the page PDFcreator.aspx\n\t\t\tCreateForm.action = &quot;.\/PDFcreator.aspx&quot;;\n\n\t\t\t\/\/create an input element and assign its value to JSON.\n\t\t\tvar Fld2 = document.createElement(&#039;input&#039;);\n            Fld2.setAttribute(&#039;type&#039;, &#039;hidden&#039;);\n            Fld2.setAttribute(&#039;name&#039;, &#039;pdfvalue&#039;);\n            Fld2.setAttribute(&#039;value&#039;,(tableValues));\n            CreateForm.appendChild(Fld2);\n            document.body.appendChild(CreateForm);\n            CreateForm.submit();\n        }\n\n        catch (err) {\n            alert(err);\n        }\n\n    }<\/pre>\n<p>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.<\/p>\n<p>After thats is done we need to intercept the data in the ASP.NET code behind page. \u00a0Lets tackle that now.<\/p>\n<p>First things first make you are download J<a title=\"Here\" href=\"http:\/\/james.newtonking.com\/projects\/json-net.aspx\" target=\"_blank\">SON.net<\/a>\u00a0once you download it add a reference to the Newtonsoft.json.dll.<\/p>\n<p>so far so good , now for the actual c#. Make sure you create another page in \u00a0your site and in the page load event we are going to apply the<\/p>\n<p>following code.<\/p>\n<p>&nbsp;<\/p>\n<pre class=\"brush: csharp; gutter: true\">using System;\nusing System.Collections.Generic;\nusing System.Collections.Specialized;\nusing System.Data;\nusing System.Linq;\nusing System.Web;\nusing System.Web.UI;\nusing System.Web.UI.WebControls;\nusing Newtonsoft.Json;\n\npublic partial class finutils_PDFcreator : System.Web.UI.Page\n{\n    protected void Page_Load(object sender, EventArgs e)\n    {\n\n        NameValueCollection nvc = Request.Form;\n        Response.Write(Request[&quot;pdfValue&quot;]);\n\n        var pdfValue = Request[&quot;pdfValue&quot;];\n        var Invoice = JsonConvert.DeserializeObject&lt;Data&gt;(pdfValue);\n       \/\/ var invoice = JsonConvert.DeserializeObject&lt;Data&gt;(pdfValue);           \n\n      }\n}\nclass Data\n{\n    [JsonProperty(PropertyName = &quot;w_vendor&quot;)]\n    public List&lt;string&gt; vendor { get; set; }\n\n}<\/pre>\n<p>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. \u00a0Its quite remarkable.<\/p>\n<p>That&#8217;s all \u00a0for now next post i will add the looping of the data class and then actually creating the pdf right in front of your eyes.<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>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 &hellip; <\/p>\n<p class=\"link-more\"><a href=\"http:\/\/miguelontheweb.com\/wordpress\/?p=248\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Post Json to Asp.net Page and Create PDF&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[],"class_list":["post-248","post","type-post","status-publish","format-standard","hentry","category-vb-net"],"_links":{"self":[{"href":"http:\/\/miguelontheweb.com\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/248","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/miguelontheweb.com\/wordpress\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/miguelontheweb.com\/wordpress\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/miguelontheweb.com\/wordpress\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/miguelontheweb.com\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=248"}],"version-history":[{"count":6,"href":"http:\/\/miguelontheweb.com\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/248\/revisions"}],"predecessor-version":[{"id":254,"href":"http:\/\/miguelontheweb.com\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/248\/revisions\/254"}],"wp:attachment":[{"href":"http:\/\/miguelontheweb.com\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=248"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/miguelontheweb.com\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=248"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/miguelontheweb.com\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=248"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}