Friday, 21 January 2011

Print Xaml in background - Part2 - the PrintTicket

Following my previous post on Printing Xaml code to XPS, here's the second part.
In this part, I explain how to attach "Page Settings" to the XpsDocument. This can be done by using the System.Printing.PrintTicket in your code.

In my example, I print to A3, landscape.

Define the printticket


1:private  static  PrintTicket  pt = new  PrintTicket ();

Fill it with the necessary data:


1:PrintQueue  pq = LocalPrintServer .GetDefaultPrintQueue();
2: 
3:pt = new  System.Printing.PrintTicket 
4:             {   OutputColor  = OutputColor .Color,
5:                 PageMediaType  = PageMediaType .Plain,
6:                 PageOrientation  = PageOrientation .Landscape,
7:                 PageMediaSize  = 
new PageMediaSize(PageMediaSizeName .ISOA3),
8:                 PageBorderless  = PageBorderless .Borderless
9:             };
10:System.Printing.ValidationResult result = 
pq.MergeAndValidatePrintTicket(pq.UserPrintTicket, pt);

Code the printing itself:

1: XpsDocument  document =
2:         new  XpsDocument (path + @"\\sample.xps" , System.IO.FileAccess .Write);
3: XpsDocumentWriter  writer =
4:     XpsDocument .CreateXpsDocumentWriter(document);
5: writer.WritingPrintTicketRequired +=
6:         new  WritingPrintTicketRequiredEventHandler (writer_WritingPrintTicketRequired);
7: writer.Write(myfixedDocumentToPrint, pt);

And finally the writer_WritingPrintTicketRequired function

1: static  void  writer_WritingPrintTicketRequired(object  sender, 
2:                         WritingPrintTicketRequiredEventArgs  e)
3: {
4:     e.CurrentPrintTicket = pt;
5: }


Have fun...

No comments:

Post a Comment