views:

67

answers:

2

Hi SO!

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!

+1  A: 

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.
scott
Inches?... silly Americans! ;D. Thanks for the reply dude. I'm pretty sure I tried doing what you've suggested at an earlier point, but to no avail! Does it make a difference if it's rendered to RTF or PDF? In will be rendered to both in different places in our app....
Might it also be complicated by the fact that a text box that starts on the previous page continues over onto the 2nd... from what I've read, it seems that positioning of elements is absolute in some senses. Obviously, I don't really have any idea how active reports functions. :P
Ive tried your approach but it doesn't make any difference. The margins are the same on every page. This makes me sad. It really seems like it should work... in fact I suspect something else is preventing the margins from showing on the 2nd page. Also, Im making these changes in VS 2010... could that potentially cause problems for this older version of AR? The 'Report' menu isn't even accessible for one. :\
One further thing; the letter template I'm making changes to extends another report - i.e. the code behind has.... public class UFAn : LetterTemplate. This report has nothing in it except a PageHeader, PageFooter and Detail section. I cant actually the designer settings for this, due to the problems with VS2010 stated above, but in the code behind, all the elements are of zero height etc.... no margins set though. Does this change the situation?
A: 

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..?

I'm not sure why they don't work in RTF/Word. Maybe we don't pass them along to Word. They can probably fix that easily. Send an email to [email protected] with a sample and I they should help out. Feel free to reference this SO thread too.
scott