Posts tagged ‘Set Custom paper size in crystal report’

Set custom paper size in crystal report on web server in asp.net c#

Problem :

When developer set custom paper size in crystal report on development machine, it will  work. But when developer deploys project setup on web server, report generated by web system will be in default paper size of printer located on web server. So How to bind custom paper size with crystal report on web server.

What exactly happening during report generation

Crystal Reports picks up the paper size from the printer that developer has the report bound to.  So the paper size will first need to be available from the printer setup in Windows server.  In Windows XP or web server,  go to Start|Printers and Faxes and make sure that the printer you are using has the custom paper size available.

Now for the embedded report designer in Visual Studio .NET, you would right click on the report surface to bring up the context menu and select Designer|Page Setup.  From here select the appropriate printer and paper size.

Above thing is possible on development machine where complete setup of visual studio is installed. But on Live web server, it is not possible to bind printer to crystal report at design time.

If you change the printer name of a report, it will screw your configuration & custom paper size setup.

Solution :

Step : 1

First add your custom paper size on web server using server properties, Create a new form and save with new name.

Step : 2

Add below code in your project page where report will generate pdf file.

System.Drawing.Printing.PrintDocument doctoprint = new System.Drawing.Printing.PrintDocument();
doctoprint.PrinterSettings.PrinterName=”Microsoft XPS Document Writer”;
int i=0;
for (i = 0; i <= doctoprint.PrinterSettings.PaperSizes.Count; i++)
{
int rawKind = 0;
if (doctoprint.PrinterSettings.PaperSizes[i].PaperName == “CHEQUEPRINT”)
{
rawKind = Convert.ToInt32(doctoprint.PrinterSettings.PaperSizes[i].GetType().GetField(“kind”, System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic).GetValue(doctoprint.PrinterSettings.PaperSizes[i]));
rd.PrintOptions.PaperSize = (CrystalDecisions.Shared.PaperSize)rawKind;
break; 
}
}

rd.ExportToDisk(CrystalDecisions.Shared.ExportFormatType.PortableDocFormat, filename);
rep_url = objconfig.GetVirRptPath;
General.OpenWindow(rep_url + rep_file, rep_file.Substring(0, rep_file.Length – 4), this.Page);

In above code example, I have created new custom paper size, named “CHEQUEPRINT” having paper size width : 4.13 inch and height : 8.40 inch.

The fact is that crystal report save the information of the format of a report in a non-portable way!
It saves the “ID” of the format instead of his “Name”!
The problem is that in Windows this ID is machine dependent. So here I have fetched Papersize id and assigned to report document.