c# print function hide window - c#

I have my main form and then launch a new form. The new form is in the front. I send data to the printer which opens a print window popup. (prints to default, no printer to select) It then defaults back to my first form and I have to do a BringtoFront on the 2nd form after 1ms. This is an ok fix because it is only a small blip, but is there a way to hide that print window popup all together so that it just prints in the background?
// code to print
PrintDocument prnDocument;
string printername; //Get the default printer name.
prnDocument = new PrintDocument();
printername = Convert.ToString(prnDocument.PrinterSettings.PrinterName);
if (string.IsNullOrEmpty(printername))
throw new Exception("No default printer is set.Printing failed!");
prnDocument.PrintPage += new PrintPageEventHandler(prnDoc_PrintPage);
prnDocument.Print();

According to this SO post, you should be able to hide the print dialog by doing the following:
PrintDocument printDocument = new PrintDocument();
PrintController printController = new StandardPrintController();
printDocument.PrintController = printController;
Is this what you mean?

Related

How to select printer for FlowDocument in wpf c#

I am trying to print a FlowDocument directly to a pdf using Microsoft Print to pdf. Each time I want to print it sets the printer to the last printer that was used, I need it to set to "Microsoft Print to PDF".
// Create a PrintDialog
PrintDialog printDlg = new PrintDialog();
// Create a FlowDocument dynamically.
FlowDocument doc = CreateFlowDocument();
doc.Name = "OrderReceipt"+orderNo;
// Create IDocumentPaginatorSource from FlowDocument
IDocumentPaginatorSource idpSource = doc;
// Call PrintDocument method to send document to printer
printDlg.PrintDocument(idpSource.DocumentPaginator, "Save PDF");
Is this possible?
You might be using the default printer setting, which can be set to the last printer used:
https://support.microsoft.com/en-us/help/4028622/windows-10-how-to-set-a-default-printer
Have you tried calling PrintDialog.ShowDialog()? It pops up the printer options and lets you select which printer to send to and set preferences.
// Create IDocumentPaginatorSource from FlowDocument
IDocumentPaginatorSource idpSource = doc;
// Display printer options
if( printDlg.ShowDialog() ?? false )
{
// Call PrintDocument method to send document to printer
printDlg.PrintDocument(idpSource.DocumentPaginator, "Save PDF");
}

WPF Printing from a TextBox

I am having trouble printing from a TextBox in WPF
My Textbox will only contain a number between 1 and 999
I wanted to print font size 72 and the text enclosed within a 4" box (so they can cut around the edges)
private void InvokePrint(String contentToPrint)
{
// Create the print dialog object and set options
PrintDialog pDialog = new PrintDialog();
pDialog.PageRangeSelection = PageRangeSelection.AllPages;
pDialog.UserPageRangeEnabled = true;
// Display the dialog. This returns true if the user presses the Print button.
Nullable<Boolean> print = pDialog.ShowDialog();
if (print == true)
{
FixedDocumentSequence fixedDocSeq = xpsDocument.GetFixedDocumentSequence();
pDialog.PrintDocument(fixedDocSeq.DocumentPaginator, "Printing Label");
}
}
I got the code from Microsoft's site but they are using an XPS printer. I want to use the default selected printer (usually an HP).
Any help would greatly be appreciated

How to print multiple datagridview's using printdocument c#

I have a winform app that will show the user 3 different datagridviews with the relevant data they are enquiring about. I have allowed for the user to select which grids to print out. I can print the first page fine but after that it get an index error. I want it to be setup like any other print out where this one dialog box and they all print out in one document. Example if they select all 3 then 3 pages print. If they select just one then just that one. If they select two then those two print. How beyond the first page can you add the other grids?
Print button click event:
PrintDialog printDialog = new PrintDialog();
printDialog.Document = printDocument1;
printDialog.Document = printDocument2;
printDialog.Document = printDocument3;
printDialog.UseEXDialog = true;
if (DialogResult.OK == printDialog.ShowDialog())
{
if (chkBoxByScale.Checked)
{
printDocument1.DocumentName = "Project Report";
printDocument1.Print();
}
if (chkBoxByUser.Checked)
{
printDocument2.DocumentName = "Project Report 2";
printDocument2.Print();
}
if (chkBoxByLine.Checked)
{
printDocument3.DocumentName = "Project Report 3";
printDocument3.Print();
}
}
If you want me to provide nay of the PrintBegin or PrintPage let me know. Seemed very lengthy for posting all.

