<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Code Cruising</title>
	<atom:link href="http://code-cruising.net/feed" rel="self" type="application/rss+xml" />
	<link>http://code-cruising.net</link>
	<description>Miscellaneous software engineering topics.</description>
	<lastBuildDate>Tue, 31 May 2011 15:56:57 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Garbage Collection or Garbage Accumelation?</title>
		<link>http://code-cruising.net/garbage-collection-or-garbage-accumelation</link>
		<comments>http://code-cruising.net/garbage-collection-or-garbage-accumelation#comments</comments>
		<pubDate>Mon, 07 Mar 2011 02:22:01 +0000</pubDate>
		<dc:creator>Sarmad Khalid</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://code-cruising.net/?p=89</guid>
		<description><![CDATA[It seems the trend nowadays is to use Java style garbage collection for all sorts of memory allocations, which makes life easier for the programmer, but does that mean it&#8217;s the right approach?

Let&#8217;s first compare garbage collection to a real world example: garbage collection! In real world you don&#8217;t go to the garbage dump every [...]]]></description>
			<content:encoded><![CDATA[<p>It seems the trend nowadays is to use Java style garbage collection for all sorts of memory allocations, which makes life easier for the programmer, but does that mean it&#8217;s the right approach?</p>
<p><span id="more-89"></span></p>
<p>Let&#8217;s first compare garbage collection to a real world example: garbage collection! In real world you don&#8217;t go to the garbage dump every time you are done with a piece of paper, instead you keep them in a small bin under your desk, then when the bin is full you dump it all at once; obviously more efficient than cleaning your garbage after every operation. But is this really how garbage collection works in modern programming languages? Almost, except that they keep the garbage on the desk, mixed with needed papers! As long as there is empty space on the desk we dont need to perform garbage collection. This could be more efficient if you are very busy and you know your task will be quite short after which you can dump the whole thing, like in a web server, but not if you are a regular user because, first, your desk will look messy 99% of the time, second, you will need more time to start any task since you will need to perform some cleaning at the beginning of each. Maybe this is why Windows always seems to be  low in memory and usually takes a considerable amount of time to launch an app even if the app looks so small!</p>
<p>Microsoft came up with the slogan: unused memory is wasted memory, and unfortunately a lot of people have fallen into this. Really, how does filling your memory with garbage make it not wasted?! When a user buys an additional memory we should assume his intention is to fill it with data, not to give it to us to use as a garbage bin.</p>
<p>So what&#8217;s the solution? The solution is obviously to give the programmer more control, and more responsibility. A balance between the completely manual allocation process of C and the completely automated process of garbage collection can save the user a lot of wasted memory and a lot of unneeded memory defragmentation. Yes, it will be a bit more work on the programmer, but I think it will be a good balance between code efficiency and implementation efficiency.</p>
]]></content:encoded>
			<wfw:commentRss>http://code-cruising.net/garbage-collection-or-garbage-accumelation/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Underscore vs Camelcase Naming Convention</title>
		<link>http://code-cruising.net/underscore-vs-camelcase-naming-convention</link>
		<comments>http://code-cruising.net/underscore-vs-camelcase-naming-convention#comments</comments>
		<pubDate>Wed, 31 Mar 2010 09:10:24 +0000</pubDate>
		<dc:creator>Sarmad Khalid</dc:creator>
				<category><![CDATA[CodeProject]]></category>
		<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[General Programming]]></category>
		<category><![CDATA[Naming Convention]]></category>

		<guid isPermaLink="false">http://sarmad-khalid.info/blog/?p=74</guid>
		<description><![CDATA[A comparison between the two most common naming conventions: underscore and camelcase conventions.]]></description>
			<content:encoded><![CDATA[<p>There used to be many naming conventions back in the days, but lately it seems that two conventions are emerging as the two most popular among programmers. Underscore naming convention and camel case naming convention. Someone might argue that there are other popular conventions, but for now let&#8217;s compare these two and ask programmers out there which one they prefer. In the end a programmer might be bound to whatever is the standard for the language he is using, but this doesn&#8217;t prevent us from doing an out-of-context comparison.</p>
<p><span id="more-74"></span></p>
<p>Underscore naming convention uses all lower case words, and separate them with underscores. The common is to use this for variable and function names, and to use Pascal convention for classes, structures, enumerations, and interfaces. Underscores were used by C programmers, then C++ adopted the same convention for its keywords and Standard Templates Library, and later on Boost used the same convention, making this convention quite popular among C++ programmers, but not dominant as other programmers used various other conventions including Pascal and camel case conventions. Underscore convention is also used by PHP&#8217;s standard libraries and is pretty much the standard for PHP developers. Ruby also uses underscores, or so I heard.</p>
<p>On the other hand, camel case naming convention is mostly known to be the standard for Java and its little sister JavaScript, although you can find it in use in various other places. Camel case simply starts each word with an uppercase letter, except for the first word which remains in lower case. The words are not separated by any underscore, obviously. Like in the underscore convention, this applies mainly to variables and functions, while type names (classes, structures, interfaces, enumerations) remain in Pascal convention. C# uses a variation of this convention by limiting the camelcase to function parameters and local variables.</p>
<p>Of course each of the two conventions has its own strengths and weaknesses. The following list summarizes the strengths and weaknesses of the two:</p>
<ul>
<li>Underscores make phrases easier to read. Compare the underscore_naming_convention to the camelCaseNamingConvention.</li>
<li>Camel case makes <span style="text-decoration: underline;">paragraphs</span> easier to read! Of course, when you read code what&#8217;s important for you is to understand the overall operation or algorithm, not the words in a phrase. Compare the following:<br />
my_first_variable=my_second_variable-my_third_variable;<br />
to this:<br />
myFirstVariable=mySecondVariable-myThirdVariable;<br />
Obviously in this case the camel case is easier to read. From the first glance you realize it&#8217;s a subtraction between two variables, while in the case of underscores you need to concentrate before you realize where the minus sign is. You may even mistake it for being one long variable instead of two. This is a strong disadvantage for the underscore convention, however, coloring can solve this problem if the editor gives identifiers a different color from operators, but that&#8217;s dependent on the editor.</li>
<li>Camel case is the negative of English. In english, the first word in a sentence starts with an upper case and the rest remains in lower case. This is probably true for many other human languages. Camel case is the exact opposite. This is mind itching indeed.</li>
<li>Underscores are harder to type. Even with the availability of intellisense, you still need to type the underscore in many cases.</li>
<li>Camel case is not consistent because with constants (which is all in upper case) you still need the underscores. On the other hand, the underscore convention CAN be consistent (if you decide to use underscores even for type names, which is doable, i.e. My_Type instead of MyType).</li>
</ul>
<p>Any more pros and cons you can think of? Please let us know.</p>
Note: There is a poll embedded within this post, please visit the site to participate in this post's poll.
]]></content:encoded>
			<wfw:commentRss>http://code-cruising.net/underscore-vs-camelcase-naming-convention/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Web Security Overdone?</title>
		<link>http://code-cruising.net/web-security-overdone</link>
		<comments>http://code-cruising.net/web-security-overdone#comments</comments>
		<pubDate>Thu, 18 Mar 2010 19:19:16 +0000</pubDate>
		<dc:creator>Sarmad Khalid</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://sarmad-khalid.info/blog/?p=53</guid>
		<description><![CDATA[Everyone is concerned about the security of their systems, and all sorts of protection softwares are installed to make sure no code is allowed to run on their machine outside the boundaries of the web browser, but is this the right approach? Most people would say yes, but consider the following real-life scenario:

Few weeks ago I [...]]]></description>
			<content:encoded><![CDATA[<p>Everyone is concerned about the security of their systems, and all sorts of protection softwares are installed to make sure no code is allowed to run on their machine outside the boundaries of the web browser, but is this the right approach? Most people would say yes, but consider the following real-life scenario:</p>
<p><span id="more-53"></span></p>
<p>Few weeks ago I was working on a medical application for managing diabetes clinics and their patients. The application is a big system that manages everything related to a diabetes clinic, but one part of it is intended to track patients&#8217; statuses. The idea is to allow patients to use their personal glucose meters to submit their blood glucose data from home. The system will then monitor the records of all patients and when a specific patient goes into the red zone the medical staff is informed immediately. Sounds like a great idea, but how do we allow the patient to upload their glucose data? The logical answer is to let them connect their devices to the serial port and have our application read that serial port. For a desktop application this is a straight forward task, but we are living in the web age where everything is going online and people are less willing to download applications locally. I was using .NET and I thought of using client-side managed code for this task, which will work if the user is an administrator. Note that by administrator I don&#8217;t mean a Windows user account with Administrator priveledges; I mean a person who has the knowledge of a system administrator in order to go into the .NET security configurations and give access rights to my code. Without the correct access rights given the code will simply error out, the user will not even know what was wrong, not to mention giving him/her the ability to allow or deny the code from running, which is what logically should happen. There is no way for us to ask the user for permission, the only solution is to have the user give the required access rights (which 99% of users don&#8217;t know how to do), or to have the user install something on their machine. This was using .NET, and Microsoft is always being accused of having loose security models, so I guess I can bet that other vendors have similar tight security settings.</p>
<p>Security enthusiasts will probably flame me for complaining and tell me there are ways around this, which is true, but should we design our needs around an existing security model? Shouldn&#8217;t it be that the security model is designed around our needs? So how should the system&#8217;s security behave then? Maybe ask the user for permission at the time a permission is needed? I don&#8217;t really know how the ultimate solution should look like; all I know is that in order to protect the user&#8217;s data I was prevented from protecting his life, literally!</p>
]]></content:encoded>
			<wfw:commentRss>http://code-cruising.net/web-security-overdone/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Rendering PDF views in ASP MVC using iTextSharp</title>
		<link>http://code-cruising.net/rendering-pdf-views-in-asp-mvc-using-itextsharp</link>
		<comments>http://code-cruising.net/rendering-pdf-views-in-asp-mvc-using-itextsharp#comments</comments>
		<pubDate>Sat, 06 Mar 2010 03:26:17 +0000</pubDate>
		<dc:creator>Sarmad Khalid</dc:creator>
				<category><![CDATA[CodeProject]]></category>
		<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[.NET]]></category>
		<category><![CDATA[ASP.NET]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[iTextSharp]]></category>
		<category><![CDATA[MVC]]></category>
		<category><![CDATA[PDF]]></category>

		<guid isPermaLink="false">http://sarmad-khalid.info/blog/?p=13</guid>
		<description><![CDATA[This post talks about how you can add a new type of views to your ASP.NET MVC application to support PDF files. The technique allows you to define your views inside the Views folder using a markup language in a way similar to the way the regular views are constructed and displayed. A downloadable sample is provided as well.]]></description>
			<content:encoded><![CDATA[<p>Generating PDF files can be done by calling iTextSharp classes and methods directly, but consistency with the rest of MVC is preferable. In this post I&#8217;m going to explain how you can use iTextSharp to create PDF views the same way HTML views are created. What we need is to write our PDF files under the Views directory using a markup language. iTextSharp supports a special XML format that can be used in our views instead of HTML. One thing to note about aspx is that your tags don&#8217;t have to be HTML tags. You can type anything you want; of course Visual Studio will only recognize HTML but the ASP engine will spit that data out to the browser anyway. So, we are going to use regular views but we are going to write iTextSharp XML tags in them instead of HTML. Then we&#8217;ll need a way to intercept the result and convert it into PDF before it&#8217;s sent to the browser.</p>
<p><span id="more-13"></span></p>
<p>We are going to expand the Controller class with functionalities needed for handling PDF views. Our new class will be called MyController and it will inherit from MVC&#8217;s Controller. Any controller that needs to support PDF views will need to inherit from MyController instead of Controller. As you know, the Controller class has a method called &#8216;View&#8217; which performs all that&#8217;s needed to be done for an HTML view to be rendered to the browser. Following a similar approach, we are going to create a new method called &#8216;ViewPdf&#8217; that will send out a PDF file instead of an HTML file. But before we write that method, we&#8217;ll need to be able to render our views into a string instead of the browser. To do this we need to create a fake HttpResponse and give it a memory stream to write to. Then we&#8217;ll need to create a fake HttpContext to use that fake response. Then we can call ExecuteResult on our ActionResult object giving it the fake context. This will cause the view to be rendered to our memory stream. These steps will be included in a new method called RenderActionResultToString. This method will later on be used by ViewPdf to generate the PDF data.</p>
<blockquote><p><span style="color: #0000ff;">protected </span><span style="color: #0000ff;">string</span> RenderActionResultToString(<span style="color: #33cccc;"><span style="color: #008080;">ActionResult</span> </span>result)<br />
{<br />
<span style="color: #008000;">    // Create memory writer.<br />
</span><span style="color: #0000ff;">    var</span> sb = <span style="color: #0000ff;">new</span> <span style="color: #008080;">StringBuilder</span>();<br />
<span style="color: #0000ff;">    var</span> memWriter = <span style="color: #0000ff;">new</span> <span style="color: #008080;">StringWriter</span>(sb);</p>
<p><span style="color: #008000;">    // Create fake http context to render the view.<br />
</span><span style="color: #0000ff;">    var</span> fakeResponse = <span style="color: #0000ff;">new</span> <span style="color: #008080;">HttpResponse</span>(memWriter);<br />
<span style="color: #0000ff;">    var</span> fakeContext = <span style="color: #0000ff;">new</span> <span style="color: #008080;">HttpContext</span>(System.Web.<span style="color: #008080;">HttpContext</span>.Current.Request,<br />
        fakeResponse);<br />
<span style="color: #0000ff;">    var</span> fakeControllerContext = <span style="color: #0000ff;">new</span> <span style="color: #008080;">ControllerContext</span>(<br />
        <span style="color: #0000ff;">new</span> <span style="color: #008080;">HttpContextWrapper</span>(fakeContext),<br />
        <span style="color: #0000ff;">this</span>.ControllerContext.RouteData,<br />
        <span style="color: #0000ff;">this</span>.ControllerContext.Controller);<br />
<span style="color: #0000ff;">    var</span> oldContext = System.Web.<span style="color: #008080;">HttpContext</span>.Current;<br />
    System.Web.<span style="color: #008080;">HttpContext</span>.Current = fakeContext;</p>
<p><span style="color: #008000;">    // Render the view.<br />
</span>    result.ExecuteResult(fakeControllerContext);</p>
<p><span style="color: #008000;">    // Restore old context.<br />
</span>    System.Web.<span style="color: #008080;">HttpContext</span>.Current = oldContext;</p>
<p><span style="color: #008000;">    // Flush memory and return output.<br />
</span>    memWriter.Flush();<br />
<span style="color: #0000ff;">    return</span> sb.ToString();<br />
}</p></blockquote>
<p>Now that we can intercept our rendered views as strings, we can now convert them into PDF files. This will be done inside the ViewPdf method, which returns an ActionResult object, so it can be used in the controller the same way the View method is used. ViewPdf creates a memory stream to which the generated PDF will be written, then it gets the buffer from that memory stream and uses it to initialize the BinaryContentResult object that will be returned to the caller.</p>
<blockquote><p>        <span style="color: #0000ff;">protected</span> <span style="color: #0082a3;">ActionResult</span> ViewPdf(<span style="color: #0000ff;">object</span> model)<br />
        {<br />
<span style="color: #008000;">            // Create the iTextSharp document.<br />
</span>            <span style="color: #008080;">Document</span> doc = <span style="color: #0000ff;">new</span> <span style="color: #008080;">Document</span>();<br />
<span style="color: #008000;">            // Set the document to write to memory.<br />
</span>            <span style="color: #008080;">MemoryStream</span> memStream = <span style="color: #0000ff;">new</span> <span style="color: #008080;">MemoryStream</span>();<br />
            <span style="color: #008080;">PdfWriter</span> writer = <span style="color: #008080;">PdfWriter</span>.GetInstance(doc, memStream);<br />
            writer.CloseStream = <span style="color: #0000ff;">false</span>;<br />
            doc.Open();</p>
<p><span style="color: #008000;">            // Render the view xml to a string, then parse that string into an XML dom.<br />
</span>            <span style="color: #0000ff;">string</span> xmltext = this.RenderActionResultToString(this.View(model));<br />
            <span style="color: #008080;">XmlDocument</span> xmldoc = <span style="color: #0000ff;">new</span> <span style="color: #008080;">XmlDocument</span>();<br />
            xmldoc.InnerXml = xmltext.Trim();</p>
<p><span style="color: #008000;">            // Parse the XML into the iTextSharp document.<br />
</span>            <span style="color: #008080;">ITextHandler</span> textHandler = <span style="color: #0000ff;">new</span> <span style="color: #008080;">ITextHandler</span>(doc);<br />
            textHandler.Parse(xmldoc);</p>
<p><span style="color: #008000;">            // Close and get the resulted binary data.<br />
</span>            doc.Close();<br />
            <span style="color: #0000ff;">byte</span>[] buf = <span style="color: #0000ff;">new</span> <span style="color: #0000ff;">byte</span>[memStream.Position];<br />
            memStream.Position = 0;<br />
            memStream.Read(buf, 0, buf.Length);</p>
<p><span style="color: #008000;">            // Send the binary data to the browser.<br />
</span>            <span style="color: #0000ff;">return </span><span style="color: #0000ff;">new</span> <span style="color: #008080;">BinaryContentResult</span>(buf,<span style="color: #993300;"> &#8220;application/pdf&#8221;</span>);<br />
        }</p></blockquote>
<p>That&#8217;s all that&#8217;s needed in our MyController class, but what&#8217;s the BinaryContentResult class used inside the ViewPdf method? It&#8217;s a class inherited from ActionResult that is used to return arbitrary content, much like the ContentResult class, but returns binary data instead.</p>
<blockquote><p>    <span style="color: #0000ff;">public </span><span style="color: #0000ff;">class</span> <span style="color: #008080;">BinaryContentResult </span>: <span style="color: #008080;">ActionResult</span><br />
    {<br />
        <span style="color: #0000ff;">private</span> <span style="color: #0000ff;">string</span> ContentType;<br />
        <span style="color: #0000ff;">private</span> <span style="color: #0000ff;">byte</span>[] ContentBytes;</p>
<p>        <span style="color: #0000ff;">public</span> BinaryContentResult(<span style="color: #0000ff;">byte</span>[] contentBytes, <span style="color: #0000ff;">string</span> contentType)<br />
        {<br />
            <span style="color: #0000ff;">this</span>.ContentBytes = contentBytes;<br />
            <span style="color: #0000ff;">this</span>.ContentType = contentType;<br />
        }</p>
<p>        <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">override</span> <span style="color: #0000ff;">void</span> ExecuteResult(<span style="color: #008080;">ControllerContext</span> context)<br />
        {<br />
            <span style="color: #0000ff;">var</span> response = context.HttpContext.Response;<br />
            response.Clear();<br />
            response.Cache.SetCacheability(<span style="color: #008080;">HttpCacheability</span>.NoCache);<br />
            response.ContentType = <span style="color: #0000ff;">this</span>.ContentType;</p>
<p>            <span style="color: #0000ff;">var</span> stream = <span style="color: #0000ff;">new</span> <span style="color: #008080;">MemoryStream</span>(<span style="color: #0000ff;">this</span>.ContentBytes);<br />
            stream.WriteTo(response.OutputStream);<br />
            stream.Dispose();<br />
        }<br />
    }</p></blockquote>
<p>We are now ready to start writing PDF views. The following example shows a simple PDF view that renders the list of orders for a specific customer.</p>
<blockquote><p>&lt;%@ Page Language=&#8221;C#&#8221; Inherits=&#8221;System.Web.Mvc.ViewPage&lt;Sample1.Models.Customer&gt;&#8221; %&gt;<br />
&lt;%@ Import Namespace=&#8221;Sample1.Models&#8221; %&gt;</p>
<p>&lt;?xml version=&#8221;1.0&#8243; encoding=&#8221;UTF-8&#8243; ?&gt;<br />
&lt;itext creationdate=&#8221;2/4/2003 5:49:07 PM&#8221; producer=&#8221;iTextSharpXML&#8221;&gt;<br />
 &lt;paragraph leading=&#8221;18.0&#8243; font=&#8221;unknown&#8221; size=&#8221;16.0&#8243; align=&#8221;Default&#8221;&gt;<br />
     &lt;chunk&gt;Orders in PDF&lt;/chunk&gt;<br />
 &lt;/paragraph&gt;<br />
 &lt;paragraph leading=&#8221;18.0&#8243; font=&#8221;unknown&#8221; size=&#8221;10.0&#8243; align=&#8221;Default&#8221;&gt;<br />
  &lt;chunk&gt;Customer Name: &lt;%= this.Model.Name %&gt;&lt;/chunk&gt;&lt;newline /&gt;<br />
  &lt;chunk&gt;Address: &lt;%= this.Model.Address %&gt;&lt;/chunk&gt;&lt;newline /&gt;<br />
 &lt;/paragraph&gt;<br />
 &lt;paragraph leading=&#8221;18.0&#8243; font=&#8221;unknown&#8221; size=&#8221;10.0&#8243; align=&#8221;Default&#8221;&gt;<br />
 &lt;chunk font=&#8221;unknown&#8221; size=&#8221;12.0&#8243;&gt;Orders:&lt;/chunk&gt;&lt;newline /&gt;<br />
 &lt;% foreach (Order o in this.Model.Order)<br />
       { %&gt;<br />
        &lt;chunk font=&#8221;unknown&#8221; size=&#8221;10.0&#8243;&gt;&lt;%= o.OrderId %&gt;, &lt;%= o.Description %&gt;, &lt;%= o.Amount %&gt;, &lt;%= o.Date %&gt;&lt;/chunk&gt;&lt;newline /&gt;<br />
 &lt;% } %&gt;<br />
 &lt;/paragraph&gt;<br />
&lt;/itext&gt;</p></blockquote>
<p>We&#8217;ll name the view OrdersInPdf and our controller will then look like this:</p>
<blockquote><p>    <span style="color: #0000ff;">public </span><span style="color: #0000ff;">class</span> <span style="color: #008080;">HomeController </span>: <span style="color: #008080;">MyController</span><br />
    {<br />
        <span style="color: #0000ff;">private</span> <span style="color: #008080;">Sample1Entities</span> entities = <span style="color: #0000ff;">new</span> <span style="color: #008080;">Sample1Entities</span>();</p>
<p>        <span style="color: #0000ff;">public</span> <span style="color: #008080;">ActionResult</span> OrdersInHtml()<br />
        {<br />
            <span style="color: #008080;">Customer</span> c = (<br />
                <span style="color: #0000ff;">from</span> cust <span style="color: #0000ff;">in</span> entities.Customer<br />
                <span style="color: #0000ff;">where</span> cust.CustomerId == 1<br />
                <span style="color: #0000ff;">select</span> cust).First();<br />
            c.Order.Load();<br />
            <span style="color: #0000ff;">return</span> View(c);<br />
        }</p>
<p>        <span style="color: #0000ff;">public</span> <span style="color: #008080;">ActionResult</span> OrdersInPdf()<br />
        {<br />
            <span style="color: #008080;">Customer</span> c = (<br />
                <span style="color: #0000ff;">from</span> cust <span style="color: #0000ff;">in</span> entities.Customer<br />
                <span style="color: #0000ff;">where</span> cust.CustomerId == 1<br />
                <span style="color: #0000ff;">select</span> cust).First();<br />
            c.Order.Load();<br />
<span style="color: #008000;">            // To render a PDF instead of an HTML, all we need to do is call ViewPdf<br />
            // instead of View. This requires the controller to be inherited from<br />
            // MyController instead of MVC&#8217;s Controller.<br />
</span>            <span style="color: #0000ff;">return</span> ViewPdf(c);<br />
        }<br />
    }</p></blockquote>
<p>As the code implies, OrdersInHtml will render an HTML view which is the default behaviour of MVC, while OrdersInPdf will render a PDF instead. Notice that the only difference between the two is the return statement, OrdersInPdf calls ViewPdf instead of View. The programmer needs to make sure when he calls ViewPdf that the view contains iTextSharp XML data rather than HTML data. The other thing to notice in the controller is the first line which shows that the controller inherits from MyController instead of Controller.</p>
<p>iTextSharp also has the ability to convert HTML into PDF so we can modify the ViewPdf (or create a new method) to make it parse HTML instead of XML. However, it&#8217;s usually hard to get the expected formatting when you convert from HTML.</p>
<p><a href="http://code-cruising.net/wp-content/uploads/2010/03/Sample1.zip">Download Sample</a></p>
<p><strong>License<br />
</strong>This article and the included code is licensed under <a href="http://code-cruising.net/?page_id=66">The Code Project Open License (CPOL)</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://code-cruising.net/rendering-pdf-views-in-asp-mvc-using-itextsharp/feed</wfw:commentRss>
		<slash:comments>11</slash:comments>
		</item>
	</channel>
</rss>

