Printing ALV along with Page numbers

By Joyjit Ghosh, IBM India

Expected Layout of the ALV report (when printing):

Step 1:  Display each new document in a new page

To achieve this below routine is written to populate the IT_SORT table parameter of ALV function module REUSE_ALV_GRID_DISPLAY. And ALV will automatically insert page break whenever a new document is encountered.
CONSTANTS:     
c_spos        TYPE SLIS_SPOS   VALUE '01',     " Column position
c_up          TYPE SLIS_SOUP   VALUE 'X',      " Sorting order
c_table       TYPE tabname     VALUE 'I_FINAL'," Name of o/p table
c_group       TYPE SLIS_CTRLS  VALUE '*',      " Sorting group
c_fld2        TYPE fieldname   VALUE 'IDCNGA04-IDCN037'." Field name(Document no)
* Prepare sort table
  PERFORM prepare_sort_table CHANGING i_sort[].
FORM prepare_sort_table CHANGING pi_sort TYPE slis_t_sortinfo_alv.
  CLEAR st_sort.
  st_sort-spos      = c_spos.     " Sort sequence
  st_sort-fieldname = c_fld2.     " Document no
  st_sort-tabname   = c_table.    " Table name
  st_sort-up        = c_up.       " Ascending
  st_sort-group     = c_group.    " Group
* Populate sort table
  APPEND st_sort TO pi_sort.
ENDFORM.                    "prepare_sort_table

Step 2: Reset page number to 1 for each new document

From ALV function module we cannot control the page numbers as it will automatically increment the page counter whenever a new page is printed. So to achieve this we have written our custom code within the TOP_OF_PAGE routine. 

Register TOP_OF_PAGE routine with the TOP_OF_PAGE event in IT_EVENTS table parameter of the ALV function module.
* Prepare event table
  PERFORM prepare_event_table.
FORM prepare_event_table .
* Populate top-of-page event
  st_event-name = slis_ev_top_of_page.
  st_event-form = slis_ev_top_of_page.
  APPEND st_event TO i_event.
  CLEAR st_event.
ENDFORM.                    " prepare_event_table

 Custom code to control the page number in TOP_OF_PAGE routine 

Code block marked in bold are used to control the page number. As in our case we are using ALV grid to display the report so during report display page number is not visible. It is only visible in print preview or at the time of printing.

FORM top_of_page.
* Local variable declaration
  STATICS: l_comm  TYPE syucomm.  " Store user command
* Declaration of local variables
  DATA: l_line_size  TYPE sylinsz,           " Line size
        l_line       TYPE slis_listheader,   " Hold list header
        l_currdoc    TYPE idcn037,           " Current doc.
        l_currtabix  TYPE sytabix.           " Current index
* Check for print or print preview
  IF ( sy-ucomm = c_prin OR
  sy-ucomm = c_rnt_prev OR
  sy-ucomm = c_rnt ).
    IF l_comm <> sy-ucomm.
      CLEAR : g_page_cnt.
    ENDIF. " l_comm <> sy-ucomm
*   Store current table index
    l_currtabix  = sy-tabix.
    CLEAR st_final.
*   If current index is 1 then start page numbering from 1
    IF l_currtabix = 1.
*     Read 1st record and store the document no
      READ TABLE i_final INDEX l_currtabix INTO st_final.
      g_prevdoc = st_final-idcnga04-idcn037.
*     Start page numbering from 1
      g_page_cnt =  1.
      g_prevtabix = 1.
    ELSE.
*     Read the table line
      READ TABLE i_final INDEX l_currtabix INTO st_final.
*     Store the current document
      l_currdoc = st_final-idcnga04-idcn037.
*     If the current doc. is same as previou doc.
*     increament the page no, otherwise start it from 1
      IF l_currdoc = g_prevdoc.
*       Increament the page no
        g_page_cnt = g_page_cnt + 1.
      ELSE.
*       Start page from 1
        g_page_cnt =  1.
