This article from http://blog.sina.com.cn/s/blog_5d5c7ed60100i6ev.html
Design reports are not only time-consuming, but also boring! However, end users trust printed reports, and most applications also need them. Therefore, how can we make full use of your UI function and let your users present it on paper? Undoubtedly, xtraprinting library is definitely your best choice.Xtraprinting LibraryIt is a 100% C # data presentation and visualization system. It is used for Special Visual Studio. NET and connects your user interfaces with printed pages. Once you start using the xtraprinting library, you can easily print the content in the advanced visual control, as simple as xtragrid and xtratreelist. This allows you to provide unlimited reports to end users without the need for a traditional report editor.
So how can we perfectly interpret the printing function of xtraprinting library? Huidu control net, which has been researching for many years in the use of Dev control packages, is willing to useXtraprinting LibraryTo share with you the experience.
First, implementPrint functionYou must add a reference: devexpress. xtraprint.
Main classes required for printing:
1. Public class printingsystem: component, isupportinitialize, iprintingsystem, iaccessible, iexporter, iprintingsystemcontext
This class provides methods to generate reports and some functions to set and print reports on pages. The attributes of this class include objects that access other management reports, such as the document class, brickgraphics (class for drawing on reports) and xtrapagesettings (class for setting printing pages)
2. Public class printablecomponentlink: Link
This class is used to print all controls that implement the devexpress. xtraprinting. iprintable interface. It provides some basic functions for generating and printing reports. After a report is generated, you can print the report through printingsystem.
The above two classes can fully implement variousControl PrintingAs long as a control implements the devexpress. xtraprinting. iprintable interface, it can print the control, such as gridcontrol and charcontrol. The specific implementation method is as follows:
Public class printsettingcontroller
{
Printingsystem PS = NULL;
String formname = NULL;
Printablecomponentlink link = NULL;
String _ printheader = NULL;
/// <Summary>
/// Print the title
/// </Summary>
Public String printheader
{
Set
{
_ Printheader = value;
}
}
String _ printfooter = NULL;
/// <Summary>
/// Footer when printing
/// </Summary>
Public String printfooter
{
Set
{
_ Printfooter = value;
}
}
Bool _ landscape;
/// <Summary>
/// Horizontal and vertical page
/// </Summary>
Public bool landscape
{
Set {_ landscape = value ;}
}
System. Drawing. Printing. paperkind _ paperkind;
/// <Summary>
/// Paper
/// </Summary>
Public System. Drawing. Printing. paperkind
{
Set {_ paperkind = value ;}
}
/// <Summary>
/// Print the Controller
/// </Summary>
/// <Param name = "control"> components to be printed </param>
Public printsettingcontroller (iprintable Control)
{
If (control = NULL) return;
Control C = (Control) control;
Formname = C. findform (). GetType (). fullname + "." + C. Name;
PS = new devexpress. xtraprinting. printingsystem ();
Link = new devexpress. xtraprinting. printablecomponentlink (PS );
PS. Links. Add (Link );
Link. Component = control;
}
/// <Summary>
/// Print and preview
/// </Summary>
Public void Preview ()
{
Try
{
If (devexpress. xtraprinting. printhelper. isprintingavailable)
{
Cursor. Current = cursors. appstarting;
If (_ printheader! = NULL)
{
Pageheaderfooter phf = link. pageheaderfooter as pageheaderfooter;
// Set the header
Phf. header. content. Clear ();
Phf. header. content. addrange (New String [] {"", _ printheader ,""});
Phf. header. font = new system. Drawing. Font ("", 14, system. Drawing. fontstyle. Bold );
Phf. header. linealignment = brickalignment. Center;
// Set the footer
Phf. footer. content. Clear ();
Phf. footer. content. addrange (New String [] {"", "", _ printfooter });
Phf. footer. font = new system. Drawing. Font (" ter", 9, system. Drawing. fontstyle. Regular );
Phf. footer. linealignment = brickalignment. Center;
}
Link. paperkind = ps. pagesettings. paperkind;
Link. Margins = ps. pagesettings. margins;
Link. Landscape = ps. pagesettings. landscape;
Link. createdocument ();
// Chinese
Devexpress. xtraprinting. Localization. previewlocalizer. Active = new dxperience. localizationchs. dxperiencextraprintingloCalizationchs ();
PS. previewformex. Show ();
}
Else
{
Cursor. Current = cursors. default;
MessageBox. Show ("Printer unavailable", "prompt", messageboxbuttons. OK, messageboxicon. information );
}
}
Finally
{
Cursor. Current = cursors. default;
}
}
/// <Summary>
/// Print
/// </Summary>
Public void print ()
{
Try
{
If (devexpress. xtraprinting. printhelper. isprintingavailable)
{
If (_ printheader! = NULL)
{
Pageheaderfooter phf = link. pageheaderfooter as pageheaderfooter;
// set the header
phf. header. content. clear ();
phf. header. content. addrange (New String [] {"", _ printheader, ""});
phf. header. font = new system. drawing. font ("", 14, system. drawing. fontstyle. bold);
phf. header. linealignment = brickalignment. center;
// set the footer
phf. footer. content. clear ();
phf. footer. content. addrange (New String [] {"", "", _ printfooter});
phf. footer. font = new system. drawing. font ("", 9, system. drawing. fontstyle. regular);
phf. footer. linealignment = brickalignment. center;
}
Link. paperkind = ps. pagesettings. paperkind;
Link. Margins = ps. pagesettings. margins;
Link. Landscape = ps. pagesettings. landscape;
Link. createdocument ();
Link. createdocument ();
// Chinese
Devexpress. xtraprinting. Localization. previewlocalizer. Active = new dxperience. localizationchs. dxperiencextraprintingloCalizationchs ();
PS. Print ();
}
Else
{
Cursor. Current = cursors. default;
MessageBox. Show ("Printer unavailable", "prompt", messageboxbuttons. OK, messageboxicon. information );
}
}
Finally
{
}
}
// Obtain page settings
Public void loadpagesetting ()
{
System. Drawing. Printing. Margins margins = new system. Drawing. Printing. Margins (60, 60, 60, 60 );
PS. pagesettings. Assign (margins, _ paperkind, _ landscape );
}
}
How to Use the client: instantiate printsettingcontroller and set parameters, including the horizontal and vertical paper type of the header and footer. Of course, all these settings can also be set in the printsettingcontroll class. I need them here, so it is added out, printsystem. the pagesettings attribute is of the xtrapagesettings type. The xtrapagesettings class encapsulates all page settings. You can modify it in the loadpagesetting () function to implement the desired function, for example, you can save the settings as an XML file.
Printsettingcontroller PSC = new printsettingcontroller (this. gridcontrol1 );
// Header
PSC. printheader = This. text;
// Footer
PSC. printfooter = This. datetimepicker1.text. tostring ();
// Horizontal and vertical directions
PSC. Landscape = This. rbtnhorizon. checked;
// Paper
PSC. paperkind = system. Drawing. Printing. paperkind. A4;
// Load page settings
PSC. loadpagesetting ();
PSC. Preview ();
In addition, this class implements the Chinese function for the print preview dialog box, adds a reference to dxchinese. dll, and thenProgramAdded the followingCodeTo achieve Localization:
Devexpress. xtraprinting. Localization. previewlocalizer. Active = new dxperience. localizationchs. dxperiencextraprintingloCalizationchs ();]
In response to the strong requirements of our customers, huide is devexpress. net users customize a Chinese resource package, devexpress official Chinese resources, you can get this set of complete and professional Chinese resources at very little cost, so that your product can be fully Chinese.
HuideDevexpress Advanced TrainingBig screen is enabled! This training aims to give you a deep understanding of the functions of the devexpress control and help you quickly learn how to use the product, application skills, key code, and troubleshooting skills. At the same time, we will combine the product application cases, devexpress advanced training provides you with in-depth industry analysis, allowing you to easily complete project development.