DImuthuUpe / AndroidPdfViewer

Android view for displaying PDFs rendered with PdfiumAndroid
Apache License 2.0
8.18k stars 1.91k forks source link

loadComplete didn't called #981

Open NinuuX opened 3 years ago

NinuuX commented 3 years ago

I'm trying to get the ToC for using it within my app, but the loadComplete never fired.. Can someone help me plz ?

@Override
    protected void onCreate(Bundle savedInstanceState)  {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_tfila_shower);
        AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);

        pDFView = (PDFView) findViewById(R.id.pdfView);

        pDFView.setMinZoom(2);

        try {
            pDFView.fromBytes( decrypt(buttonId, mac_address) )
                    .pageFitPolicy(FitPolicy.WIDTH)
                    // .enableSwipe(true)
                    // .swipeHorizontal(true)
                    .fitEachPage(true)
                    .enableDoubletap(false)
                    .enableAntialiasing(true) // improve rendering a little bit on low-res screens
                    // spacing between pages in dp. To define spacing color, set view background
                    .spacing(0)
                    .autoSpacing(false)
                    .nightMode(false)
                    .load();

        } catch (IOException e) {
            e.printStackTrace();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (NoSuchPaddingException e) {
            e.printStackTrace();
        } catch (InvalidKeyException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void loadComplete(int nbPages) {
        PdfDocument.Meta meta = pDFView.getDocumentMeta(); // ?
        Toast.makeText(getApplicationContext(), "Load Complete !", Toast.LENGTH_SHORT).show();

        printBookmarksTree(pDFView.getTableOfContents(), "-");
    }
    [...]
    public void printBookmarksTree(List<PdfDocument.Bookmark> tree, String sep) {
        for (PdfDocument.Bookmark b : tree) {

            Log.w("ToC: ", String.format("%s %s, p %d", sep, b.getTitle(), b.getPageIdx()));

            if (b.hasChildren()) {
                printBookmarksTree(b.getChildren(), sep + "-");
            }
        }
    }
sanxy commented 3 years ago

Did you check the sample? You need to implement OnLoadCompleteListener in your activity then add .onLoad(this). Implement the callback like shown below:

public class PDFViewActivity extends AppCompatActivity implements OnLoadCompleteListener { ...... ...... }

change your code for pdf view to the code below: pDFView.fromBytes( decrypt(buttonId, mac_address) ) .pageFitPolicy(FitPolicy.WIDTH) .fitEachPage(true) .enableDoubletap(false) .enableAntialiasing(true) .spacing(0) .autoSpacing(false) .nightMode(false) .onLoad(this) .load();

That is all that is need to have loadComplete callback work perfectly.