Click here to Skip to main content
15,885,365 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi All,
I am using paper stationary of size 6in X 4 in. I have designed a report in Crystal Report and set Paper Size as 6in X 4in. When I open report in Web Browser, it displays report in A4 size only. So I decided to write code for setting Page Size and Printer Name using coding. Following is the code for the same.

ReportDocument rd = new ReportDocument();

CrystalDecisions.CrystalReports.Engine.PrintOptions REPOPTIONS;
REPOPTIONS = rd.PrintOptions;
           
REPOPTIONS.PaperOrientation=CrystalDecisions.Shared.PaperOrientation.Portrait;
          
REPOPTIONS.PaperSize = getpapersizeid("EpsonLX", "Receipts");
REPOPTIONS.PrinterName = "EpsonLX";
rd.Load(Server.MapPath("~\\DonationReciptdot.rpt"));
rd.Database.Tables[0].SetDataSource(rptDRdt);
Session["cryrpt"] = rd;
Response.Buffer = false;
Response.ClearContent();
Response.ClearHeaders();
Session["rpt"] = rd;
Response.Redirect("~/Receipts/ReportViw.aspx");


C#
protected int getpapersizeid(string printername, string papersizename)
    {

        System.Drawing.Printing.PrintDocument doctoprint=new System.Drawing.Printing.PrintDocument();
        int papersizeid = 0;
        string ppname = "";
        string s = "";


        doctoprint.PrinterSettings.PrinterName=printername;
        int i;
        for (i = 0; i < doctoprint.PrinterSettings.PaperSizes.Count - 1; i++)
        {
            int rawkind;
            ppname = papersizename;
            if (doctoprint.PrinterSettings.PaperSizes[i].ToString() == ppname)
            {
                //rawkind = convert.toint16(doctoprint.printersettings.papersizes(i));
                rawkind = convert.toint16(doctoprint.PrinterSettings.PaperSizes[i].GetType().GetField("kind", System.Reflection.BindingFlags.Instance || System.Reflection.BindingFlags.NonPublic).getvalue(doctoprint.PrinterSettings.PaperSizes[i]));

                papersizeid = rawkind;
                break;
            }
        }
        return papersizeid;
    }


Above code set Paper Size as Receipts which is of size 6in X 4in. (I have created custom page with name Receipts using Server Properties in Devices in Printers option) and EpsonLX is my shared printer name.

Now above code gives me following errors.

Error 5 Cannot implicitly convert type 'int' to 'CrystalDecisions.Shared.PaperSize'. An explicit conversion exists (are you missing a cast?) E:\RamnathTest2\MeargeRandF\Donation.aspx.cs 639 35 E:\RamnathTest2\MeargeRandF\


Error 10 Operator '||' cannot be applied to operands of type 'System.Reflection.BindingFlags' and 'System.Reflection.BindingFlags' E:\RamnathTest2\MeargeRandF\Donation.aspx.cs 1187 111 E:\RamnathTest2\MeargeRandF\

Tryint to resolve but unable to. Kindly help.

If anyone has other easier solutions, please reply back.

Thanks.
Posted
Updated 17-Mar-14 2:47am
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900