I have a bitmap I want the user to see before he prints it. So I open for him print preview, if the user decides to print I want to execute some code.
The problem is, printPreviewDialog will not return an answer. This may be because it has only a print button and close button, but no print-and-close so I can know the user decided to print.
If you have a solution for that I'll be happy, if you think it's not the best way to do so please tell me.
code:
PrintDocument pd = new PrintDocument();
pd.PrintPage += new PrintPageEventHandler(Print_Page);
PrintPreviewDialog pritdlg = new PrintPreviewDialog();
pritdlg.Document = pd;
if (pritdlg.ShowDialog() == DialogResult.OK)
pd.Print();
else
MessageBox.Show("you have canceled print");
private void Print_Page(object o, PrintPageEventArgs e)
{
e.Graphics.DrawImage(target, 0,0);
}
Subscribe to the EndPrint event of the document you are sending to the printPreviewDialog control, then check the PrintAction in its PrintEventArgs argument.
Example:
private void buttonPrintPreview_Click(object sender, EventArgs e)
{
PrintPreviewDialog printDialog = new PrintPreviewDialog();
printDialog.Document = yourDocument;
yourDocument.EndPrint += doc_EndPrint; // Subscribe to EndPrint event of your document here.
printDialog.ShowDialog();
}
void doc_EndPrint(object sender, System.Drawing.Printing.PrintEventArgs e)
{
if (e.PrintAction == System.Drawing.Printing.PrintAction.PrintToPrinter)
{
// Printing to the printer!
}
else if (e.PrintAction == System.Drawing.Printing.PrintAction.PrintToPreview)
{
// Printing to the preview dialog!
}
}
Related
I am facing a problem to print using print document C#. When I am going to print and input 1 to 3 pages from 9 pages using printer setting and then command for print, this command cannot print 1 to 3 pages, this command print by default all documents. How can I fix this?
This is the code:
private void PrintPreview_Click(object sender, EventArgs e)
{
ToolStripButton b = new ToolStripButton();
b.Text = "print";
((ToolStrip)(printPreviewDialog1.Controls[1])).Items.RemoveAt(0);
((ToolStrip)(printPreviewDialog1.Controls[1])).Items.Insert(0, b);
((ToolStripButton)((ToolStrip)printPreviewDialog1.Controls[1]).Items[0]).Click += new System.EventHandler(this.button2_Click);
printPreviewDialog1.WindowState = FormWindowState.Maximized;
if (printPreviewDialog1.ShowDialog() == DialogResult.OK)
{
printPreviewDialog1.Show();
}
}
private void Print_Click(object sender, EventArgs e)
{
PrintDialog p1 = new PrintDialog();
p1.AllowSelection = true;
p1.AllowSomePages = true;
if (p1.ShowDialog ()== DialogResult.OK)
{
printDocument1.Print();
}
}
My boss wants me to create the window form that has printing function, but he wants to print the datagridview after preview.
So now I encourage the problem, I cannot print multiple set of paper or choose the printer or make any changes when click the print button on printpreviewdialog.When I click the button, it will direct print the paper. So I wish to join the printpreviewdialog and printdialog.
Why the printpreviewdialog and printdialog can only be used in different buttons? It's lack of usibility when needed to click one button to preview and click another button to print multiple set and make changes of printer.
Any one can help me?
printdialog
DialogResult result = printDialog1.ShowDialog();
// If the result is OK then print the document.
if (result == DialogResult.OK)
{
position = 0;
pageno = 1;
printDocument2.DefaultPageSettings.Margins = new Margins(20, 20, 20, 20);
printDocument2.OriginAtMargins = true;
printPreviewDialog1.Document = printDocument2;
printPreviewDialog1.ShowDialog();
}
printpreviewdialog
printDocument3.DefaultPageSettings.Margins = new Margins(20, 20, 20, 20);
printDocument3.OriginAtMargins = true;
//((ToolStripButton)((ToolStrip)printPreviewDialog1.Controls[1]).Items[0]).Enabled = false;
printPreviewDialog1.Document = printDocument3;
printPreviewDialog1.ShowDialog();
I know it is late, but i think someone will still need that.
As Hans Passant say, "print preview is heavily depended on printer and page settings."
But there is a print-button in printpreviewdialog, which is still reasonable for most cases. But that button directly prints to your default printer, and never shows a dialog.
If you want a print dialog from printpreview dialog, you can just manipulate ToolStrip of PrintPreviewDialog.
Here it goes (assuming you initialized printPreviewDialog1, printDialog1 and printDocument1 objects)
printPreviewDialog1.Document = printDocument1;
ToolStripButton b = new ToolStripButton();
b.Image = Properties.Resources.PrintIcon;
b.DisplayStyle = ToolStripItemDisplayStyle.Image;
b.Click += printPreview_PrintClick;
((ToolStrip)(printPreviewDialog1.Controls[1])).Items.RemoveAt(0);
((ToolStrip)(printPreviewDialog1.Controls[1])).Items.Insert(0, b);
printPreviewDialog1.ShowDialog();
Using above code, you can remove default print button on ToolStrip of PrintPreview and replace it with a newly created "print button". This button now has an Click event handler, and by using it, you can show the PrintDialog.
private void printPreview_PrintClick(object sender, EventArgs e)
{
try
{
printDialog1.Document = printDocument1;
if (printDialog1.ShowDialog() == DialogResult.OK)
{
printDocument1.Print();
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, ToString());
}
}
Works good...
Have one tip...
You can re-use the current icon by:
this.ToolStripButton.Image = ((System.Windows.Forms.ToolStrip)(printPreviewDialog.Controls[1])).ImageList.Images[0];
The rest of the snippet:
{
this.ToolStripButton = new System.Windows.Forms.ToolStripButton();
this.ToolStripButton.Image = ((System.Windows.Forms.ToolStrip)(printPreviewDialog.Controls[1])).ImageList.Images[0];
this.ToolStripButton.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Image;
this.ToolStripButton.Click += new System.EventHandler(this.printPreview_PrintClick);
((System.Windows.Forms.ToolStrip)(printPreviewDialog.Controls[1])).Items.RemoveAt(0);
((System.Windows.Forms.ToolStrip)(printPreviewDialog.Controls[1])).Items.Insert(0, ToolStripButton);
}
private void printPreview_PrintClick(object sender, System.EventArgs ee)
{
try
{
this.printDialog.Document = printDocument;
if (printDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
printDocument.Print();
}
}
catch (System.Exception ex)
{
System.Windows.MessageBox.Show(ex.Message, ToString());
}
}
private System.Windows.Forms.ToolStripButton ToolStripButton;
The snippet provided by #AceIndy above, does not take into account if the user changes the default printer or its settings. This is how I solved that problem:
private void printPreview_PrintClick(object sender, EventArgs e)
{
try
{
printDialog.Document = printDocument;
if (printDialog.ShowDialog() == DialogResult.OK)
{
printDocument.PrinterSettings = printDialog.PrinterSettings;
printDocument.Print();
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, ToString());
}
}
Good day! I was trying to print a png file using a Picturebox as my button.
However, I can't seem to print. Could you please help me? or Give me a link that will guide me on how to print on a default printer using C# VS 2010
private void pictureBox2_Click(object sender, EventArgs e)
{
using (PrintDocument pd = new PrintDocument())
{
using (PrintDialog printDialog = new PrintDialog())
{
if (printDialog.ShowDialog() == DialogResult.Yes)
{
pd.PrintPage += new PrintPageEventHandler(pd_PrintPage);
pd.Print();
}
}
}
}
private void pd_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
System.Drawing.Image img = Image.FromFile(#"C:\Coke\res10.png");
e.Graphics.DrawImage(img, 0,0);
}
Found a solution for this.
PrintDocument pd = new PrintDocument();
pd.PrintPage += (sender, args) =>
{
Image i = Image.FromFile("C://tesimage.PNG");
args.Graphics.DrawImage(i, args.PageBounds);
};
pd.Print();
This is a simple one, but easy to miss..
You are doing just about everything right. Just one tiny thing is not 'OK':
You need to change
if (printDialog.ShowDialog() == DialogResult.Yes)
to
if (printDialog.ShowDialog() == DialogResult.OK)
It is not a question, after all, so it shows 'OK' and 'Cancel' not 'Yes' and 'No'.
I've been trying to solve this myself, but I haven't figured it out yet. I want to open a print dialog window when I'm pressing the btn_print button. I've called out one line I assume is not needed anymore as this is defining the size of the printed page.
Could anyone look at my code and tell me what I could do?
private void btn_print_Click(object sender, RoutedEventArgs e)
{
try
{
PrintDocument pd = new PrintDocument();
//pd.DefaultPageSettings.PaperSize = new PaperSize("A4", 827, 1169);
pd.PrintPage += new PrintPageEventHandler(this.pd_PrintPage);
pd.Print();
}
catch (Exception ex)
{
MessageBox.Show("An error occurred while printing", ex.ToString());
}
}
private void pd_PrintPage(object sender, PrintPageEventArgs e)
{
}
Try something like this:
PrintDocument pd = new PrintDocument();
//pd.DefaultPageSettings.PaperSize = new PaperSize("A4", 827, 1169);
pd.PrintPage += new PrintPageEventHandler(this.pd_PrintPage);
System.Windows.Forms.PrintDialog p = new System.Windows.Forms.PrintDialog();
p.Document = pd;
if (p.ShowDialog() == System.Windows.Forms.DialogResult.OK)
pd.Print();
I have this demo app, I need to print FlowDocument without pop-up the Printing Dialogue from the operational system. Do you have any idea how to do it? Or you know other possible posolution?
private void Button_Click(object sender, RoutedEventArgs e)
{
PrintDialog pd = new PrintDialog();
pd.ShowDialog();
if (pd.ShowDialog() != true) return;
flowDocument.PageHeight = pd.PrintableAreaHeight;
flowDocument.PageWidth = pd.PrintableAreaWidth;
IDocumentPaginatorSource idocument = flowDocument as IDocumentPaginatorSource;
pd.PrintDocument(idocument.DocumentPaginator, "Printing Flow Document...");
}
delete this lines:
pd.ShowDialog();
if (pd.ShowDialog() != true) return;