set page margins programmatically in crystal report

坚强是说给别人听的谎言 提交于 2019-12-11 09:53:41

问题


I m using crystal report for one of my report. I need to set the margins dynamically for the report. The margins are being set by user so I need to apply the margins programmatically.

I am using the below code to set the margins programmatically.

ReportDocument rd = new ReportDocument();
PageMargins pageMargins = new PageMargins();
pageMargins.leftMargin = 25;
pageMargins.topMargin = 100;
pageMargins.rightMargin = 25;
pageMargins.bottomMargin = 50;
rd.PrintOptions.ApplyPageMargins(pageMargins);

and then show preview of print to the user and then user can print. I am using below code for showing preview.

Response.Buffer = false;
Response.ClearHeaders();
Response.ClearContent();
rd.ExportToHttpResponse(ExportFormatType.PortableDocFormat, System.Web.HttpContext.Current.Response, false, "Print");

Above is not working for me. It doesn't apply margins (The same is working when I set margins statically from Design -> Page Setup). It shows the content as if margins are not applied when used dynamically. I have attached the image for how it is being shown as preview.

Can anybody help me for what can be the problem? Why margins are not being applied?


回答1:


I found the solution for this problem. There is only a little change with the code I used earlier. I used the code below.

ReportDocument rd = new ReportDocument();
PageMargins margins;
// Get the PageMargins structure and set the 
// margins for the report.
margins = rd.PrintOptions.PageMargins;
margins.bottomMargin = 350;
margins.leftMargin = 600;
margins.rightMargin = 350;
margins.topMargin = 300;
// Apply the page margins.
rd.PrintOptions.ApplyPageMargins(margins);

So the above is working fine. We just need to get the report document page margins and set the margins there instead of new initialization of PageMargins object.



来源:https://stackoverflow.com/questions/31426112/set-page-margins-programmatically-in-crystal-report

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!