Discuss creating a report print service based on WSE and its implementation

Source: Internet
Author: User
Tags abstract bool constructor reference tostring client
Report Printing | creating | Report Printing Summary:
I've seen Mr. Lu Yan. On the Web report printing implementation of the article, will certainly for the solution provided in the section applauded, this article tries to show you a more flexible print operation water, and has a certain practicality. It is recommended that you read the two articles of Mr. Lu Yan before reading this article, at the same time, the use of Microsoft's WSE (1.0) as an auxiliary tool, a strange friend, suggested that the first reference to a number of conceptual articles, I put in the end of the article a number of reference materials and the required tools, we can experience.

--------------------------------------------------------------------------------

Directory
Introduction
Software principle
Program implementation
Attention matters
Summarize
Resources

--------------------------------------------------------------------------------

Introduction:
WSE provides a very convenient feature, is to support the transmission of attachments, although we can use other ways to achieve this goal, such as direct return byte data, but for most applications, directly return an attachment, such as a picture more practical, please do not misunderstand my article title, To create a webservice can be easily achieved by printing, our software principles and the way the final print is not much different from what Mr. Lu Yan mentions, we just take advantage of the strong penetration of webservice to make this way more flexible, easier to apply and expand, All the code used in this article is written in C #.

--------------------------------------------------------------------------------

Software principle:
This article takes the form of XML data, the client will need to print the data and some basic parameters, such as image size, image form and so on to the server side, and the server side according to the requirements of the client to generate a specific one or more pictures returned to the client, by the client's print program unified processing, Looking at this logic, we can see that all the business rules are shipped entirely on the server side, and the client only needs a small amount of code to print the report. This avoids the annoyance of a variety of upgrades, and when it comes to adding one or more charts to the actual application, all we need to do is add or modify the server-side business rules, and all the customers have to do is tell us to print the chart.
This paper uses a number of simple XML data, only for demonstration purposes, the client's demo data are as follows:
<?xml version= "1.0" encoding= "Utf-8"?>
<root printtype= "line" width= "450" height= "" title= "Print Demo" >
<child text= "1" value= "color=" "Black" >
</Child>
<child text= "2" value= "color=" "Orange" >
</Child>
<child text= "3" value= "color=" "Red" >
</Child>
<child text= "4" value= "color=" "Gray" >
</Child>
<child text= "5" value= "color=" "Blue" >
</Child>
<child text= "6" value= "color=" "Green" >
</Child>
</Root>

Where Printtype is the type of print required by the client.
Program implementation:
Server-side code
This system also uses the abstract factory design pattern, in order to facilitate the server side convenient expansion, here will not repeat.
Create a new Web service project, add abstract base classes, implement classes, parse classes, and the finished interface looks like this:
Need to add a reference to the Microsoft.Web.Services namespace


which
PrintBase.cs is a base class
Parser.cs is the Analytic class
LinePrint.cs is a class that implements specific images
Base class Code:
public class Printbase
{
Public Printbase ()
{
//
TODO: Add constructor logic here
//
}

Public virtual Stream DrawImage ()
{
return null;
}
}

Parse Class Code:
public class Parser
{
Public Parser ()
{
//
TODO: Add constructor logic here
//
}

public static Printbase createelement (DataSet DS)
{
Printbase PB = null;

String l_strprinttype = ds. tables["Root"]. rows[0]["Printtype"]. ToString ();

Switch (l_strprinttype)
{
Case "line":
PB = new Lineprint (DS);
Break
Default
PB = new Printbase ();
Break
}
return PB;
}

}

Implementing the code for the class:
Because the code here is longer, I only posted part of the code for reference, we can according to their actual situation to draw graphics.
<summary>
Rules for overloaded painting
</summary>
<returns> Image stream</returns>
public override Stream DrawImage ()
{
_chartsize = new SizeF (float. Parse (ds. tables["Root"]. rows[0]["Width"]. ToString ()),
Float. Parse (ds. tables["Root"]. rows[0]["Height"]. ToString ()));

Bitmap B = new Bitmap (int) _chartsize.width, (int) _chartsize.height,
PIXELFORMAT.FORMAT32BPPARGB);

Class
_graphics = Graphics.fromimage (b);

The following omitted, please draw your own

Store return
MemoryStream s = new MemoryStream ();
B.save (s,imageformat.png);
return s;
}

I personally always prefer to use dataset to manipulate small XML data, please adjust to their preferences, the method shown here will be directly for the Web service class call.
Code for the Web service class:
[WebMethod]
public bool CreateImage (DataSet DS)
{
bool L_bstatus = true;

Try
{
Printbase PB = null;

PB = Parser.createelement (ds);

Stream s = pb. DrawImage ();

SoapContext sc = httpsoapcontext.responsecontext;

Sc. Attachments.Add (New DimeAttachment ("Image/png", typeformatenum.mediatype,s));

}
Catch
{
L_bstatus = false;
}

return l_bstatus;
}

At this point, our server-side code is basically completed, the following we look at the client needs to do the work, I set up a WinForm project here to do the demo, the actual use of the Lu Yan article mentioned in the UserControl can be.
Client code:
private void Butto



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.