Click here to Skip to main content
15,892,059 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi Friends,

I have a small issue in printing. When i try to print a document from the print preview dialog box. its not printing anything. If i hide the preview dialog and invoke pdoc.Print(); its work perfect. Is there any special event for preview dialog button. please help...
C#
public void print()
       {
           PrintDialog pd = new PrintDialog();
           PrintDocument pdoc = new PrintDocument();
           int w=Convert.ToInt32(Length/2.54)*100;
           int h = Convert.ToInt32(Width / 2.54) * 100;
           //pdoc.DefaultPageSettings.Landscape = true;
           PaperSize psize = new PaperSize("Custom", w, h);
           pdoc.DefaultPageSettings.PaperSize = psize;
           pd.Document = pdoc;
           pdoc.PrintPage += new PrintPageEventHandler(pdoc_PrintPage);
           DialogResult result = pd.ShowDialog();
           if (result == DialogResult.OK)
           {
               PrintPreviewDialog ppd = new PrintPreviewDialog();
               ppd.Document = pdoc;
               ppd.PrintPreviewControl.Zoom = 1.0;
               ((Form)ppd).WindowState = FormWindowState.Maximized;
               DialogResult ppdResult = ppd.ShowDialog();
               // if i comment the above  line and uncomment pdoc.Print(); i can save the print as pdf.
              // pdoc.Print();
           }
       }
Posted

1 solution

 
Share this answer
 
Comments
jinesh sam 8-Nov-14 11:38am    
"The following code example shows the Click event-handling method for a button on the form." I need a button click event for the preview dialog Box
CoderzF1 8-Nov-14 19:50pm    
it also states that the print content basically has to be recreated to actually print.

if printpreviewdialog.showdialog = dialogresult.ok then
'insert code here to recreate the printable content
end if
jinesh sam 9-Nov-14 6:15am    
i try like this but no result
if (ppdResult == DialogResult.OK)
{
MessageBox.Show("hai");//this code block not executing
pdoc.Print();//this code block not executing
}
CoderzF1 14-Nov-14 9:13am    
I don't quite know how you are going about creating the printable content, but the code below works for me. It will show a printer selection dialog, so i can choose which printer I want to use. Then it will show the preview dialog so I know what the document will look like before printing. Then if i click the printer icon up top, the document will print to my desired printer. I have doPDF installed on my pc and it printed just fine. Is this what you are trying to acheive?

[code]
Private Sub Document_PrintPage(sender As System.Object, e As System.Drawing.Printing.PrintPageEventArgs) Handles Document.PrintPage
e.Graphics.DrawString("Hello", Me.Font, Brushes.Black, 100, 100)
End Sub

Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
prntDlg.Document = Document
If prntDlg.ShowDialog = Windows.Forms.DialogResult.OK Then
Dialog.Document = Document
Dialog.ShowDialog()
End If
End Sub

[/code]

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900