Printing in WPF similar to Winforms

Below is the process I worked in Windows forms for printing.
I used PrintDocument class. It contains PrintPage event and I used that to draw the graphics of what I need to print and obtained the result successfully as I expected.
Below is the code:
public PrintDocument Printing
{
m_printDocument = new PrintDocument();
m_printDocument.PrintPage += new PrintPageEventHandler(OnPrintPage);
}
The code for OnPrintPage as follows:
protected virtual void OnPrintPage(object sender, PrintPageEventArgs e)
{
//Image img = I have the things to be printing in the form of image.
e.Graphics.DrawImage(img, new Point(0,0));
}
In WPF:
I am working with Fixed document and by using the below code I can print
PrintDialog print = new PrintDialog();
print.PrintDocument(FixedDocument.DocumentPaginator, "Print") //Where Fixed document contains the data to be printed.
This results is insufficient memory to continue the execution of the program.
But I got Fixed document without any problem.
Any solutions ...?
I hope the similar thing as like Windows form would be there in WPF too...
I used to get this when my pages contained lots of visual elements (drawings/images/ complex diagrams). Instead of printing the complete document at once (which can lead to out of memory)
print.PrintDocument(FixedDocument.DocumentPaginator, "Print")
I printed one of its page.
PrintQueue selectedPrntQueue = printDialog.PrintQueue;
XpsDocumentWriter writer = PrintQueue.CreateXpsDocumentWriter(selectedPrntQueue);
SerializerWriterCollator collator = writer.CreateVisualsCollator();
collator.BeginBatchWrite();
var paginator = FixedDocument.DocumentPaginator;
FixedPage fixedPage = paginator.GetFixedPage(printedPageCount)
ContainerVisual newPage = new ContainerVisual();
Size sz = new Size(pageSize.Height.Value, pageSize.Width.Value);
fixedPage.Measure(sz);
fixedPage.Arrange(new Rect(new Point(), sz));
fixedPage.UpdateLayout();
newPage.Children.Add(fixedPage);
collator.Write(newPage);
I had to do GC after printing few pages (my magic number was 10).
You may need to tweak the this up to your requirement.

Using System.Drawing.Printing.PrintDocument in WPF

I have a WPF application and I use external library for generating documents. This library returns document as System.Drawing.Printing.PrintDocument. How can I print this document in WPF? I can use Print() method directly, but I need to allow user to select printer and settings. If I use WPF PrintDocument dialog, I can't set my document to it as in WinForms dialog.Document. Is there a way to convert old PrintDocument to some WPF friendly form?
WinForms way:
// get document for printing
PrintDocument document = exporter.GetPrintDocument();
System.Windows.Forms.PrintDialog dialog = new System.Windows.Forms.PrintDialog();
dialog.Document = document;
if (dialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
document.Print();
}
WPF way:
System.Windows.Controls.PrintDialog dialog = new System.Windows.Controls.PrintDialog();
if (dialog.ShowDialog() == true)
{
// how to print old PrintDocument???
dialog.PrintDocument(...);
}
I also tried to open WinForms dialog in WPF but it is not possible. Dialog is just not shown.
Thanks for any help.
I found an answer. You have to set UseDialogEx dialog property to true.
MessageBox.Show(printDialog1.PrinterSettings.PrinterName);
printDialog1.PrinterSettings.PrintFileName = "A.txt";
MessageBox.Show(printDialog1.PrinterSettings.PrintFileName);
printDialog1.ShowDialog();
printDocument1.DocumentName = "A.txt";
if (printDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
printDocument1.Print();
}

Categories

Resources