WPF Print Tickets

Source: Internet
Author: User
Tags blank page

Recent work on the content of WPF, the overall development of no difficulty, mainly in print because there is no experience, made some difficult, but fortunately, the solution is not very laborious.

WPF printing a ticket or printing ordinary paper is not very different, just said print the ticket to the need to play the content of the position, the scaling, you can put the ticket directly printed.
So the key point is 3:
1. What classes and methods are used by WPF to perform printing
2. How to Position
3. How to fix the zoom ratio

1. What classes and methods are used by WPF to perform printing

The problem is easy to solve, and you can find the sample code by searching for WPF print or WPF printing.
So I'm using the Printvisual method of PrintDialog. PrintDialog can be seen from the name as a dialog box that lets the user manually select the printer. If you do not want to pop up the dialog box and select a printer, you can read the default printer or configure the printer name in the configuration file and locate it. This requires the use of another two classes: PrintQueue and Localprintserver.
Print with PrintDialog:

var printDialog = new PrintDialog ();p rintdialog.printqueue = GetPrinter ();p rintdialog.printvisual (visual, Visual. Name);

Get the printer task queue:

public static PrintQueue GetPrinter (string printername = null) {Try{printqueue selectedprinter = null;if (!string. IsNullOrEmpty (PrinterName)) {var printers = new Localprintserver (). Getprintqueues (); selectedprinter = printers. FirstOrDefault (p = p.name = = printername);} Else{selectedprinter = Localprintserver.getdefaultprintqueue ();} return selectedprinter;} Catch{return null;}}
2. How to Position

Notice that the printed code above is used by printvisual, the parameter is visual, then what is this visual?
I cite the inheritance relationship of a WPF Grid class: Grid:Panel:FrameworkElement:UIElement:Visual, so WPF controls are inherited from UIElement and are inherited by Visual.
So we think of the grid as a bill or a piece of paper, and it's OK to put the printed content on this piece of paper.
You can create a user control to drag and drop, pass in the entity object binding value, or generate a grid dynamically.

3. How to fix the zoom ratio

Just put it in place and print it out is not necessarily the result we want. Because the size of the bill is different, especially the Bank ID card or the amount of small lattice, playing crooked can only explain the technology is not home ah.
So the placement is to have a basis, the basis is to scan the ticket, and then in the scanned basemap placed, the sample position will not be misplaced. The scaling is then the concept of DPI (DPI is the abbreviation for dots per inch printed). The image we scanned is pixel, and the actual paper cannot be in pixels. The conversion between this needs to rely on DPI.
Method of specific scaling:

Note that I am here dpi write Dead is 150, in practice your dpi is how much to see scan how to sweep. var settings = new PrintSettings {Width = Visual. Width, Height = Visual. Height, DPI =};var rendertarget = new RenderTargetBitmap ((int) settings. Width, (int) settings. Height, settings. DPI, settings. DPI, pixelformats.default);p rintdialog.printticket = new PrintTicket ();p rintDialog.PrintTicket.PageMediaSize = new Pagemediasize (Rendertarget.width, rendertarget.height); var capabilities = PrintDialog.PrintQueue.GetPrintCapabilities (printdialog.printticket); var scale = Math.max (capabilities. Pageimageablearea.extentwidth/visual. Width, capabilities. Pageimageablearea.extentheight/visual. Height); Visual. LayoutTransform = new ScaleTransform (scale, scale); var sz = new Size (capabilities. Pageimageablearea.extentwidth, capabilities. pageimageablearea.extentheight); Visual. Measure (SZ); visual. Arrange (New Rect (new point (0, 0), SZ));

This allows us to zoom in and you can look at MSDN to see what the specific classes and methods mean.

Other needs: 1, vertical dozen

Some of the documents are narrower, but the width can be, so I hope to be able to print vertically, to meet this demand is also a sentence of the matter.
In Visual. Measure (SZ); Add the following two lines of code before the statement.

PrintDialog.PrintTicket.PageOrientation = Pageorientation.landscape;printdialog.printticket.pagemediasize = new Pagemediasize (Rendertarget.height, rendertarget.width);
2, paper back (needle printer)

Paper back is not a common function, but put the wrong paper want to take out also have to cost some effort, so want to let the printer automatically spit paper. I also searched a lot of questions and answers and articles, did not try to come out a successful, may be incorrect method. Finally, a way to compare a chicken thief is to print a blank page and then automatically exit the paper. Each needle printer may be different, so the size of the blank page of the exit should be adjusted well.

var printer = GetPrinter (); var visual = new Grid () {Width = 1000,height = 1500,verticalalignment = Verticalalignment.top,ho Rizontalalignment = HorizontalAlignment.Left}; Printvisual (printer, visual);
3. Monitor Print task status

When printing, you must want to know whether the task is printed, remind the user to put the paper, after printing to remind users to print. I wrote a Printjobchecker class here that will check the task queue and print time based on the interval of the timer.
But. NET provides the method is not very good to achieve the ideal effect, can only get to the task has no, it is very depressing thing. Once the printer starts printing (note that it is not finished), the job is null. This does not determine if the paper is still in print. If a friend knows how to handle it, look for a comment to tell.

public class Printjobchecker{private DispatcherTimer _timer;private PrintQueue _printer;private action<string> _ Checkingaction;public DateTime? startprinttime {get; set;} private int _interval = 100;public int Timerinterval{get {return _interval;} Set{_interval = Value;_timer. Interval = Timespan.frommilliseconds (value);}} Public Printjobchecker (PrintQueue printer, action<string> checkingaction) {if (printer = = NULL | | checkingaction = = NULL) {return;} _printer = Printer;_checkingaction = Checkingaction;_timer = new Dispatchertimer{interval = TimeSpan.FromMilliseconds ( TimerInterval),};_timer. Tick + = Checkjobstatus; Printingstatus = "Printing"; Printerrorstatus = "Error printing"; Printofflinestatus = "Please connect the printer"; Printwaittingstatus = "Please put the corresponding form to the printer"; Printunknownstatus = "Unknown Error";} public void Start () {_timer. Start ();} public void Stop () {_timer. Stop ();} private void Checkjobstatus (object sender, EventArgs e) {if (_printer = = null) {return;} var job = _printer. Getlastjob (); if (job = = null) {if (! StartprinttimE.hasvalue) {startprinttime = DateTime.Now;} _checkingaction (printingstatus);} Else{var statustext = getjobstatus (Job); _checkingaction (statustext);}} public string Printingstatus {get; set;} public string Printerrorstatus {get; set;} public string Printofflinestatus {get; set;} public string Printwaittingstatus {get; set;} public string Printunknownstatus {get; set;} private string Getjobstatus (Printsystemjobinfo job) {if (job = null) return Null;if ((job.   Jobstatus & printjobstatus.completed) = = printjobstatus.completed) | | (Job. Jobstatus & printjobstatus.printed) = = printjobstatus.printed)) {startprinttime = Datetime.now;return Printingstatus;} if (job. Jobstatus & printjobstatus.error) = = Printjobstatus.error) {_timer. Stop (); return printerrorstatus;} if (job.   Jobstatus & printjobstatus.offline) = = printjobstatus.offline| | Job. Jobstatus = = Printjobstatus.none) {return printofflinestatus;} if (job. Jobstatus & printjobstatus.printing) = = printjobstatus.printing) {if (job. Timesincestartedprinting > 0) {return printingstatus;} Else{return printwaittingstatus;}} return printunknownstatus;}}

  

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

WPF Print Tickets

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.