Sunday 23 November 2014

Print Functionality in Oracle ADF

To implement print functionality using Oracle ADF, we need to follow the below steps.
Approach -1:
Step-1:- Add <af:showPrintablePageBehavior/> component inside the button.

<af:commandButton text="Print1" id="cb1" binding="#{backingBeanScope.backing_PrintPage.cb1}">

<af:showPrintablePageBehavior/>

</af:commandButton>

Step-2:- Add before phase method inside view tag. Code show looks like the below


Step-3:- Add the below code inside backing/managed bean.

public void beforePhaseMethod(PhaseEvent phaseEvent) {

        if (phaseEvent.getPhaseId() == PhaseId.RENDER_RESPONSE) {

            FacesContext fctx = FacesContext.getCurrentInstance();

            Map requestMap = fctx.getExternalContext().getRequestMap();

            Object showPrintableBehavior = requestMap.get("oracle.adfinternal.view.faces.el.PrintablePage");

            if (showPrintableBehavior != null) {

                if (Boolean.TRUE == showPrintableBehavior) {

                    ExtendedRenderKitService erks = null;

                    erks = Service.getRenderKitService(fctx, ExtendedRenderKitService.class);

                    //invoke JavaScript from the server

                    erks.addScript(fctx, "window.print();");

                }

            }

        }

    }

NOTE:- if you don’t want to display some component in your print page then add
rendered="#{adfFacesContext.outputMode != 'printable'}" to that component.


Approach-2: Add the below Code on click of button 
public String printPage() {
        // Add event code here...
        FacesContext facesContext = FacesContext.getCurrentInstance();

        org.apache.myfaces.trinidad.render.ExtendedRenderKitService service =
                   org.apache.myfaces.trinidad.util.Service.getRenderKitService(facesContext,   ExtendedRenderKitService.class);

               service.addScript(facesContext, "window.print();");
        return null;
    }


In this approach-2 it will not open the new window with content but the same page the print window will open.

Thanks...

No comments:

Post a Comment