Using XML to implement universal web report printing (4)

xiaoxiao2021-03-06  103

Finally, we also need a parser to parse the top layer label and generate the corresponding object, and its role in this mode is a "factory class", which is responsible for producing the "product" required by the user. code show as below:

using System; using System.Xml; namespace RemotePrint {public class Parser {public Parser () {} public static PrintElement CreateElement (XmlNode element) {PrintElement printElement = null; switch (element.Name) {case "text": printElement = new TEXT (ELEMENT); Break; Case "Table": Printelement = New Table (Element); Break; default: printelement = new printelement (); Break;} Return Printelement;}}}

Ok, the core analysis and the specific print method of the label have been completed, and now we go back to PrintControl to write some code to test our results.

First, you need to reference two namespaces to be used:

Using system.xml; using system.drawing.printing;

Then, before printing, you need to set the pages of the printer according to the PageSetting tag in the XML file, so we write a method to set the printer. Add a private method in the PrintControl class:

private void SettingPrinter (XmlNode ps) {file: // orientation (portrait / landscape) this.printDocument1.DefaultPageSettings.Landscape = bool.Parse (ps [ "landscape"] .InnerText); file: // set the paper type string papername = ps [ "paperkind"] InnerText; bool fitpaper = false; file:. // Get all paper supported by the printer type foreach (PaperSize size in this.printDocument1.PrinterSettings.PaperSizes) {if (papername == size.PaperName) / / See if the printer has the paper type we need {this.printDocument1.defaultpagesettings.papersize = size; fitpaper = true;}}} If there is no need to type, customizable Size this.PrintDocument1.defaultpagesettings.papersize = new papersize ("Custom", INT.PARS (PS ["Paperwidth"]. InnerText), int.Parse (PS ["paperHeight"]. InnerText);}}

Next, we add an XMLDocument object and a static variable calculation page number:

Private xmldocument doc = new xmldocument (); public static int pages = 1;

Then load XML report data for the object in the LOAD event of the control, the code is as follows:

Private Void PrintControl_load (Object Sender, System.EventArgs E) {Try {File: // Load Report XML Data THISLABEL1.TEXT = "Loading report data, please wait a little ..."; doc.load ("http: //localhost/report.xml" ?;this.label1.text = "Report data is loaded!"; this.button1.enabled = this.button2.enabled = this.button3.enabled = true;} catch (Exception EX) {this.label1.text = "An error:" ex.Message;}} Please note that we only put a local test data file, in fact, it can be changed to any place or dynamic from anywhere on the network. XML file, such as the above Doc.Load ("http: //localhost/report.xml") can be rewritten:

Doc.Load ("http://www.nywhere.com/report.xml");

Doc.Load ("http://www.anyywhere.com/report.asp");

Doc.Load ("http://www.anyywhere.com/report.jsp?date=xxx");

Wait, as long as the loaded data is possible to meet the XML data documentation we specified.

Then add a printed event to the control of the control:

Public printControl () {initializationComponent (); this.printDocument1.printpage = new printpageeventhandler (this.pd_printpage);}

The code of the entrustment method is as follows:

private void pd_PrintPage (object sender, PrintPageEventArgs ev) {Graphics g = ev.Graphics; bool HasMorePages = false; PrintElement printElement = null; foreach (. XmlNode node in doc [ "root"] [ "reporttable"] ChildNodes) {printElement = Parser.createElement (Node); // Call the parser Generate the corresponding object try {hasmorepages = Printelement.draw (g); // Need to paid} catches (Exception ex) {this.label1.text = ex. measureage;} } file: // Output page number font font = new font ("black body", 12.0f); brush brush = new solidbrush (color.black); g.drawstring ("No. Pages.Tostring () "Page", Font, Brush, Ev.Marginbounds.Width / 2 ev. Marginbounds.left - 30, ev.pagebounds.Height - 60); if (HasmorePages) {Pages ;} ev.hasmorepages = hasmarepages;}

Three button Click event codes are as follows: @ Page Setup private void button1_Click (object sender, System.EventArgs e) {this.pageSetupDialog1.ShowDialog (); this.printDocument1.DefaultPageSettings = this.pageSetupDialog1.PageSettings;} file : // Print preview private void button2_Click (object sender, System.EventArgs e) {try {this.printPreviewDialog1.ShowDialog ();} catch (Exception ex) {this.label1.Text = ex.Message;}} file: / / Print Private Void Button3_Click (Object sender, system.eventargs e) {TRY {this.printDocument1.print ();} catch (exception ex) {this.label1.text = ex. measure;}}

Ok, our print control is all done here, choose to generate a release version, then copy the generated printControl.dll file to the IIS in the project directory, then create a RemotePrint.htm HTML Format file, plus: , in order to more image And beautiful, you can also make the data you need to print into a web page. If you need to get the XML is a dynamic data source, you can use the dynamic script of ASP to generate the web page, if you need to get the XML is a static Text, you can use XSLT to convert an XML file into a web form directly.

Open the browser, type: http://localhost/remoteprint.htm, if you have already made an XML report data file like me, you can see the effect shown below.

Please note: All the data in the illustration sample is the author's casual virtual. The table data and print data in the web page are not from the same data source, nor deliberately goes right, just to demonstrate the effect, so the webpage shows the report and print There are some new reports in the preview. In practical applications, web display data can be identical to printout data.

转载请注明原文地址:https://www.9cbs.com/read-122904.html

New Post(0)
CopyRight © 2020 All Rights Reserved
Processed: 0.043, SQL: 9