How to show print dialog in Crystal Report? - c#

I want to print my Crystal report direct to the printer. Currently I am having the export to PDF. But my client want this to go to Printer directly. How can I show the Print Dialog on click of Print Button to Print the report directly to Printer.
I would like to mention: I am using C# and asp.net for my project.
Thank you.

Try the following code
private void Button1_Click(object sender, EventArgs e)
{
CrystalReport1 report1 = new CrystalReport1();
PrintDialog dialog1 = new PrintDialog();
report1.SetDatabaseLogon("username", "password");
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;
report1.PrintOptions.PrinterName = dialog1.PrinterSettings.PrinterName;
report1.PrintToPrinter(copies, collate, fromPage, toPage);
}
report1.Dispose();
dialog1.Dispose();
}
you will have to change the "username" and the "password" with the credentials of your database.
EDIT
This code can be used for server side printing only.

No way; Cristal Report Viewer is intended for showing and browsing a report.
It never shows all report pages.
It has no buttons or methods for direct printing.
You could, instead, export directly the report in PDF so Report Viewer is never seen by users, and printing becomes 1-click operation.

PrintButton_click Event and add following code as you ..
//show Print Dialog
PrintDialog printDialog = new PrintDialog();
DialogResult dr = printDialog.ShowDialog();
if (dr == DialogResult.OK)
{
ReportDocument crReportDocument = (ReportDocument)CrystalReportViewer1.ReportSource;
System.Drawing.Printing.PrintDocument printDocument1 = new System.Drawing.Printing.PrintDocument();
//Get the Copy times
int nCopy = printDocument1.PrinterSettings.Copies;
//Get the number of Start Page
int sPage = printDocument1.PrinterSettings.FromPage;
//Get the number of End Page
int ePage = printDocument1.PrinterSettings.ToPage;
crReportDocument.PrintOptions.PrinterName =printDocument1.PrinterSettings.PrinterName;
//Start the printing process. Provide details of the print job
crReportDocument.PrintToPrinter(nCopy, false, sPage, ePage);
// Form_Printerd = true;
}

Related

PrintDialog open file after print

I am using PrintDialog and PrintDocument to generate pdf reports. The files are saved to user defined paths that is set at PrintDialog.
After the print event I want to open the file using System.Diagnostics.Process.Start.
The problem is that I do not know the user defined path.
PrintDialog.Document.PrinterSettings.PrintFileName throws an exception "The given path's format is not supported.".
However the file has been created and saved correctly to the user defined path.
// Allow the user to choose the page range
ProductionPrintDialog.AllowSomePages = true;
// Show the help button.
ProductionPrintDialog.ShowHelp = true;
// Set the Document property to the PrintDocument for
// which the PrintPage Event has been handled. To display the
// dialog, either this property or the PrinterSettings property
// must be set
ProductionPrintDialog.PrintToFile = true;
ProductionPrintDialog.Document = ProductionPrintDocument;
//ProductionPrintDialog.Document.PrinterSettings.PrintFileName = "c:\\test.pdf";
DialogResult result = ProductionPrintDialog.ShowDialog();
// If the result is OK then print the document.
if (result == DialogResult.OK)
{
ProductionPrintDocument.Print();
if(ProductionPrintDialog.Document.PrinterSettings.PrinterName == "Microsoft Print to PDF")
{
System.Diagnostics.Process.Start(ProductionPrintDialog.Document.PrinterSettings.PrintFileName);
}
}
How can I get the user defined path set at PrintDialog?

C# OpenFileDialog/CommonOpenFileDialog

