Printing RTF to specific printers in wpf ... without user interaction
-
Monday, April 30, 2012 6:17 PM
I have what should be a simple task but apparently is not.
I have a WPF program that generates 2 RTF documents. I need these two documents to be printed on two different local printers ( there are reasons why). However, I do not want any user interaction. The two printers have different names ( "PrinterA", "PrinterB"), These two printers are not usually the default printers.
I see a lot of code on how to print using the PrintDialog but nothing that shows me how to select a printer with a "pre-known" name.
Does anyone have an example of sending a document to a specific printer?
All Replies
-
Wednesday, May 02, 2012 9:38 AMModerator
Hi
Try the method in below link:http://support.microsoft.com/default.aspx?scid=kb;en-us;812425
If you have any additioanl questions, plase feel free to let me know.
Have a nice day.
Annabella Luo[MSFT]
MSDN Community Support | Feedback to us
- Marked As Answer by Annabella LuoModerator Tuesday, May 08, 2012 9:37 AM
-
Tuesday, May 08, 2012 9:37 AMModeratorWe are temporarily marking this as "Answer", if you have any concerns or new findings; please feel free to let me know.
Best regards.Annabella Luo[MSFT]
MSDN Community Support | Feedback to us
-
Wednesday, May 09, 2012 12:23 AM
Here is my partial solution made of pieces I found around the web.
private IEnumerable<PrintQueue> GetPrintQueues(string servername) { PrintServer ps; if (string.IsNullOrEmpty(servername)) { // local printer name ps = new LocalPrintServer(); } else { // network printer share ps = new PrintServer(servername); } return ps.GetPrintQueues(); } PrintQueue getPrintQueue(string printerName) { IEnumerable<PrintQueue> queues = GetPrintQueues(Properties.Settings.Default.Server); if (queues != null) { foreach (PrintQueue pq in queues) { if (pq.Name == printerName) { return pq; } } } return null; } public void printRtf(string rtf, string printer) { System.IO.Stream s = new System.IO.MemoryStream(ASCIIEncoding.Default.GetBytes(rtf)); FlowDocument flowDocument = new FlowDocument(); TextRange textRange = new TextRange(flowDocument.ContentStart, flowDocument.ContentEnd); textRange.Load(s, DataFormats.Rtf); System.Drawing.Printing.PrinterSettings.StringCollection printers = System.Drawing.Printing.PrinterSettings.InstalledPrinters; PrintQueue pq = getPrintQueue(printer); // Create a XpsDocumentWriter object, open a Windows common print dialog. // This methods returns a ref parameter that represents information about the dimensions of the printer media. XpsDocumentWriter docWriter = PrintQueue.CreateXpsDocumentWriter(pq); PageImageableArea ia = pq.GetPrintCapabilities().PageImageableArea; PrintTicket pt = pq.UserPrintTicket; if (docWriter != null && ia != null) { DocumentPaginator paginator = ((IDocumentPaginatorSource)flowDocument).DocumentPaginator; // Change the PageSize and PagePadding for the document to match the CanvasSize for the printer device. paginator.PageSize = new Size((double)pt.PageMediaSize.Width, (double)pt.PageMediaSize.Height); //Thickness pagePadding = flowDocument.PagePadding; //flowDocument.PagePadding = new Thickness( //Math.Max(ia.OriginWidth, pagePadding.Left), //Math.Max(ia.OriginHeight, pagePadding.Top), //Math.Max((double)pt.PageMediaSize.Width - (double)(ia.OriginWidth + ia.ExtentWidth), pagePadding.Right), //Math.Max((double)pt.PageMediaSize.Height - (double)(ia.OriginHeight + ia.ExtentHeight), pagePadding.Bottom)); //flowDocument.ColumnWidth = double.PositiveInfinity; // Send DocumentPaginator to the printer. docWriter.Write(paginator); } }A call to printRtf with a string that has rtf formatting will get printed to the specified print. It is only a partial solution because I have noticed that the text prints off the left side of the paper in some printers and farther to the right than I'd like on other printers.
I am not sure what the problem is but this seems to work for my current purposes.
- Marked As Answer by Annabella LuoModerator Wednesday, May 09, 2012 4:10 AM
-
Wednesday, May 09, 2012 4:10 AMModeratorThank you for your sharing, BuddyAtHome.
Annabella Luo[MSFT]
MSDN Community Support | Feedback to us

