How to move fields from a section in ActiveReports 6.0 report to new page if page width is less than the total width of fields to print?

StackOverflow https://stackoverflow.com/questions/9125972

Pergunta

I've a ActiveReports 6.0 report to which I'm adding fields to be displayed at run-time. These fields and the data to be displayed is coming from a DataGridView.

The problem is that when total width of the fields to be displayed goes higher than the width of the page on which it is to be printed e.g. A4, then the fields continue on the next physical page and it happens that they are printed partially on one page and rest on the new page.

I'm unable to find any solution so that i can move the fields to new page if the width can not be printed on the current page completely.

Example:

There is a DataGridView with 8 columns, each having a width of 250 pixels, totalling to 2000 pixels which is approx 21 inches for 96 DPI system. An A4 paper width is approx 8.25 inches.

Margins are
Left : 0.25 Inches
Right : 0.25 Inches
Top : 0.69 Inches
Bottom : 0.69 Inches

Initial 3 columns print on page 1. Column 4 prints partially on Page 1 and partially on Page 2.
I want that as column 4 can not be printed completely on page 1 then move it to Page 2 and it will be printed completely on page 2

Thanks in advance

Foi útil?

Solução

Horizontal page breaking is tricky indeed. I came up with the below function to deal with it in your case:

/// <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;
}

Below is a simple example of using the above function with 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);
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top