The realization of printing A4 paper in C #

Source: Internet
Author: User
Tags count trim

In VB, if you want to print a A4 document, and the content is dynamically obtained from DB, or the DataGrid medium, it is simple to implement, such as the following code (RS represents a Recordset):

Rs. MoveFirst
Printer.papersize = vbPRPSA4
Printer.orientation = vbprorportrait
Printer.fontname = "Courier New "
Printer.fontbold = True
Printer.fontsize =
Printer.print" FQA "
Printer.font Size =
Printer.print "pallet ID:" & Trim (RS ("pallet_id")
NewLine2 = String (M, "")
Mid (newl Ine2, 1, 5) = "no#"
Mid (NewLine2) = "System s/n"
Mid (NewLine2,%) = "box_id"
Mid (NewLine2, = "pallet_id"
While not Rs. EOF
NewLine2 = String (M, "")
Mid (NewLine2, 1, 5) = Trim (rs ("No"))
Mid (NewLine2,) = Trim ( RS ("SN"))
Mid (NewLine2,) = Trim (rs ("box_id"))
Mid (NewLine2, m) = "" & Trim (RS ("pallet_id")
Printer.print NewLine2
Rs. MoveNext
Wend
printer.print NewLine2
Printer.print String (+, "-")

In the above code, if the number of records set, that is, content beyond a page, the program does not have to do any settings, will automatically page, then. NET, how to achieve such a simple function?

Check a lot of information, the Internet search for a long time, found no similar examples, looked at the MSDN, only to find that, to VB.net, VB so good printer disappeared, for the description of MSDN: http://msdn.microsoft.com/zh-tw/ library/cc438273 (vs.71). aspx

"The Printer object in Visual Basic 6.0 is replaced by the PrintDocument component in Visual Basic. NET. The behavior of the two is different, but in most cases the functionality can be replicated. The following table lists the Visual Basic 6.0 properties, methods, and events and their Visual Basic. NET equivalents. If there is no direct peer-to-peer usage, a link is provided to obtain additional information. Unless otherwise noted, all objects are in the System.Drawing namespace. "

The first feeling is that there are better printing features in. NET than this. And more powerful, so began to turn the book, the Internet to find examples, spent a good long time, finally found two examples on the internet, with textbooks (C # Advanced Programming Fourth Edition 25th, VB 2005 Introductory Classic 7th chapter) in the example, almost, Examples are to open the document first, after LoadFile, and then calculate how many lines, then set some properties, and so on, although powerful, but extremely complex, can not be used directly, so had to rewrite their own ...

Print function:

public bool Printdoc ()
{
Try
{
Get what you want to print
String strSQL = "exec [Usp_palletprintdocinfo] '"
+ parameters.strstation + "', '" + Parameters.strpalletid + "";
Dsprintinfo = Dodb.getdataset (strSQL);
if (dsprintinfo.tables[0). Rows.Count < 1 | | DSPRINTINFO.TABLES[1]. Rows.Count < 1)
{
parameters.strmsg = "Get Print information Error";
return false;
}
Print
PrintDocument PrintDocument = new PrintDocument ();
Printdocument.printpage + = new Printpageeventhandler (this.pd_printpage);
Printdocument.print ();
/**/////Print Preview, when debugging, you can save paper through this
PrintPreviewDialog PPD = new PrintPreviewDialog ();
PrintDocument PD = new PrintDocument ();
Pd. PrintPage + = new Printpageeventhandler (this.pd_printpage);
Ppd. Document = PD;
Ppd. ShowDialog ();
return true;
}
catch (Exception ex)
{
Parameters.strmsg = ex. Message.tostring ();
return false;
}
}

Related Article

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.