Question

I'm trying to programmatically set the margins in an active report based on the page number.

Specifically, the first page needs to have small margins (so that the top-most text box with a return address matches the alignment of a company logo) and every page after that should have standard 2.54cm margins.

I've read posts that suggest that detecting the actual page number can be problematic so I've tried using the ReportStart and PageStart handlers along with some very simple logic to set the margins.

In the code-behind for the report, I added the two handlers and bool value:

 this.ReportStart += UFAnReportStart;
        this.PageStart += UFAnPageStart;
 bool bFirstPage = true;

And then added the two callbacks as follows:

private void UFAnReportStart(object sender, System.EventArgs eArgs)
    {           
        this.PageSettings.Margins.Top = 0.1965278F;
    }

private void UFAnPageStart(object sender, System.EventArgs eArgs)
    {
        // every page after the first should have standard margins.
        if (!bFirstPage)
        {               
            this.PageSettings.Margins.Top = 2.54F;
        }
        bFirstPage = false;
    }

This doesn't seem to have any effect on the margins. Is this approach just plain wrong? Or is the PageSettings object a report wide property?

Any suggestions for alternative approaches are welcomed.

using Activereports3, version 5.2.1013.2.

Thank you!

Was it helpful?

Solution

Programmatically the units are in inches not CM :) The design-time setting only affects what is shown in the designer.

The following worked for me:

public void ActiveReport_ReportStart()
{
    rpt.PageSettings.Margins.Top = 0.05f;
    rpt.PageSettings.Margins.Left = 0.05f;
    rpt.PageSettings.Margins.Right = 0.05f;
    rpt.PageSettings.Margins.Bottom = 0.05f;    
}

public void ActiveReport_PageEnd()
{
    // The first page (page index 0) will inherit the page margins set in ReportStart. 
    // We immediately reset the page margins in the first PageEnd event to ensure subsequent pages get the larger margins.
    if (rpt.Document.Pages.Count == 0)
    {
        rpt.PageSettings.Margins.Top = 1.0f;
        rpt.PageSettings.Margins.Left = 1.0f;
        rpt.PageSettings.Margins.Right = 1.0f;
        rpt.PageSettings.Margins.Bottom = 1.0f;
    }
}

The ActiveReports Support Forums are free, active, and monitored by our support team so they're a great place to ask questions about ActiveReports.

Hope this helps,

Scott Willeke
GrapeCity inc.

OTHER TIPS

As it turns out, the approach you suggested DOES work! We render the document into both PDF and RTF. The margins are correct when viewed as a PDF in Adobe Reader, but don't show when viewd as an RTF in Word 2010; something I had failed to notice.

Ach, hours wasted! :P

This, of course, raises the question of why the margins don't show in the RTF-in-word version..?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top