Asp.net MVC learning diary 3 (output in PDF format)

Source: Internet
Author: User

1. First you need to download an itextsharp. dll http://somewebguy.wordpress.com/2009/05/08/itextsharp-simplify-your-html-to-pdf-creation/

2. Find an htmltow.builder. CS file in the URL and introduce it to your project.

3. Create a pdfresult class

Public class pdfresult: actionresult
{
Public override void executeresult (controllercontext context)
{

Htmltow.builder builder = new htmltow.builder (pagesize. letter );
Htmlpdfpage page1 = builder. addpage ();

Page1.appendhtml ("Page1.appendhtml ("<p> Andrew Cycler <br> ");
Page1.appendhtml ("4254 some street <br> ");
Page1.appendhtml ("Los Angeles, CA </P> ");
Page1.appendhtml ("<Table> <tr> <TD> <B> product </B> </TD> <B> price </B> </TD> </tr> ");
Page1.appendhtml ("<tr> <TD> ASP. net mvc cookbook </TD> <TD> $40.00 </TD> </tr> </table> ");

Byte [] buffer = new byte [4096];
Byte [] file = builder. renderpdf ();

Httpresponsebase response = context. httpcontext. response;
Response. contenttype = "application/pdf ";
Memorystream multicast stream = new memorystream (File );
While (true)
{
Int READ = mongostream. Read (buffer, 0, buffer. Length );
If (read = 0)
Break;
Response. outputstream. Write (buffer, 0, read );
}
Response. End ();
}
}

4. Implement an action (getpdf) in homecontroller)

Public pdfresult getpdf ()
{
Return new pdfresult ();
}

5. In the index view, add a connection-oriented getpdf

Click <% = html. actionlink ("here", "getpdf", "home") %> to get your order in pdf format!

 

OK. Make sure you are happy.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.