Programmatically print to a virtual printer (XPS)

I'm going to display part of my C # WinForms application using PrintDocument , it's almost done, but the problem is with my printers. I use the following codes to capture an image of my form, and then print this image, and finally I use PrintPreviewDialog to display the preview:

 PrintDocument doc = new PrintDocument(); doc.PrintPage += doc_PrintPage; doc.Print(); printPreviewDialog1.Document = doc; printPreviewDialog1.ShowDialog(); 

and this is the doc_PrintPage function:

 Bitmap bmp = new Bitmap(tabControl1.Width, tabControl1.Height, System.Drawing.Imaging.PixelFormat.Format32bppArgb); tabControl1.DrawToBitmap(bmp, new Rectangle(0, 0, tabControl1.Width, tabControl1.Height)); e.Graphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic; e.Graphics.DrawImage((Image)bmp, 0, 0); 

When the doc.print() function is doc.print() , the Microsoft OneNote program opens and displays my printable form, and the PrintPreviewDialog control opens a new form containing my preview.

I will have quiet printing, so that no printer program (for example, OneNote installed as my default printer) or a physical printer opens (I think that if my user is connected to a physical printer, the page will actually print, which is definitely not what I want). I just want to display a print preview without any printing, can I print to XPS in any way (virtual printer file?), Or in any other way that doesn't actually work?

+4
source share
2 answers

you will need to set / change PrintDocument.PrinterSettings to install a specific printer, otherwise Print will not know which specific printer is needed and will prompt the user to confirm the default printer or choose another one.

Update:

On top of my head, something like the following might do the trick:

 PrinterSettings printerSettings = new PrinterSettings(); printerSettings.PrinterName = "Microsoft XPS Document Writer"; doc.PrinterSettings = printerSettings; 

You may have to experiment with various settings to get exactly what you want.

+2
source

I can not add comments, so the answer. Peter Ritchie's suggestion is true if you want to be sure that the printout does not end on a (virtual) printer.

I checked PrintPreviewDialog and here is what I got:

 PrintDocument doc = new PrintDocument(); doc.PrinterSettings.PrinterName = this.m_printingParameters.SelectedPrinterName; doc.PrinterSettings.PrintFileName = Path.Combine(Path.GetTempPath(), "Temporary_result.xps"); doc.PrinterSettings.PrintToFile = true; doc.PrintPage += doc_PrintPage; PrintPreviewDialog printPreviewDialog1 = new PrintPreviewDialog(); printPreviewDialog1.Document = doc; printPreviewDialog1.ShowDialog(); 

If the user clicks on the print icon, the result will be printed to a file placed in a temporary folder. To make sure you can delete the file later.

However, if you want to create a print preview dialog without a print button, check this section " Disable and print" in the print preview dialog box .net

Is this what you were looking for?

+2
source

Source: https://habr.com/ru/post/1479256/


All Articles