Crystal Report 2008 Hosting :: Customizing Papersize in Crystal Report

I have been using Crystal Report for so long, and it has been my partner in generating valuable reporting for my application projects. Crystal Reports provides its latest update from their website. I design my Reports to print in a non-standard paper size that is not available on paper size list of print options. The Report have  8.5 x 6.5 inches dimensions that is about half of a standard Long band paper size (8.5×13 inches) in a portrait Orientation, that should be 6.5 Inches in height and 8.5 Inches in width. This type of Printer preferences is a customized type and there should have lots of settings you should done before it will gonna work.

The Tweaks:

Windows allows you to create your own paper size for that matter, let it be known, the tweaks is that you can create a custom paper size by means of a “Printer’s Form” and it will automatically create the PaperID that the Crystal report use in its initial printer configuration before printing a document. To Create new form follow the procedure below:

  1. Go to Printer and Faxes. (Start->Settings->Printer and Faxes).
  2. Select your Printer.
  3. On File Menu Select File->Server Properties.
  4. Tick the “Create new Form” Check box.
  5. On the Form name Field, enter your desired Paper size Name, like for instance “CEPCRP”.
  6. On the Form Description (measurements).
  7. Enter the desired paper size, example: Width: 8.5 and Height: 6.5
  8. Enter the print area margins as well.
  9. and then click Save Form to create the form.

After you add the new paper size, this paper size will be available in windows paper size list.

The Code:

If you just configured your crystal report at design time and have successfully print it from the CR Designer, during run time this will not work for the reason that, CR does not save the paper size name but rather the equivalent paperid of that custom paper size we have created earlier, in order that the correct paperid corresponding to that we have created we need to create a routine  to iterate through system.drawing.printing.printdocument namespace to extract the paperid given only the paper size name and the printer name, this to insure that the paper size name is available on that specific printer in which we are about to print or else the default setting will be use that is not compatible with our crystal report settings. Below is the routine I created to iterate through the available paper size list.

Visual Basic.net Fragment Code:

 ''' <summary>
 ''' Gets the PaperSizeID from the Given PapersizeName.
 ''' </summary>
 ''' <returns></returns>
 ''' <remarks></remarks>
 Public Function GetPapersizeID(ByVal PrinterName As String, ByVal PaperSizeName As String) As Integer
     Dim doctoprint As New System.Drawing.Printing.PrintDocument()
     Dim PaperSizeID As Integer = 0
     Dim ppname As String = ""
     Dim s As String = ""
     doctoprint.PrinterSettings.PrinterName = PrinterName  '(ex. "Epson SQ-1170 ESC/P 2")
     For i As Integer = 0 To doctoprint.PrinterSettings.PaperSizes.Count - 1
         Dim rawKind As Integer
         ppname = PaperSizeName
         If doctoprint.PrinterSettings.PaperSizes(i).PaperName = ppname Then
            rawKind = CInt(doctoprint.PrinterSettings.PaperSizes(i).GetType().GetField("kind",             
Reflection.BindingFlags.Instance Or Reflection.BindingFlags.NonPublic).GetValue(doctoprint.PrinterSettings.PaperSizes(i)))
                PaperSizeID = rawKind
                Exit For
         End If
     Next
         Return PaperSizeID
End Function

And then you can this function before printing the document:

Dim repOptions As CrystalDecisions.CrystalReports.Engine.PrintOptions
With ReportDocument
    repOptions = .PrintOptions  
.Load(ReportPath, CrystalDecisions.Shared.OpenReportMethod.OpenReportByDefault)
    .SetDataSource(ds.Tables(0))
    With repOptions
          .PaperOrientation = CrystalDecision.Shared.PaperOrientation.Portrait
          .PaperSize = GetPapersizeID("EPSON LX-300+II", "CEPCRP")
          .PrinterName = "EPSON LX-300+II"              
    End With
          .Refresh()
          .PrintToPrinter(NumberOFCopies, Collated, StartPage, EndPage)
End With

These are just fragment of my codes to print a custom paper size with 8.5×6.5 inches dimension in a portrait orientation.

Comments are closed.

Sponsors

Advertise Here

Cheap Reseller Hosting

Best European ASP.NET 4.0 Hosting Provider

Best Blog Hosting Provider