Home>

As the title says, when I tried to print with WPF, there seemed to be several means, and I found multiple samples.

However, I am in trouble because I cannot find the place to print by specifying the printer name from the code.
Currently, I am printing as follows.

Windows10 C # WPF VS2015

The canvus on the third line from the top is created like Canvas.SetTop (textBlock, 100) ;.

          FixedDocument doc = new FixedDocument ();
            FixedPage fp = CreateFixedPage (page, 1);
            fp.Children.Add (canvus);
            PageContent pc = new PageContent ();
            pc.Child = fp;
            doc.Pages.Add (pc);
            PrintDialog dlg = new System.Windows.Controls.PrintDialog ();
            // if (dlg.ShowDialog () == true)
            // {
                System.Windows.Xps.XpsDocumentWriter xpsdw = PrintQueue.CreateXpsDocumentWriter (dlg.PrintQueue);
                doc.PrintTicket = dlg.PrintTicket;
                xpsdw.Write (doc, dlg.PrintTicket);
    //}
        }
        public static Size PageSize {get;set;}
        private static FixedPage CreateFixedPage (Page page, int size)
        {
            Action dummy = () =>{};
            var disp = System.Windows.Threading.Dispatcher.CurrentDispatcher;
            FixedPage fixpage = new FixedPage ();
            {
                try
                {
                    Frame fm = new Frame ();
                    fm.Content = page;
                    disp.Invoke (dummy, System.Windows.Threading.DispatcherPriority.Loaded);// When you need to do various bindings
                    FixedPage.SetLeft (fm, 0);
                    FixedPage.SetTop (fm, 0);
                    fixpage.Children.Add (fm);
                    page.Width = 8.27 * 96;// 793.92
                    page.Height = 11.69 * 96;// 1122.24
                    Size sz = new Size (page.Width, page.Height);
                    fixpage.Width = page.Width;
                    fixpage.Height = page.Height;
                    fixpage.Measure (sz/* PageSize * /);
                    fixpage.Arrange (new Rect (new Point (), sz/* PageSize * /));
                    fixpage.UpdateLayout ();
                }
                catch (Exception ex)
                {
                    MessageBox.Show (ex.Message);
                }
                return fixpage;
            }
  • Answer # 1

    It will not be resolved, so the solution is also listed here.

              PrintDialog dlg = new System.Windows.Controls.PrintDialog ();
                // Create a local print server
                LocalPrintServer ps = new LocalPrintServer ();
                dlg.PrintQueue = ps.GetPrintQueue (printerMei);// Write the actual printer name in PrinterMei
                // if (dlg.ShowDialog () == true)
                {
                    System.Windows.Xps.XpsDocumentWriter xpsdw = PrintQueue.CreateXpsDocumentWriter (dlg.PrintQueue);
                     doc.PrintTicket = dlg.PrintTicket;
                    xpsdw.Write (doc, dlg.PrintTicket);