I am working on this piece of code that is supposed to open a file dialog and put them into a textbox.
The error is that every time I select more than 1 file while running the app, I get an error in the textbox. If I select only one file, it works fine.
The code is this
private void filePickerButton_Click(object sender, RoutedEventArgs e)
{
// Create the OpenFileDialog object
OpenFileDialog dialog = new OpenFileDialog();
dialog.InitialDirectory = "C:\\Users";
dialog.Multiselect = true;
// Check to see if we have a result
if (dialog.ShowDialog() == true)
{
filePickerTextBox.Text = dialog.FileNames.ToString();
}
else
{
outputTextBox.Text = "Operation cancelled." + "\n" + outputTextBox.Text;
}
}
I am switching between dialog.Filename.ToString(); (to select one file) and dialog.Filenames.ToString(); to select multiple. When using the latter and selecting a file (whether its only one, or more that one, doesn't matter) the my text box shows System.String[]
Anyone know how to fix this?
Thx!
when you are selecting multiple files you get a array of files, as your textbox says: System.String[]
you could use:
filePickerTextBox.Text = string.join(",", dialog.FileNames);

WPF Report Printing

I am generating a crystal report using C# and WPF.
My code so far is
report.PrintOptions.PaperSize = (CrystalDecisions.Shared.PaperSize)System.Drawing.Printing.PaperKind.A5;
report.PrintToPrinter(2, true, 0, 0);
crystalReportsViewer1.ViewerCore.ReportSource = report;
crystalReportsViewer1.ToggleSidePanel = SAPBusinessObjects.WPF.Viewer.Constants.SidePanelKind.None;
I need to show a dialog box ie the PrintDialog to allow user just to select the printer he wants to print on and rest of the printing settings are done by me in the code. Please suggest ....
Try the following code
PrintDialog printDialog1 = new PrintDialog();
if (printDialog1.ShowDialog() == true)
{
report.PrintOptions.PrinterName = printDialog1.PrintQueue.Name;
report.PrintOptions.PaperSize = (CrystalDecisions.Shared.PaperSize)System.Drawing.Printing.PaperKind.A5;
report.PrintToPrinter(2, true, 0, 0);
crystalReportsViewer1.ViewerCore.ReportSource = report;
crystalReportsViewer1.ToggleSidePanel = SAPBusinessObjects.WPF.Viewer.Constants.SidePanelKind.None;
}
System.Printing must be in your references so that you can use PrintQueue.Name.

Print two documents with one same button

I'm printing two documents with Single button click. When i do this,
I have an error like : Dialog boxes must be open by user.
Here is the code :
PrintDocument monDocument = new PrintDocument();
btPrint.IsEnabled = true;
monDocument.PrintPage += new EventHandler<PrintPageEventArgs>(monDocument_PrintPage);
monDocument.Print("Fiche");
System.Threading.Thread.Sleep(5000);
if (itm.Letter != null || itm.Letter != "")
{
_lineIndex = 0;
_documentBodyLines = new List<string>();
string[] lines = tbLetter.Text.Split(new char[] { '\r' }, StringSplitOptions.None);
_documentBodyLines.AddRange(lines);
PrintDocument maLetter = new PrintDocument();
maLettreMotiv.PrintPage += new EventHandler<PrintPageEventArgs>(maLettreMotiv_PrintPage);
maLettreMotiv.Print("Letter");
}
Here, itmis the object I'm currently on.
The line throwing the error is the last one. I tried adding a sleep, but no results.
When I do this separately (one print with comment), this is working, but I don't find a way to do the two documents printing.
Firstly, there is a bug where if you put a break point in a print routine, you might end up with that error message.
Secondly, you should look at HasMorePages property. By setting it to true, you can print multi-page reports.

Page-Range-Problem at Printing a Document

i try to print out the content of my editor:
PrintDialog pd = new PrintDialog();
pd.PageRangeSelection = PageRangeSelection.AllPages;
pd.UserPageRangeEnabled = true;
FlowDocument fd = DocumentPrinter.CreateFlowDocumentForEditor(CurrentDocument.Editor);
DocumentPaginator dp = ((IDocumentPaginatorSource)fd).DocumentPaginator;
bool? res = pd.ShowDialog();
if (res.HasValue && res.Value)
{
fd.PageHeight = pd.PrintableAreaHeight;
fd.PageWidth = pd.PrintableAreaWidth;
fd.PagePadding = new Thickness(50);
fd.ColumnGap = 0;
fd.ColumnWidth = pd.PrintableAreaWidth;
pd.PrintDocument(dp, CurrentDocument.Editor.FileName);
}
The test-document i used has about 14 pages (with this pagesize-settings).
i tested it: the printdialog appears and I´ve chosen a pagerange (i typed "1-3" into the textbox) and clicked print. above the printdocument() I set a breakpoint and looked into the printdialog-object. it says pd.PageRangeSelection = PageRangeSelection.UserPage and pd.PageRange = {1-3}. I guess this is right, because I wanted to print out only page 1-3. then the printdocument() executed and in the output-pdf (for testing I use a pdf-printer) has 14 pages (the whole document was printed).
where is my mistake? why does the pagerange-setting not work?
thanks for your help
In your code you manually set:
pd.PageRangeSelection = PageRangeSelection.AllPages;
This is why your code prints all the pages.
The reason for this is because FlowDocument's DocumentPaginator does not handle UserPageRanges. You can see that FlowDocument implementation creates a FlowDocumentPaginator, and it doesn't take into account ranges.
If it did handle it, in FlowDocumentPaginator.(Async)GetPage you would see, code checking to see if the page requested to be printed is in an index of available pages; or maybe if a key exists in a Dictionary whose value is the DocumentPage to print.
In other words, and the reason the PrintDialog default has UserPageRangeEnabled set to false, is because in order to use that feature, you'll usually have to write your own DocumentPaginator or you have to add some logic to compile a new temporary document to hold only the pages you want to print.
Feel free to ask any questions.

Categories