在 crystal 报告中以编程方式设置页边距

set page margins programmatically in crystal report

我正在使用 crystal 报告作为我的一份报告。我需要为报告动态设置边距。边距由用户设置,因此我需要以编程方式应用边距。

我正在使用以下代码以编程方式设置边距。

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

然后向用户显示打印预览,然后用户可以打印。我正在使用下面的代码来显示预览。

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

以上内容对我不起作用。它不应用边距(当我从 Design -> Page Setup 静态设置边距时同样有效)。它显示的内容就像在动态使用时不应用边距一样。我附上了预览图的显示方式。

任何人都可以帮助我解决问题吗?为什么不应用边距?

我找到了这个问题的解决方案。我之前使用的代码只有一点点变化。我使用了下面的代码。

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);

以上内容运行良好。我们只需要获取报告文档页边距并在那里设置边距,而不是重新初始化 PageMargins 对象。