Friday, September 28, 2012

Programatically Set Custom PaperSize in CrystalReport

Programatically Set Custom 
PaperSize 
in CrystalReport

Hi,
     For one of our clients we are using crystal report  developed using Visual Studio 2005. At the deployment phase we often come across the problem of Setting Custom PaperSize in report. Our client is using custom page size of 11in x 6in , whenever the report preview is shown the report viewer automatically sets the size to A4. So the user can't able to view or print the report. So i searched  a lot to set paper size programatically. At last i found the following solution ,

  i)  First i have created Custom PageSize using ServerProperties (ie.,11in x 6in)  and named it as 11x6.

  ii) Then added the following method to my code,

  public Int32 GetPaperSize(String sPrinterName, String sPaperSizeName)
        {
            PrintDocument docPrintDoc = new PrintDocument();
            docPrintDoc.PrinterSettings.PrinterName = sPrinterName;
            for (int i = 0; i < docPrintDoc .PrinterSettings.PaperSizes.Count; i++)
            {
                int raw = docPrintDoc.PrinterSettings.PaperSizes[i].RawKind;           
                if (docPrintDoc.PrinterSettings.PaperSizes[i].PaperName == sPaperSizeName)
                {
                    return raw;
                }
            }
            return 0;
        }

  iii) Then i called this method  for the current Crystal Report Document, In my case 
CrystalReport11 is the Report Document



                this.CrystalReport11.PrintOptions.PrinterName = "Wipro LQ 1050+DX
";

                this.CrystalReport11.PrintOptions.PaperSize = (CrystalDecisions.Shared.PaperSize)GetPapersizeID("Wipro LQ 1050+DX
", "11x6";