Monday, January 05, 2004


How to specify page size and paper orientation for PDF document in Crystal Reports ASP.NET

Crystal Reports: how to specify page size and paper orientation for PDF document


When developer asks Crystal Reports to print a document, Crystal Reports behaves in strange way: It selects one of the installed printers and starts to consider this printer as "default printer" (in spite that the printer is not a default printer).
As a result:
1) "oRpt.PrintToPrinter(,,,)" method by default prints to this "self-selected" printer.
2) "oRpt.Export()" method (which prepares PDF document from the Crystal Report) define page size in correspondence with settings of the "self-selected" printer.

What is "self-selected" printer: it is a printer which is installed in Windows, but not necessarily is specified in your Crystal Reports document.

In my case, all PDF documents were formatted by size of my "SATO Label Printer" (4 inches width (height)). In spite that I explicitly defined standard Laser Printer for my Crystal Report document.
Obviously this is a Crystal Report bug.

Solution: first step

In Crystal Reports designer set:
"Right mouse click" on report -> Designer -> Printer Setup -> Check "No printer".
"No printer" should be checked after report design is completed. It is important, because some fields could become unavailable after specifying "No printer".

"No printer" will allow to get rid of "self-selected" printer bug.
By default, standard "A4" paper with portrait orientation will be used.
In most cases this is a solution. But what if you want to use non-A4-size paper or want to print with "Landscape" orientation?
Let's see "second step" of the solution.

Solution: second step

Before you call "oRpt.Export()" --- explicitly specify printer's name, Paper orientation, and Paper size.
Use piece of my code as example:
oRpt.PrintOptions.PrinterName = @"\\FAXSERVER\GEN6FAXPR";
oRpt.PrintOptions.PaperOrientation = CrystalDecisions.Shared.PaperOrientation.Landscape;
oRpt.PrintOptions.PaperSize = CrystalDecisions.Shared.PaperSize.PaperLetter;
//oRpt.PrintToPrinter(1, false,0,0);
oRpt.Export(); // C:\Inetpub\wwwroot\Reports\ExportedReports should exist!
Note: This code will work only if you checked "No printer" checkbox in Crystal Report designer (see: "Solution: first step" above).

How I found solution:

My colleague gave me "first step" of the solution (about "no printer" checkbox).
It worked until I tried to prind "Landscape" document.
Then I spend half a day on Google and found this article:
landscape problems - preview vs. print (.NET)

Not everything in this article is up-to-date, but the article definitely has good idea: to specify Crystal Reports printing parameters explicitly.
So, I made this experiment and succeeded.

Comments: Post a Comment

<< Home

This page is powered by Blogger. Isn't yours?