How to present print settings from print preview? - c#

In my C# Project I have 2 print functions. One that prints the document directly and another that presents a preview to the user and prints if the user chooses to do so.
While both methods are working, the direkt print version presents the print settings window before it prints the document.
private void printButton_Click(object sender, EventArgs e)
{
PrintDialog printDialog = new PrintDialog();
printDialog.Document = printIssues;
printDialog.UseEXDialog = true;
if (DialogResult.OK == printDialog.ShowDialog())
{
printIssues.DocumentName = "Some Name";
printIssues.DefaultPageSettings.Landscape = true;
printIssues.Print();
}
}
private void previewButton_Click(object sender, EventArgs e)
{
PrintPreviewDialog printPreview = new PrintPreviewDialog();
printPreview.Icon = Properties.Resources.favicon;
printPreview.Document = printIssues;
printIssues.DefaultPageSettings.Landscape = true;
((Form)printPreview).WindowState = FormWindowState.Maximized;
printPreview.ShowDialog();
}
The second Version where I present the preview first, once I click the print button the document gets printed on the Default Printer without presenting the Settings window. I've tried some things and searched quiet some time but couldn't find anything that helped me.
I appreciate your help.

I'm afraid this is a known limitation of the PrintPreviewDialog.
It needs to know the printer to draw the layout so it uses the default printer.
I've had the same problem in the past, and I believe it can be solved by showing a PrintDialog before showing the PrintPreviewDialog.
private void previewButton_Click(object sender, EventArgs e)
{
PrintDialog printDialog = new PrintDialog();
if (DialogResult.OK == printDialog.ShowDialog())
{
PrintPreviewDialog printPreview = new PrintPreviewDialog();
printPreview.Document = printIssues;
// this is were you take the printersettings from the printDialog
printPreview.Document.PrinterSettings = printDialog.PrinterSettings;
printIssues.DefaultPageSettings.Landscape = true;
printPreview.ShowDialog();
}
}
Another workaround would be to make your own PrintPreviewDialog. But it requires more coding.
can you tell me if the above code works for you?

Related

How to bring Print Dialog box to front?

I have a Print Button to print my Crystal report directly to the printer. My code is as below:
CrystalDecisions.CrystalReports.Engine.ReportClass clsReport = new CrystalDecisions.CrystalReports.Engine.ReportClass();
protected void Button3_Click(object sender, EventArgs e)
{
System.Windows.Forms.PrintDialog dialog1 = new System.Windows.Forms.PrintDialog();
dialog1.AllowSomePages = true;
dialog1.AllowPrintToFile = false;
if (dialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
int copies = dialog1.PrinterSettings.Copies;
int fromPage = dialog1.PrinterSettings.FromPage;
int toPage = dialog1.PrinterSettings.ToPage;
bool collate = dialog1.PrinterSettings.Collate;
clsReport.PrintOptions.PrinterName = dialog1.PrinterSettings.PrinterName;
clsReport.PrintToPrinter(copies, collate, fromPage, toPage);
}
}
This working fine but my problem is, whenever I click on Print Button the Print Dialoq appears at the back of all pages so that sometimes I cannot see even that it came or not. So, I want to bring the Print Dialog box in front so on top of all window.
Thank you.

printing content of a picturebox

HI all,
I have a picture box in my C# WinForms application which is sized 800x800. I want to print the content of this picture box using the following code but it does not do anything at all (just shows the print dialog and when I click on PRINT in the dialog it doe nothing too. What's wrong?
private void menuFilePrint_Click(object sender, EventArgs e)
{
printDocument.OriginAtMargins = true;
printDocument.DocumentName = "TEST IMAGE PRINTING";
printDialog.Document = printDocument;
printDialog.ShowDialog();
}
private void printDocument_PrintPage(object sender, PrintPageEventArgs e)
{
e.Graphics.DrawImage(curveBox.Image, 0, 0);
}
You didn't instruct the printDocument to print:
if(printDialog.ShowDialog() == DialogResult.OK)
printDocument.Print();
the printDialog is used to set printing settings.
Without this command "printDocument1.Print();" Nothing will work.

Printing a document in C#

