Lester's WPF\SL Blog

Simple, Easy & Beautiful

Printing RichTextBox content: (Find the idle printer)

Printing RichTextBox content: (Find the idle printer)

Rate This
  • Comments 1

Recently, Praj blogged about printing content of a RichTextBox. Normally, if you use the documentPaginator or the visual, you might end up with some text clipped. Not a nice thing to happenJ. One thing that bugs me while printing is the print dialog. Often times, I just want to use a printer that’s not busy. Just don’t make me wait. It’s pretty simple to code  and its worth it. No more waiting at the printer. ;)

foreach (PrintQueue pq in GetPrintQueues("\\\\servername"))

            {

                if (!pq.IsBusy)

                {

                   Print(pq);

                   return;

                }

            }

        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();

       }

        void Print(PrintQueue pq)

        {

            TextRange sourceDocument = new TextRange(richTextBox.Document.ContentStart, richTextBox.Document.ContentEnd);

            MemoryStream stream = new MemoryStream();

            sourceDocument.Save(stream, DataFormats.Xaml);

 

           // Clone the source document's content into a new FlowDocument.

            FlowDocument flowDocumentCopy = new FlowDocument();

            TextRange copyDocumentRange = new TextRange(flowDocumentCopy.ContentStart, flowDocumentCopy.ContentEnd);

            copyDocumentRange.Load(stream, DataFormats.Xaml);

            // 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)flowDocumentCopy).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 = flowDocumentCopy.PagePadding;

                flowDocumentCopy.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));

                flowDocumentCopy.ColumnWidth = double.PositiveInfinity;

                // Send DocumentPaginator to the printer.

                docWriter.Write(paginator);

            }

        }

Share this post