Tips   >   Reports   >   Dynamic Report Page Header

Dynamic Report Page Header

Ask yourself the following questions:

  1. Do the report page headers contains the same or very similar information on 80% of your reports?
  2. Do you find it redundant to add and position the same page header fields on 80% of the reports?

There is a relatively easy solution for this. Leave the page header blank in all your reports that use a standard page header and add the page header fields and text to each report instance using notation. Omnis Studio can add your standard page header fields to a report instance in the blink of an eye! The beauty of adding report page header fields on-the-fly is that if you want to make a change to the page header in your standard reports, you only have to change the code in one place and presto, all of your standard reports will immediately reflect the change.

As with anything in Omnis Studio there are many ways to accomplish the above. For this solution we are going to structure the code as follows:

  1. The object class oReportTools contains the code which adds fields to the report instance page header.
  2. The report class must contain an invisible linemarker field to indicate the page header line where a field is to be added. If the linemarker does not exist, the field will not be added.
  3. The report class rPageHeader_template contains the template fields which will be copied to the report instance by oReportTools. Using a template report class makes it easy for the developer to modify any of the page header fields.

The sequence of event for this solution is as follows:

  1. The target report instance is opened.
  2. Omnis Studio sends a $construct message to the report instance.
  3. The $construct method of the report sends a $constructPageHeader($cinst) message to oReportTools.
  4. The $constructPageHeader method find the rPageHeader_template report class and then searches for the appropriate invisible linemarker fields in the target report instance and copies the corresponding field from the template report class to the specified line on the target report instance.
Instructions and code for the above solution are included with this tip.

Page Header Template

To make it easy for you to edit the fields for your standard page header we will create a page header template report class.

  1. Create a new report class.
  2. Name the report class rPageHeader_template
  3. Add a Page Header section to the report.
  4. Add each of the standard page header fields you use on your report page headers. Name each field with name that is easy to read and understand. e.g. PageCount, Title, SubTitle, PrintInfo, PageHeaderNotes.
Click the View Report Class button in the StudioTips Browser to see the rPageHeader_template report class. Free free to copy the report class from StudioTips to your library.

Report Tools Object Class

The report tools object class contains the code which adds the template fields to the report instance.

  1. Create a new object class.
  2. Name the object class oReportTools
  3. Add a $constructPageHeader method to the object class with the following code.

    ; $constructPageHeader (method)

    ; Figure out the page report width.
    If pfrReportInst.$orientation=kOrientPortrait
       Calculate Width as (pfrReportInst.$paperwidth-pfrReportInst.$leftmargin-pfrReportInst.$rightmargin)*100/pfrReportInst.$scale
    Else
       Calculate Width as (pfrReportInst.$paperlength-pfrReportInst.$leftmargin-pfrReportInst.$rightmargin)*100/pfrReportInst.$scale
    End If

    ; Find the page header template report class.
    Do $clib.$classes.$findname('rPageHeader_template') Returns irTemplateRpt
    If irTemplateRpt
       
       ; Print Info
       Calculate FieldName as 'PrintInfo'
       Do method addTemplateField (pfrReportInst,FieldName) Returns rField
       If not(isnull(rField))
          
          Do rField.$left.$assign(0)
          
          ; Page Count
          Calculate FieldName as 'PageCount'
          Do method addTemplateField (pfrReportInst,FieldName) Returns rField
          If not(isnull(rField))
             
             Do rField.$left.$assign(Width-rField.$width)
             
             ; Company Name
             Calculate FieldName as 'CompanyName'
             Do method addTemplateField (pfrReportInst,FieldName) Returns rField
             If not(isnull(rField))
                
                Do rField.$left.$assign(0)
                Do rField.$width.$assign(Width)
                
                ; Title
                Calculate FieldName as 'Title'
                Do method addTemplateField (pfrReportInst,FieldName) Returns rField
                If not(isnull(rField))
                   
                   Do rField.$left.$assign(0)
                   Do rField.$width.$assign(Width)
                   
                   ; SubTitle
                   Calculate FieldName as 'SubTitle'
                   Do method addTemplateField (pfrReportInst,FieldName) Returns rField
                   If not(isnull(rField))
                      
                      Do rField.$left.$assign(0)
                      Do rField.$width.$assign(Width)
                      
                      ; Page header notes
                      Calculate FieldName as 'PageHeaderNotes'
                      Do method addTemplateField (pfrReportInst,FieldName) Returns rField
                      If not(isnull(rField))
                         
                         Do rField.$left.$assign(0)
                         Do rField.$width.$assign(Width)
                         
                         Calculate FlagOK as kTrue
                         
                      End If
                   End If
                End If
             End If
          End If
       End If
    End If
    Quit method FlagOK

  4. Add an addTemplateField private method to the object class with the following code.

    ; addTemplateField (private method)

    If len(pLineMarkerName_opt)=0
       Calculate pLineMarkerName_opt as con(pFieldName,'_linemarker')
    End If

    ; Find the linemarker
    Do pfrReportInst.$objs.$findname(pLineMarkerName_opt) Returns rMarker
    If rMarker
       
       ; Find the template object.
       Do irTemplateRpt.$objs.$findname(pFieldName) Returns rSourceField
       If rSourceField
          
          ; Add the field to the report.
          ; $add(type[,cComponentLibrary,cComponentControl],iTop,iLeft,iHeight,iWidth) inserts a new object and returns an item reference to it
          If rSourceField.$objtype=kComponent
             
             Calculate Lib as rSourceField.$componentlib
             Calculate Control as rSourceField.$componentctrl
             Do pfrReportInst.$objs.$add(kComponent,Lib,Control) Returns rField
             If rField
                
                Do rField.$name.$assign(rSourceField.$name)
                Do rField.$fieldstyle.$assign(rSourceField.$fieldstyle)
                
                Do rField.$lineno.$assign(rMarker.$lineno)
                Do rField.$left.$assign(rSourceField.$left)
                Do rField.$width.$assign(rSourceField.$width)
                Do rField.$height.$assign(rSourceField.$height)
                Do rField.$align.$assign(rSourceField.$height)
                
             End If
             
          Else
             Do pfrReportInst.$objs.$add(kEntry) Returns rField
             If rField
                
                ; Copy all of the source field attributes to the target added field.
                Calculate rField as rSourceField
                
                Do rField.$fieldstyle.$assign(rSourceField.$fieldstyle)
                Do rField.$lineno.$assign(rMarker.$lineno)
                
             End If
             
          End If
       End If
    End If
    Quit method rField

Click the View Object Class button in the StudioTips Browser to see the oReportTools report class. Free free to copy the object class from StudioTips to your library.

Note

The oReportTools object class in StudioTips includes a $setHorzLines method which can be called to dynamically position and stretch the horizontal lines across the report paper width.

Dynamic Header Report Class

Each standard report class will need to include the page header lines and appropriate linemarker fields for each standard page header field you want to include in the report page header.

  1. Create a new report class.
  2. Name the report class whatever name is appropriate for the particular report.
  3. Add a Page Header section to the report.
  4. Add invisible linemarker fields off the left side of the report class page. Name each linemarker with template field name and the suffix _linemarker. e.g. PageCount_linemarker, Title_linemarker.
  5. Add the following code to the $construct method of the report class.

    ; Construct the standard page header.
    Do ioReportTools.$constructPageHeader($cinst) Returns FlagOK



    The ioReportTools instance variable must be pointed to the the oReportTools object class.
Click the View Report Class button in the StudioTips Browser to see the rDynamicPageHeader report class. Free free to copy the report class from StudioTips to your library as a starting point for your report classes.