Friday, November 23, 2018

Custom Printer and Paper Sizes Using C# and Crystal Report

Using Custom Printer and Paper Size With Crystal Report.

Often you may have faced situations where you are required to design a report (mostly bills, receipts, invoices etc) based on half or one 3rd of regular available paper sizes. It is fairly easy to define custom paper sizes based on requirement how ever a few points should be taken into consideration if your application is a client server (web) based application.

First let us see how to define a custom paper size.
Go to Start – settings – Printers to open Windows Printer Folder.
If you have not already installed a printer do it now.
Now go to File Menu and select Server Properties to open Print Server Properties


Now check Create a New Form.
Specify Paper Width and Height usually in Inches.
Provide a Name to your Form which a custom paper size . This will be listed in your print option dialog box.
Now Save the Form clicking on the save button.
Now your custom paper size is ready.
Choosing Printer & Paper Size :-
Before generating Crystal report you should select the printer and paper size programmatically if your application is hosted on a system which has more than one printer installed.
The following is code for the Printer & Paper initialization.










ReportDocument rptDoc = new ReportDocument();
rptDoc.Load(Server.MapPath("yourcrystalreport.rpt"));
CRT.ReportSource = rptDoc;
System.Drawing.Printing.PrintDocument doctoprint3 = new System.Drawing.Printing.PrintDocument();
doctoprint3.PrinterSettings.PrinterName = "yourprintername";
int i3 = 0;
for (i3 = 0; i3 <= doctoprint3.PrinterSettings.PaperSizes.Count - 1; i3++) { int rawKind3; if (doctoprint3.PrinterSettings.PaperSizes[i3].PaperName == "yourcustompapername") { rptDoc.PrintOptions.PaperSize = (CrystalDecisions.Shared.PaperSize)doctoprint3.PrinterSettings.PaperSizes[i3].RawKind; break; }//if }//for



VB.net 
cmd.CommandText = ("SELECT * FROM StockItem")
cmd.Connection = Con
con.Open()
DSRpt.Clear()
DA.SelectCommand = (cmd)
DA.Fill(DSRpt, cmd.CommandText)
DTRpt = DSRpt.Tables(0)
rptDoc = New ReportDocument
Dim rptPath As String = Application.StartupPath & "\CrystalReceipt.rpt"
rptDoc.Load(rptPath)
rptDoc.SetDataSource(DTRpt)
frmRptViewer.Text = "Print Sales Order"
Con.Close()
frmRptViewer.CrystalReportViewer1.ReportSource = rptDoc
Dim doctoprint As New System.Drawing.Printing.PrintDocument()
doctoprint.PrinterSettings.PrinterName = "EPSON LQ-300+ /II ESC/P 2" '"EPSON LQ-300+II ESC/P2" '(ex. "Epson SQ-1170 ESC/P 2")
For i = 0 To doctoprint.PrinterSettings.PaperSizes.Count - 1
   Dim rawKind As Integer
   If doctoprint.PrinterSettings.PaperSizes(i).PaperName = "A6 LR" Then
      rawKind = CInt(doctoprint.PrinterSettings.PaperSizes(i).GetType().GetField("kind", Reflection.BindingFlags.Instance Or Reflection.BindingFlags.NonPublic).GetValue(doctoprint.PrinterSettings.PaperSizes(i)))
      rptDoc.PrintOptions.PaperSize = rawKind
      Exit For
   End If
Next
frmRptViewer.ShowDialog()
frmRptViewer.Dispose()
rptDoc = Nothing

No comments:

Post a Comment