*       Store current doc. as previous doc.
        g_prevdoc = l_currdoc.
      ENDIF. " l_currdoc = g_prevdoc
    ENDIF. " l_currtabix = 1
  ENDIF. " sy-ucomm = c_prin OR
  IF g_page_cnt = 1.
*   Store the user command
    l_comm = sy-ucomm.
  ENDIF. " g_page_cnt = 1
* Display page no
  IF g_page_cnt > 0 .
*  Store the report width
    l_line_size = sy-linsz.
*  Calculate position
    l_line_size = l_line_size - 10.
*  Display page no
    WRITE AT l_line_size 'Page no:'(021).
*  Calculate position
    l_line_size = l_line_size + 5.
    WRITE AT l_line_size g_page_cnt.
  ENDIF. " g_page_cnt > 0
  REFRESH st_list_top_of_page.
* Populate company name
  CLEAR l_line.
  l_line-typ  = c_typ.
  l_line-info = g_comp_name.
  APPEND l_line TO st_list_top_of_page.
* Populate heading
  CLEAR l_line.
  l_line-typ  = c_typ.
  l_line-info = text-020.
  APPEND l_line TO st_list_top_of_page.
* At the time of printing or print preview display additional info
* on 1st page
  IF g_page_cnt = 1 AND
    ( sy-ucomm = c_prin OR
      sy-ucomm = c_rnt_prev OR
      sy-ucomm = c_rnt ).
    CLEAR l_line.
    l_line-typ  = 'A'.
*   Populate Staff name
    CONCATENATE text-017
                st_final-idcnga04-idcn053
                INTO l_line-info
                SEPARATED BY ':'.
    APPEND l_line TO st_list_top_of_page.
    CLEAR l_line.
    l_line-typ  = 'A'.
*   Populate Accountant
    CONCATENATE text-018
                '______________________'
                INTO l_line-info
                SEPARATED BY ':'.
    APPEND l_line TO st_list_top_of_page.
    CLEAR l_line.
    l_line-typ  = 'A'.
*   Populate Book keeper
    CONCATENATE text-019
                '______________________'
                INTO l_line-info
                SEPARATED BY ':'.
    APPEND l_line TO st_list_top_of_page.
    l_line-typ  = 'A'.
    l_line-info = space.
    APPEND l_line TO st_list_top_of_page.
  ENDIF.
* Display list header
  CALL FUNCTION 'REUSE_ALV_COMMENTARY_WRITE'
    EXPORTING
      i_logo             = space
      it_list_commentary = st_list_top_of_page.
* No sy-subrc check is required
ENDFORM.                    " top_of_page

Step 3: Display report

Display the ALV report using 'REUSE_ALV_GRID_DISPLAY' function module.
  CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
    EXPORTING
      i_callback_program      = l_prog         " Call back progrm
      it_fieldcat             = i_fieldcat_msg " Field catalog
      is_layout               = st_layout      " Layout
      it_sort                 = i_sort[]       " Sort table
      i_save                  = c_save         " 'A'
      is_variant              = l_variant      " ALV variant
      it_events               = i_event        " ALV events
      is_print                = st_print       " Print parameters
    TABLES
      t_outtab                = i_final        " Output table
    EXCEPTIONS
      program_error           = 1
      OTHERS                  = 2.

Result:

Report is displayed in ALV grid. Note that no page number is displayed. Now press the print preview button

Report is displayed in print preview with page numbers.

Limitations: 

  1. For positioning the page number in the report we have to calculate the position based on the list width. As in this case we are using the formula

Page number position = sy-linsz – 10. But if you want you can change it. 

  1. In print preview only 1st page number is visible for every document i.e. if a document spreading multiple pages only the 1st page number will be visible. You can see all the page numbers at the time of actual printing.
 

Please send us your feedback/suggestions at webmaster@SAPTechnical.COM 

HomeContribute About Us Privacy Terms Of Use • Disclaimer • SafeCompanies: Advertise on SAPTechnical.COM | Post JobContact Us  

Graphic Design by Round the Bend Wizards

footer image footer image