Back to Blog

    Developer Blog

    How do I open a PDF document from a specified PDF file path using PDF SDK for Android?

    Foxit PDF SDK for Android

    Foxit PDF SDK for Android provides multiple interfaces to open a PDF document. You can open a PDF document from a specified PDF file path, or from a memory buffer. For from a specified PDF file path, there are two ways to do that.

    The first one is that just use the openDoc interface, which includes the operations of creating a PDF document object (PDFDoc(String path)), loading the document content (load), and setting the PDF document object to view control (setDoc). Following is the sample code:

    Note: The openDoc interface is only available for opening a PDF document from a file path. If you want to customize to load a PDF document, you can implement it in the callback function (FileRead), and then create a document object with a FireRead instance using PDFDoc(FileRead fileRead). Next, also load the document content using load, and set the PDF document object to view control using setDoc.

    // Assuming A PDFViewCtrl has been created.
    
    // Open an unencrypted PDF document from a specified PDF file path.
    String path = "/mnt/sdcard/input_files/Sample.pdf";
    pdfViewCtrl.openDoc(path, null);
    

    The second one is that use the createFromFilePath interface to create a PDF document object, use load interface to load the document content, and then use setDoc to set the PDF document object to view control. Following is the sample code:

    // Assuming A PDFViewCtrl has been created.
    
    String path = "/mnt/sdcard/input_files/Sample.pdf";
    pdfViewCtrl.openDoc(document);
    

    The second one is that use the PDFDoc(String path) interface to create a PDF document object , use load interface to load the document content, and then use setDoc to set the PDF document object to view control. Following is the sample code:

    // Assuming A PDFViewCtrl has been created.
    
    String path = "/mnt/sdcard/input_files/Sample.pdf";
    try {
    
    // Initialize a PDFDoc object with the path to the PDF file.
    PDFDoc document = new PDFDoc(path);
    
    // Load the unencrypted document content.
    document.load(null);
    
    // Set the document to view control.
    pdfViewCtrl.setDoc(document);
    
    } catch (Exception e) {
    
    // TODO Auto-generated catch block 
    e.printStackTrace();
    
    }