With the development and popularization of computer technology, more and more organizations or departments have basically achieved paperless management information, the computer processing result should also be output to the paper in the form of a report through the printer, such as printing the invoice for the company's product sales, enterprise qualification certificate, etc. In the B/S architecture, the general printing method is to use "print" in the browser file menu ". This method is simple to print, but the printed pages will show the headers, footers, page numbers, and backgrounds that you do not want to appear, as well as the poor layout, moreover, accurate printing is not possible. Based on ASP. NET technology, this article discusses how to use vs 2008 to print and print webpage content in B/S architecture.
1. Use vs 2008 to create a Microsoft SQL Server database
1. Open vs 2008 and create a web form project.
2. Select "server resource manager" from the "View" menu, or press CTRL + ALT + S to open "server resource manager", as shown in figure 1.
3. in the left pane of the window, right-click "Data Connection" and choose "create SQL Server database" from the floating menu. A new window is displayed, in this window, select "server name", for example, "47012caad3a04ce/sqlexpress" (if the SQL Server Browser in "service" is not enabled, the server name cannot be found ), then fill in the "New Database Name", such as "library ". Click OK with the left mouse button ".
4. in Figure 1, right-click the table and choose add new table from the floating menu that appears ", you can add the "column name" and "Data Type" of the table to the right of Figure 1 in the database.
5. click the "X" symbol in the upper-right corner of Figure 1 with the left mouse button. A window appears. Click "yes" in the window, and a smaller window appears, in this window, enter the database table name, such as "book", and click "OK". Then the table is created, you can manually or programmatically add data to an empty table.
II. Implementation of Common page Printing
1. Select the current project in solution, add "dataset" to the current project, and add a able, such as book, to "dataset.
2. Create a webpage file, drag and drop the crystal report source control in the toolbox in vs 2008 to the webpage, and configure the report source. You can design a report by following the report generation wizard. In the "available data sources" in the report generation wizard, select the book table in the previously created dataset.
3. Drag the crystal report viewer control to the webpage and click the Crystal Report viewer control to set the report source for the control.
4. Place a button control in a proper position on the webpage and change the text attribute of the button to "print report ". Double-click the "print report" button to open the code page and introduce the following namespace in The namespace of the code page:
Using crystaldecisions. shared;
Using crystaldecisions. reportsource;
Using crystaldecisions. crystalreports. engine;
Add the following code to the button event:
ReportDocument orpt = new reportdocument ();
Orpt. Load (server. mappath ("crystalreport1.rpt "));
// Crystalreport1.rpt is the default name of the previously created crystal report file.
Orpt. printoptions. papersize = papersize. defaultpapersize;
// Set the paper size to the default size.
Pagemargins margins;
Margins = orpt. printoptions. pagemargins;
Margins. bottommargin = 0;
Margins. leftmargin = 0;
Margins. rightmargin = 0;
Margins. topmargin = 0;
// Set the margins of the above four behaviors.
Orpt. printoptions. applypagemargins (margins );
Orpt. printoptions. printername = "";
// Set the printer name here. If it is null, the printer is the default printer in the operating system.
Orpt. printtoprinter (1, false, 1, 1 );
// In the code, the first parameter in orpt. printtoprinter (1, false, 1, 1) is the number of printed copies.
// The second parameter indicates whether a copy-by-copy printing is enabled. If the value is false, the table is not printed in copy-by-copy mode.
// The third parameter is the starting page number for printing, and the fourth parameter is the ending page number.
Iii. Implementation of the precise printing function on webpages
Accurate printing refers to printing the page content to a certain format of forms, invoices, certificates, the implementation steps are as follows:
1. You can scan tables, invoices, and certificates that will be printed accurately to generate image files, or use image tools to draw images. Whether scanned or drawn, it must have the same size and proportion as the original.
2. open the report file, select "insert" from the Crystal Reports menu, select "OLE object", and select "create by file" in the displayed window ", click Browse to select the scanned or drawn image file on your computer.
3. if the accurately printed content is managed by the database, drag and drop the related database fields in the "field resource manager" of the report to the position where the report table, credential, and invoice are to be printed, design fonts, fonts, and sizes as required. If the printed content is not managed by the database, drag other related fields. 2.
4. Select the inserted OLE object, right-click the object, select "set object format", and select "suppress display" in the displayed window ". In this way, forms, certificates, and invoices in the crystal report will not be printed during printing (in other words, the form, document, and invoice here only serves to accurately place the printed content.
5. The related code is similar to the previously mentioned Code. However, if the size of the printed paper exceeds the A4 size, modify the following statement:
Orpt. printoptions. papersize = papersize. defaultpapersize;
For example, change defapappapersize to papera3, which indicates that the paper size is A3.