水平分页确实很棘手。在您的情况下,我想出了以下功能来处理它:
/// <summary>
/// Horizontally page breaks a control.
/// </summary>
/// <param name="requestedLeft">The requested left position of the control.</param>
/// <param name="controlWidth">The width of the control.</param>
/// <param name="paperWidth">The width of the target paper</param>
/// <param name="leftMargin">The width of the paper's left margin.</param>
/// <param name="rightMargin">The width of the paper's right margin.</param>
/// <returns>The new left position for the control. Will be requestedLeft or greater than requestedLeft.</returns>
public static float HorizontallyPageBreak(float requestedLeft, float controlWidth, float paperWidth, float leftMargin, float rightMargin)
{
var printArea = paperWidth - (leftMargin + rightMargin);
var requestedPageNum = (int) (requestedLeft/paperWidth);
// remove the margins so we can determine the correct target page
var left = (requestedLeft - ((leftMargin + rightMargin) * requestedPageNum));
var pageNum = (int)( left / printArea);
var leftOnPage = left % printArea;
if (leftOnPage + controlWidth > printArea)
{ // move it to the next page
left += printArea - leftOnPage;
left += rightMargin + leftMargin;
}
// add in all the prior page's margins
left += (leftMargin + rightMargin) * pageNum;
return left;
}
下面是在 ActiveReports 中使用上述函数的简单示例:
NewActiveReport1 rpt = new NewActiveReport1();
float controlWidth = 0.53f;
float nextControlLeft = 0f;
for (int controlCount = 0; controlCount < 1000; controlCount++)
{
var oldLeft = nextControlLeft;
controlWidth += 0.21f;
nextControlLeft = HorizontallyPageBreak(nextControlLeft, controlWidth, rpt.PageSettings.PaperWidth, rpt.PageSettings.Margins.Left, rpt.PageSettings.Margins.Right);
var txt = new DataDynamics.ActiveReports.TextBox();
txt.Text = "Column " + controlCount;
txt.Top = 0;
txt.Border.Color = Color.Black;
txt.Border.Style = BorderLineStyle.Solid;
txt.Left = nextControlLeft;
txt.Width = controlWidth;
rpt.Sections["detail"].Controls.Add(txt);
nextControlLeft += controlWidth;
rpt.PrintWidth = Math.Max(rpt.PrintWidth, nextControlLeft + controlWidth);
}
this.viewer1.Document = rpt.Document;
rpt.Run(true);