How to add print dialog to the printpreviewdialog? - c#

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

Related

print document cannot print specific paper from multiple pages c#

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

Print a png file however can't seem to print

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'.

Click on "X" button to save file

I am trying to make WTF application were user clicks on "X" button to make save file, now this can be done by Message.Show or to ask directly to save. I have created one code already but when user click on save or cancel, error window shows up thatprogram start working and it wants to send information to Microsoft.
private void Window_Closing(object sender, CancelEventArgs e)
{
Microsoft.Win32.SaveFileDialog saveDlg = new Microsoft.Win32.SaveFileDialog();
saveDlg.DefaultExt = ".rtf";
saveDlg.Filter = "RTF Documents (.rtf)|*rtf";
Nullable<bool> rezultat = saveDlg.ShowDialog();
if (rezultat == true)
{
string filename = saveDlg.FileName;
System.IO.File.Create(filename);
}
{
this.Close();
}
}
I think that you might have intended there to be an else
else
{
this.Close();
}
Secondly, calling this.Close(); inside the Window_Closing event is just asking for a Stack Overflow exception.
You don't need to close the window again. It is already closing.
modify with your code with proper else statement with your if condition
private void Window_Closing(object sender, CancelEventArgs e)
{
Microsoft.Win32.SaveFileDialog saveDlg = new Microsoft.Win32.SaveFileDialog();
saveDlg.DefaultExt = ".rtf";
saveDlg.Filter = "RTF Documents (.rtf)|*rtf";
Nullable<bool> rezultat = saveDlg.ShowDialog();
if (rezultat == true)
{
string filename = saveDlg.FileName;
System.IO.File.Create(filename);
}
else
{
this.Close();
}
}

Show a user print preview and execute code if he printed

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!
}
}

Set print orientation to landscape

i already can create a print to print a file in my windows forms. However, whenever i add this code:
printDialog.PrinterSettings.DefaultPageSettings.Landscape = true;
I can't see the Orientation of the page become LandScape, it is still Portrait.
How do I make it LandScape as default? So, whenever i click PrintPreview or PrintFile, the Orientation of the page will become LandScape, not Portrait.
Here is the code:
private void PrintPreview(object sender, EventArgs e)
{
PrintPreviewDialog _PrintPreview = new PrintPreviewDialog();
_PrintPreview.Document = printDocument1;
((Form)_PrintPreview).WindowState = FormWindowState.Maximized;
_PrintPreview.ShowDialog();
}
private void PrintFile(object sender, EventArgs e)
{
PrintDialog printDialog = new PrintDialog();
printDialog.Document = printDocument1;
printDialog.UseEXDialog = true;
if (DialogResult.OK == printDialog.ShowDialog())
{
printDocument1.DocumentName = "Test Page Print";
printDocument1.Print();
}
}
try setting the Landscape of PrintDocument as follows,
printDocument1.DefaultPageSettings.Landscape = true;
The pdfprinting.net library is excellent for implementing the print functionality and be productive. Here is the simple code snippet to set print orientation to landscape(pdfPrint.IsLandscape = true;)
var pdfPrint = new PdfPrint("demoCompany", "demoKey");
string pdfFile = #"c:\test\test.pdf";
pdfPrint.IsLandscape = true;
int numberOfPages = pdfPrint.GetNumberOfPages(pdfFile);
var status = pdfPrint.Print(pdfFile);
if (status == PdfPrint.Status.OK) {
// check the result status of the Print method
// your code here
}
// if you have pdf document in byte array that is also supported
byte[] pdfContent = YourCustomMethodWhichReturnsPdfDocumentAsByteArray();
status = pdfPrint.Print(pdfFile);

Categories

Resources