This code works well printing to default printer, but how I can do to display the Print Dialog to allow the user to select any printer?
var report = new ReportDocument();
report.Load(System.Windows.Forms.Application.StartupPath + #"\Reports\RouteSlip.rpt");
report.Database.Tables[0].SetDataSource(dtData.Tables[0]);
var prntSettings = new PrinterSettings
{
Copies = 1,
Collate = false,
};
report.PrintToPrinter(prntSettings, new PageSettings(), false);
Related
I have a WPF application written in C# where i directly send the report to printer without preview,when i do that garbage values are being printed on the print out page.However if i use ExportToDisk and download the report to my desktop the report is being downloaded with proper content .This code written is just a conversion of an existing older VB.NET webforms application where i was able to print to printer successfully.
ReportDocument ObjDoc = new ReportDocument();
cocPrinting.FetchReportDataforUTEnglish(pdtPrintDetails.Rows[lirow]["DOCMFRAN"].ToString().Trim(), pdtPrintDetails.Rows[lirow]["DOCMBRCD"].ToString().Trim(), pdtPrintDetails.Rows[lirow]["DOCMDPCD"].ToString().Trim(), pdtPrintDetails.Rows[lirow]["DOC_NO"].ToString().Trim(), pdtPrintDetails.Rows[lirow]["DOCMBANNER"].ToString().Trim(), pdtPrintDetails.Rows[lirow]["COPY_TYPE"].ToString().Trim(), lsOrgDup, ref cdtUTDocEng, lsErr);
ObjDoc.Load("//Server Name//Crystal_Reports//VHRSSALEFDE002.rpt");
if (cdtUTDocEng.Rows.Count > 0)
ObjDoc.SetDataSource(cdtUTDocEng);
else
{
lsPrintMessage = "Printing Failed -Failed to fetch Undertaking English details";
break;
}
if (cEnvironment.Production == psEnvironment)
{
// ObjDoc.PrintOptions.PaperOrientation = PaperOrientation.Portrait
// ObjDoc.PrintOptions.PaperSize = CrystalDecisions.Shared.PaperSize.PaperA4
ObjDoc.PrintOptions.PrinterDuplex = PrinterDuplex.Default;
lsPrinterName = cmbPrinter.SelectedValue.ToString().Trim();
if (CheckifPrinterInstalled(cmbPrinter.SelectedValue.ToString().Trim()) == true)
{
// ObjDoc.PrintOptions.PrinterName = cmbPrinter.SelectedValue.ToString().Trim()
ObjDoc.PrintOptions.PrinterName = cmbPrinter.SelectedValue.ToString().Trim();
ObjDoc.PrintToPrinter(1, false, 0, 0);
ObjDoc.Dispose();
lsPrintMessage = lsPrintMessage + "Full documentation English " + pdtPrintDetails.Rows[lirow]["COPY_TYPE"].ToString().Trim() + " has been sent to printer " + cmbPrinter.SelectedValue.ToString().Trim() + #" \r\n";
}
else
lsPrintMessage = "This printer " + cmbPrinter.SelectedValue.ToString().Trim() + " is not installed on server,cannot print";
}
if (cEnvironment.Development == psEnvironment)
{
ExportFormatType formatType = ExportFormatType.NoFormat;
formatType = ExportFormatType.PortableDocFormat;
ObjDoc.ExportToDisk(formatType, exportPath + "\\UndertakingEnglishDoc.pdf");
ObjDoc.Dispose();
lsPrintMessage = lsPrintMessage + "Undertaking English Doc downloaded to desktop";
}
I resolved the above issue we need to call an overloaded method of PrintToPrinter and pass printer setting and page settings to it, instead of the one mentioned in the code above.Below is the solution
ReportDocument ObjDoc = new ReportDocument();
System.Drawing.Printing.PrintDocument pDoc = new System.Drawing.Printing.PrintDocument();
CrystalDecisions.Shared.PrintLayoutSettings PrintLayout = new CrystalDecisions.Shared.PrintLayoutSettings();
System.Drawing.Printing.PrinterSettings printerSettings = new System.Drawing.Printing.PrinterSettings();
System.Drawing.Printing.PageSettings pSettings = new System.Drawing.Printing.PageSettings(printerSettings);
System.Drawing.Printing.PageSettings pageSettings = new System.Drawing.Printing.PageSettings(printerSettings);
//get your details in Dataset
ObjDoc.Load("//ServerName//Crystal_Reports//VHRSSALEPDU001.rpt"); ObjDoc.SetDataSource(YourDataset);
ObjDoc.PrintOptions.PrinterDuplex = PrinterDuplex.Default;
lsPrinterName = cmbPrinter.SelectedValue.ToString().Trim();
ObjDoc.PrintOptions.PrinterName = cmbPrinter.SelectedValue.ToString().Trim(); ObjDoc.PrintToPrinter(printerSettings, pSettings, false, PrintLayout);
ObjDoc.Dispose();
Need only 2 dll for this
CrystalDecisions.CrystalReports.Engine
CrystalDecisions.Shared
Hi I am working on a MVC Project in which I have a reports page where user can view reports with the use of a report viewer.
I need to set the page size dynamically for a report, I tried many ways to solve this but I couldn’t.
I can change the ReportViewer Size with this code
rptviewer.Height = Unit.Pixel(520);
Kindly help me in with the following queries.
1.Is it possible to change the SSRS report page height using C# Code?
2.Is it possible to change the paper size in the runtime?
My Previous workarounds
<-------- 1 --------->
System.Drawing.Printing.PageSettings pg = new System.Drawing.Printing.PageSettings();
pg.Margins.Top = 0;
pg.Margins.Bottom = 0;
pg.Margins.Left = 0;
pg.Margins.Right = 0;
System.Drawing.Printing.PaperSize size = new PaperSize();
size.RawKind = (int)PaperKind.A5;
pg.PaperSize = size;
rptviewer.SetPageSettings(pg);
ViewBag.ReportViewer = rptviewer;
return View("_ReportView");
<-------- 2 --------->
System.Drawing.Printing.PageSettings MyPageSize= new System.Drawing.Printing.PageSettings();
MyPageSize.PaperSize = new System.Drawing.Printing.PaperSize("Custom", 17, 12);
rptviewer.SetPageSettings(MyPageSize);
<-------- 3 --------->
var setup = rptviewer.GetPageSettings();
setup.PaperSize.Height = 1500;
rptviewer.SetPageSettings(setup);
None of the above logic worked for me :-(
The thing is, In order to control the page size in the rendering process we need to pass the proper Device Information Settings to the report at run time. This will work for physical page oriented renders like PDF, Image, etc. This is a simple Xml string that can be passed as a parameter to the report in order to control these settings. Each export type has a different set of properties that can be overridden and controlled in this way.
In the following example, an export to PDF is performed and passes the page height and width to match an A4 paper size as the targeted device (line 66):
private void RenderReportToClient()
{
//set credentials
RSExecuteProxy.ReportExecutionService rs = new RSExecuteProxy.ReportExecutionService();
rs.Credentials = System.Net.CredentialCache.DefaultCredentials;
RSProxy.ReportingService2005 rsInfo = new RSProxy.ReportingService2005();
rsInfo.Credentials = System.Net.CredentialCache.DefaultCredentials;
// init render args
byte[] result = null;
string reportPath = rptViewer.ServerReport.ReportPath;
string format = "PDF";
string historyId = null;
string encoding;
string mimeType;
string extension;
RSExecuteProxy.Warning[] warnings = null;
string[] streamIDs = null;
//init exec info
RSExecuteProxy.ExecutionInfo execInfo = new RSExecuteProxy.ExecutionInfo();
RSExecuteProxy.ExecutionHeader execHeader = new RSExecuteProxy.ExecutionHeader();
rs.ExecutionHeaderValue = execHeader;
//get report
execInfo = rs.LoadReport(reportPath, historyId);
String SessionId = rs.ExecutionHeaderValue.ExecutionID;
//get parameter info
ReportParameterInfoCollection parameters = rptViewer.ServerReport.GetParameters();
//figure out how many parameters we will have
//those with multi-value will need there own ParameterValue in the array
int paramCount = 0;
foreach (ReportParameterInfo pramInfo in parameters)
{
paramCount += pramInfo.Values.Count;
}
RSExecuteProxy.ParameterValue[] prams = new SSRSWeb.RSExecuteProxy.ParameterValue[paramCount];
int currentPramPosition = 0;
//set pram values
foreach (ReportParameterInfo pramInfo in parameters)
{
foreach (string pramValue in pramInfo.Values)
{
prams[currentPramPosition] = new SSRSWeb.RSExecuteProxy.ParameterValue();
prams[currentPramPosition].Label = pramInfo.Name;
prams[currentPramPosition].Name = pramInfo.Name;
prams[currentPramPosition].Value = pramValue;
currentPramPosition++;
}
}
rs.SetExecutionParameters(prams, "en-US");
//build the device settings (A4 8.3 × 11.7)
string deviceInfo = string.Format("<DeviceInfo><PageHeight>{0}</PageHeight><PageWidth>{1}</PageWidth></DeviceInfo>", "11.7in", "8.3in");
//get report bytes
result = rs.Render(format, deviceInfo, out extension, out encoding, out mimeType, out warnings, out streamIDs);
Response.ClearContent();
Response.AppendHeader("Content-Disposition", "inline;filename=report.pdf");
Response.AppendHeader("content-length", result.Length.ToString());
Response.ContentType = "application/pdf";
Response.BinaryWrite(result);
Response.Flush();
Response.Close();
}
When the report is saved you can view the PDF and examine the properties and notice that the page height and width is 8.3in x 11.7in as specified.
I am using the free version of Spire.PDF to print a local pdf file but the print is in grayscale even though the pdf file is in color. Here is my code
PdfDocument doc = new PdfDocument();
doc.LoadFromFile(fileName);
doc.ColorSpace = PdfColorSpace.RGB;
PrintDialog dialogPrint = new PrintDialog();
dialogPrint.AllowPrintToFile = true;
dialogPrint.AllowSomePages = true;
dialogPrint.PrinterSettings.MinimumPage = 1;
dialogPrint.PrinterSettings.MaximumPage = doc.Pages.Count;
dialogPrint.PrinterSettings.FromPage = 1;
dialogPrint.PrinterSettings.ToPage = doc.Pages.Count;
if (dialogPrint.ShowDialog() == DialogResult.OK)
{
//Set the pagenumber which you choose as the start page to print
doc.PrintFromPage = dialogPrint.PrinterSettings.FromPage;
//Set the pagenumber which you choose as the final page to print
doc.PrintToPage = dialogPrint.PrinterSettings.ToPage;
//Set the name of the printer which is to print the PDF
doc.PrinterName = dialogPrint.PrinterSettings.PrinterName;
PrintDocument printDoc = doc.PrintDocument;
printDoc.DefaultPageSettings.Color = true;
dialogPrint.Document = printDoc;
printDoc.Print();
}
I have checked dialogPrint.PrinterSettings.SupportsColor returns true
I try to print to XPS printer (it's not my default printer), but the program opens me a dialog.
Can I skip the dialog? This is the code:
pSettings = new PrinterSettings();
pSettings.PrintFileName = "test.xps";
RawPrinterHelper.SendStringToPrinter(pSettings.PrinterName, toSend);
spcn = new StandardPrintController();
printDocument1.PrinterSettings.PrinterName = "Microsoft XPS Document Writer";
printDocument1.PrintController = spcn;
printDocument1.PrintPage +=
new PrintPageEventHandler(printDocument1_PrintPage);
printDocument1.Print();
You can print PDF to XPS without a dialog using Aspose API
Aspose API
//Create PdfViewer object and bind PDF file
PdfViewer pdfViewer = new PdfViewer();
pdfViewer.OpenPdfFile("input.pdf");
//Set PrinterSettings and PageSettings
System.Drawing.Printing.PrinterSettings printerSetttings = new System.Drawing.Printing.PrinterSettings();
printerSetttings.Copies = 1;
printerSetttings.PrinterName = "Microsoft XPS Document Writer";
//Set output file name and PrintToFile attribute
printerSetttings.PrintFileName = "C:\\tempfiles\\printoutput.xps";
printerSetttings.PrintToFile = true;
**//Disable print page dialog**
**pdfViewer.PrintPageDialog = false;**
//Pass printer settings object to the method
pdfViewer.PrintDocumentWithSettings(printerSetttings);
pdfViewer.ClosePdfFile();
I have a problem with selecting printer to print my document.
My code is :
var filename = #"C:\Users\I\Desktop\test.doc";
PrintDialog pd = new PrintDialog();
pd.PrinterSettings =new PrinterSettings();
if (DialogResult.OK == pd.ShowDialog(this))
{
Process objP = new Process();
objP.StartInfo.FileName = filename;
objP.StartInfo.WindowStyle =
ProcessWindowStyle.Hidden; //Hide the window.
objP.StartInfo.Verb ="print";
objP.StartInfo.Arguments ="/p /h \"" + filename + "\" \"" + pd.PrinterSettings.PrinterName + "\"";
objP.StartInfo.CreateNoWindow = false;
//true;//!! Don't create a Window.
objP.Start();
//!! Start the process !!//
objP.CloseMainWindow();
}
and whatever I choose, process always will use default printer, no matter what value of pd.PrinterSettings.PrinterName is.
What's wrong with my code?
You probably want to use "PrintTo" instead of "print" for the verb. You already set objP.FileName to the filename so there's no need to get complicated in the arguments. Pass the printer name alone there.
var filename = #"C:\Users\I\Desktop\test.doc";
PrintDialog pd = new PrintDialog();
pd.PrinterSettings =new PrinterSettings();
if (DialogResult.OK == pd.ShowDialog(this))
{
Process objP = new Process();
objP.StartInfo.FileName = filename;
objP.StartInfo.WindowStyle = ProcessWindowStyle.Hidden; //Hide the window.
objP.StartInfo.Verb ="PrintTo";
objP.StartInfo.Arguments = pd.PrinterSettings.PrinterName;
objP.StartInfo.CreateNoWindow = false;
//true;//!! Don't create a Window.
objP.Start();
//!! Start the process !!//
objP.CloseMainWindow();
}
Try changing pd.PrinterSettings =new PrinterSettings(); to read something like this:
pd.PrinterSettings =new System.Drawing.Printing.PrinterSettings;
By default when you create an instance of printer settings it returns the default printer name just an fyi... you can then try something like this
//sudu code
foreach(string strPrinter in PrinterSettings.InstalledPrinters)
{
// or unless you know the name of the printer then skip this and assign it to the code above
}