I am trying to print a document with .txt extension and for that I am referring to my course book but confused. Here is the source code given in the book :
private void Print_Click(object sender, EventArgs e)
{
printDialog1.Document = printDocument1 // there is no object in my program names ad printDocument1
DialogResult result = printDialog1.ShowDialog();
if (result == DialogResult.OK)
{
printDocument1.Print();
and there is lot more code given
Your example is assuming that a PrintDocument object has been dragged onto the form from the toolbox: you could though just as easily create the object yourself.
private void Print_Click(object sender, EventArgs e)
{
PrintDocument printDocument = new PrintDocument();
printDocument.PrintPage += new PrintPageEventHandler(printDocument_PrintPage);
PrintDialog printDialog = new PrintDialog
{
Document = printDocument
};
DialogResult result = printDialog.ShowDialog();
if (result == DialogResult.OK)
{
printDocument.Print(); // Raises PrintPage event
}
}
void printDocument_PrintPage(object sender, PrintPageEventArgs e)
{
e.Graphics.DrawString(...);
}
Open the form in design mode. Locate the PrintDocument control in the toolbox. Drag and drop it onto your form. That adds a field to your form class named "printDocument1". Next, double-click the printDocument1 image displayed below the form. That adds the PrintPage event handler. Use the code from your book.
Review the instructions in the book, it should have mentioned this. Best to use its step-by-step instructions rather than mine.
If you want to print an existing .txt file, you can have Windows print it:
using System.Diagnostics;
Process myProcess = new Process();
myProcess.StartInfo.FileName = "C:\\thefile.txt"; // adjust to your needs
myProcess.StartInfo.Verb = "print";
myProcess.Start();
See Process.

PrinterSetting are ignored while printing an Image

I'm trying to print an Image using PrintDocumentin C# but somehow the setting (like Number of Pages and Image Quality ) are ignored while printing and preview.
Is there anything wrong in following code, Am I missing something?
private void button1_Click(object sender, EventArgs e)
{
using (var printDialog = new PrintDialog())
{
if (printDialog.ShowDialog() == DialogResult.OK)
{
_printDocument.PrinterSettings = printDialog.PrinterSettings;
}
}
}
void _printDocument_Print(object sender, PrintPageEventArgs e)
{
using (Image image = new Bitmap("image0002.tif"))
{
e.Graphics.DrawImage(image, e.MarginBounds.X, e.MarginBounds.Y);
}
}
Have you tried setting the PrintDialog's Document property to the document you want to print? The dialog should automatically manage the settings for the current PrintDocument if I remember correctly, so there should be no need to manually assign the PrinterSettings.
In addition, I think that a DialogResult.OK from PrintDialog.ShowDialog() means that you should print the document (the user clicked the 'Print' button).
For example:
using (var printDialog = new PrintDialog { Document = _printDocument })
{
if (printDialog.ShowDialog() == DialogResult.OK)
{
_printDocument.Print();
}
}
Does that help?
EDIT: If you don't want to print right away, you could try:
using (var printDialog = new PrintDialog { Document = _printDocument })
{
printDialog.ShowDialog();
}
but users might find it a little strange if they click 'Print' and the document doesn't print.

.NET PrintDocument - Text gets cut off

I have an application (C#, .NET 3.5) that writes receipts. They are usually printed with a small receipt printer with the .NET PrintDocument. The problem is the exception cases where I want to print with a regular printer. In these cases the text gets cut off. I wish to have a check or a switch to prevent this, but still keep the tight margins on the small printer.
What would be the best way of dealing with this? Can I do this without touching the graphics generation?
Once you have set the Printer in PrintDocument, you might take a look at the present PaperSize using the value of:
PrintDocument.PrinterSettings.DefaultPageSettings.PaperSize
Or maybe:
PrintDocument.PrinterSettings.PaperSizes
My issue was that I was setting the printerDocument.DefaultPageSettings.PaperSize.
Instead, I recommend this
private PrintDocument _printDocument = new PrintDocument();
private int _checkPrint;
public Form1()
{
InitializeComponent();
_printDocument.BeginPrint += _printDocument_BeginPrint;
_printDocument.PrintPage += _printDocument_PrintPage;
}
private void btnPrint_Click(object sender, EventArgs e)
{
PrintDialog printDialog=new PrintDialog();
if (printDialog.ShowDialog() == DialogResult.OK)
_printDocument.Print();
}
private void _printDocument_PrintPage(object sender, PrintPageEventArgs e)
{
// Print the content of RichTextBox. Store the last character printed.
_checkPrint = rchEditor.Print(_checkPrint, rchEditor.TextLength, e);
// Check for more pages
e.HasMorePages = _checkPrint < rchEditor.TextLength;
}
private void _printDocument_BeginPrint(object sender, PrintEventArgs e)
{
_checkPrint = 0;
}

Categories