简体   繁体   中英

How can I open a PDF in my Android app and allow the users to use the embedded hyperlinks?

i am making a pdf reader in android app through android studio, i am using pdfviewer library in my app. pdf view workes but it doesnt working with embedded hrperlinks in pdf. .

public class StudyActivity extends AppCompatActivity implements OnPageChangeListener,OnLoadCompleteListener {
private static final String TAG = MainActivity.class.getSimpleName();
public static final String SAMPLE_FILE = "XAT.pdf";
PDFView pdfView;
Integer pageNumber = 0;
String pdfFileName;

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


    pdfView= (PDFView)findViewById(R.id.pdfView);
    displayFromAsset(SAMPLE_FILE);
}

private void displayFromAsset(String assetFileName) {
    pdfFileName = assetFileName;

    pdfView.fromAsset(SAMPLE_FILE)
            .defaultPage(pageNumber)
            .enableSwipe(true)

            .swipeHorizontal(false)
            .onPageChange(this)
            .enableAnnotationRendering(true)
            .onLoad(this)
            .scrollHandle(new DefaultScrollHandle(this))
            .load();
}


@Override
public void onPageChanged(int page, int pageCount) {
    pageNumber = page;
    setTitle(String.format("%s %s / %s", pdfFileName, page + 1, pageCount));
}


@Override
public void loadComplete(int nbPages) {
    PdfDocument.Meta meta = pdfView.getDocumentMeta();
    printBookmarksTree(pdfView.getTableOfContents(), "-");

}

public void printBookmarksTree(List<PdfDocument.Bookmark> tree, String sep) {
    for (PdfDocument.Bookmark b : tree) {

        Log.e(TAG, String.format("%s %s, p %d", sep, b.getTitle(), b.getPageIdx()));

        if (b.hasChildren()) {
            printBookmarksTree(b.getChildren(), sep + "-");
        }
    }
}



@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
    }


    }

xml file is //xml file

<TextView
    android:layout_width="match_parent"
    android:layout_height="40dp"
    android:background="@color/colorPrimaryDark"
    android:text="View PDF"
    android:textColor="#ffffff"
    android:id="@+id/tv_header"
    android:textSize="18dp"
    android:gravity="center"></TextView>

<com.github.barteksc.pdfviewer.PDFView
    android:id="@+id/pdfView"
    android:layout_below="@+id/tv_header"
    android:layout_width="match_parent"
    android:layout_height="match_parent"/>


</RelativeLayout>

in gradle

compile 'com.github.barteksc:android-pdf-viewer:2.0.3'
compile 'com.github.bumptech.glide:glide:3.5.2'

In barteksc's libary the linkHandler(DefaultLinkHandler()) method is available since version: 3.2.0 beta-1. In your code you just need to add this method while calling fromAsset:

 pdfView.fromAsset(SAMPLE_FILE)
        .linkHandler(DefaultLinkHandler(pdfView))
        .defaultPage(pageNumber)
        .enableSwipe(true)

Unfortunately it was invented too late for you: in 2019

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM