Customize behaviors to open PDF files

IAPMiniProgram SDK by default only can open regular PDF files and cannot open encrypted PDF files. In order to support super app in opening encrypted PDF files, the SDK provides the ability to customize the behavior to open a PDF file per demands.

Procedure

Step 1. Implement the Activity for loading PDF files

This code implements the functionality of a customized PDF viewer activity. It extends the Activity class and overrides the onCreate method. The code retrieves the file type and file path from the intent extras. It then checks whether the file exists and calls the showPdf method to load and display the PDF document. The logic for loading and displaying the PDF document is to be implemented in the showPdf method.

You can take the SDK UI illustration after loading a PDF file as references.

copy
public class CustomPdfViewerActivity extends Activity {

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_custom_pdf_viewer);

        String fileType = getIntent().getStringExtra("fileType");
        String filePath = getIntent().getStringExtra("filePath");
        File file = new File(filePath);
        if (file.exists()) {
            showPdf(file, null);
        }
    }

    private void showPdf(File file, String password) {
        // Implement the logic of loading PDF documents and display them here
    }

}

Step 2. Implement the GriverOpenDocumentExtension interface

This code implements the functionality of a customized document opener extension. It implements the GriverOpenDocumentExtension interface and overrides the openDocument method. The method creates an intent to launch the CustomPdfViewerActivity and passes the file type and file path as extras. The activity is then started using startActivity.

copy
public class CustomGriverOpenDocumentExtensionImpl implements GriverOpenDocumentExtension {

    @Override
    public void openDocument(Activity activity, String fileType, String filePath) {
        // 
        Intent intent = new Intent();
        intent.setClass(activity, CustomPdfViewerActivity.class);
        intent.putExtra("fileType", fileType);
        intent.putExtra("filePath", filePath);
        activity.startActivity(intent);
    }
}

Step 3. Register the CustomGriverOpenDocumentExtensionImpl class

After initialize the SDK, call the registerExtension API to register theCustomGriverOpenDocumentExtensionImpl class.

copy
IAPConnect.init(application,initConfig,new InitCallback() {
    public void onSuccess() {
        //ยทยทยท
        Griver.registerExtension(new GriverExtensionManifest(
                                GriverOpenDocumentExtension.class, new CustomGriverOpenDocumentExtensionImpl()));
    }
});

Interfaces

GriverOpenDocumentExtension interface

The GriverOpenDocumentExtension interface is used to customize the behavior to open a PDF file, such as an encrypted PDF file. The following code shows the definition of this interface:

copy
public interface GriverOpenDocumentExtension extends GriverExtension {
    void openDocument(Activity activity, String fileType, String filePath);
}

Based on the definition, this interface provides the following methods:

Method

Description

Required

openDocument

The method that is called for SDK to open PDF documents.

Yes

openDocument method

The openDocument method has the following input parameters:

Parameter

Data type

Description

Required

activity

Activity

Indicates the context of Activity

Yes

fileType

String

Indicates the type of file that can be pdf, doc, docx, xls, xlsx, ppt, or pptx.

Yes

filePath

String

Indicates the file storage directory.

Yes

Appendices

UI illustration after loading a PDF file

When you customize the Activity for loading PDF files, you can refer to the default UI illustration of the SDK, which includes the following parts:

Load PDF files successfully

Failed to load PDF files

image.png

image.png