Aditya Bokade
Aditya Bokade

Reputation: 1786

Crystal Reports margins are not changing at run time in Visual Studio 2010

I have Visual Studio 2010 Enterprise.

Here is my code that works perfectly for all other things but it does not change margins at runtime. Even if the value of objRpt.PrintOptions.PageMargins.leftMargin, rightmargin, etc. are being changed, it doesn't reflect in CrystalReportViewer nor in Hardcopy output.

Here is the code:

CrystalReport1 objRpt;
objRpt = new CrystalReport1();
adepter.Fill(Ds, "Customer");
objRpt.SetDataSource(Ds);
MessageBox.Show("Left margin:"+objRpt.PrintOptions.PageMargins.leftMargin.ToString()); //Here it shows 1440 

objRpt.PrintOptions.PrinterName = "HP LaserJet 1020";
PageMargins margins = objRpt.PrintOptions.PageMargins;

margins.bottomMargin = 350;
margins.leftMargin = 350;
margins.rightMargin = 350;
margins.topMargin = 350;
objRpt.PrintOptions.ApplyPageMargins(margins);
MessageBox.Show("Left margin:"+objRpt.PrintOptions.PageMargins.leftMargin.ToString()); 

//Here it shows 350 BUT no use.

I managed to set the margins at design time by right clicking on Report>Design>Page Setup. If I set here, margins work perfectly and they reflect in both reportviewer and hardcopy. But this does not happen when I use above code.

Upvotes: 1

Views: 3077

Answers (1)

Joel B
Joel B

Reputation: 882

From doing a bit of digging, it seems as though Crystal Reports margin settings are overwritten by the default printer settings when using the Crystal Reports Viewer (at least for the Crystal Reports Viewer in a windows form and clicking the Print button on the top of the report viewer). A possible workaround for this is to use your own print button and call PrintToPrinter, which respects your own margin settings. Other workarounds include: - Editing the report with the necessary blank space to imitate a margin - Intercept the printing call and adjust the margins then (I can't remember the name of the function call off the top of my head) - Set the default printer for the report to "No Printer". When selecting a printer from the Report Viewer, it should then apply the default printer settings then.

I ran into this issue when working on an application to print out ID cards. Calling PrintToPrinter() would not force a margin in the cards, which what was wanted. Running it through the Report Viewer, and clicking the print button there would force it to use the printer's default settings - with a margin of 0.17" or so. The result was cards that wouldn't print properly.

Upvotes: 1

Related Questions