Use lodop in mvc2.0 to provide a perfect solution for Web Printing

Source: Internet
Author: User

The lodopweb print control is introduced through friend callhot. Since it was developed by Chinese people, I have carefully studied it over the past two days and intend to use it in future projects. I will share my learning achievements with you. If there are any deficiencies, you can point them out.

The specific implementation steps are as follows:

I. Preparations

1. mvc2.0 + jquery1.4.1 development environment.

2. lodop web printing controls, official address: http://mtsoftware.v053.gokao.net/download.html (Note: Chinese development, free software ).

3. stringtemplate, C # Open Source template engine. Address: http://www.stringtemplate.org.

This article mainly provides the implementation scheme of printing steps on the Web. For detailed technical implementation details, please refer to the official API. Lodop and stringtemplate have provided detailed documentation.

 

Ii. Create a print Template Using stringtemplate in mvc2.0

In stringtemplate, St is short. There are related documents on the network to introduce st efficiency is not bad. This document uses st as a report printing template. In actual project development, complicated reports are printed and some work content is allocated to the artist for processing. Developers only need to provide data source interfaces. Using ST can reduce the workload of developers. The division of report development tasks is more detailed. The benefits brought to the project will not be discussed much. The specific implementation is as follows:

1. reference the st core DLL in the mvc2.0 project:

2. Create the st template file, template. St (dedicated st template file ):

You can also think that the st file is a common HTML file. This part is mainly handled by the artist, such as CSS.

3. Create a jsonresult that provides the data source in mvc2.0 controller:

Public jsonresult print () {// construct the print data list <customertest> List = new list <customertest> (); For (INT I = 0; I <100; I ++) {list. add (New customertest {customername = "Candy" + I, customeraddress = "simming district" + I, customerphone = "13148484855" + I}); list. add (New customertest {customername = "Linda" + I, customeraddress = "Huili district" + I, customerphone = "13847487545" + I}); list. add (New customertest {custom Ername = "Ellie" + I, customeraddress = "haichang district" + I, customerphone = "1359984665" + I}) ;}// stringtemplate print the template file, to improve program efficiency, print template files should be cached in actual projects. String serverpath = system. Web. httpcontext. Current. server. mappath ("~ "); String Path = path. combine (serverpath, @ "printtemplate \"); stringtemplategroup group = new stringtemplategroup ("mygroup", path, typeof (templatelexer); stringtemplate ST = group. getinstanceof ("template"); ST. setattribute ("customer", list); // html-related hypertext content is provided for printing. Stringbuilder sb = new stringbuilder (); sb. append (@ "<HTML xmlns = 'HTTP: // www.w3.org/5o/xhtml'lang = 'zh-cn'>"); sb. append ("

Here, customertest is a custom data class and has provided detailed comments. It is not hard to understand.

4. Add the JS Code to the mvc2.0 view HTML head:

<Asp: Content ID = "content3" contentplaceholderid = "head" runat = "server"> <script language = "JavaScript" src = "checkactivx. JS "> </SCRIPT> <Object ID =" lodop "classid =" CLSID: 2105c259-1e0c-4534-8141-a753534cb4ca "width =" 0 "Height =" 0 "> </Object> <SCRIPT type =" text/JavaScript "> function prn1_preview (data) {lodop. print_init (" lo_ lodop _ "); // report title lodop. add_print_htm (50,300,330,300, "<font color = 'Black' size = '6'> customer list </font> <font color = 'blue' size = '2'> (tabulation person: James Zhang) </font> "); // print the report content. Lodop. add_print_table (100,150,760,900, data); lodop. preview () ;}; $ (function () {$ ("# btnprint "). click (function () {var url = '<% = URL. action ("print", "home") %> '; $. ajax ({type: "Post", URL: URL, cache: false, datatype: 'json', success: function (result) {If (result. success) {prn1_preview (result. data) ;}}}) ;}) ;}</SCRIPT> </ASP: content>


Iii. Running

Print and preview the last page:

Horizontal printer printing:

 Iv. Notes

The web printing solution provided in this article requires readers to have a certain understanding of mvc2.0, jquery, and stringtemplate. In addition, this example only implements web printing in IE. If you need to support web printing in Firefox or other browsers, contact the author of lodop.

 

I hope this article will help you. If you have any shortcomings, thank you!

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.