android 如何使用voghDev/PdfViewPager库在PDF中的每一页上显示页码?

tf7tbtn2  于 2023-03-27  发布在  Android
关注(0)|答案(3)|浏览(311)

我已经研究了好几天了,但我还是找不到办法。
这里是Github上的库的链接。Library

public class PDFViewActivity1 extends AppCompatActivity {
    LinearLayout root;
    EditText etPdfUrl;
    Button btnDownload;
    PDFPagerAdapter adapter;
    String id;
    ArrayList<String> topicLink;
    File file;
    String filePath;

    PDFViewPager pdfViewPager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_SECURE, WindowManager.LayoutParams.FLAG_SECURE);

        setContentView(R.layout.activity_p_d_f_view);

        root = (LinearLayout) findViewById(R.id.remote_pdf_root);
        final Context ctx = this;

        Intent intent = getIntent();
        String str = intent.getStringExtra("filePath");
        str = str;   // This contains the path of the PDF on my device
        Log.i("filePathInString", str);

        pdfViewPager = new PDFViewPager(ctx, str);
        pdfViewPager.setId(R.id.pdfViewPager);
        pdfViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            }

            @Override
            public void onPageSelected(int position) {

            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
        
        adapter = new PDFPagerAdapter(ctx, str);
        pdfViewPager.setAdapter(adapter);
        
        root.removeAllViewsInLayout();
        root.addView(pdfViewPager,
                LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        ((PDFPagerAdapter) pdfViewPager.getAdapter()).close();
    }
    
}

PDF完美地呈现了我的设备,但我无法看到页码,标题,总页数的PDF时,PDF加载。
这是我的XML文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/remote_pdf_root"
    android:orientation="vertical"
    android:background="#FFFFFF"
    tools:context=".PDFViewActivity1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <es.voghdev.pdfviewpager.library.PDFViewPager
        android:id="@+id/pdfViewPager"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>

</LinearLayout>

当页面滚动时,我该怎么做,我不知道该怎么做,我尝试了很多PDFAdapter的方法,但都没有成功:(我该怎么做?)

icnyk63a

icnyk63a1#

RelativeLayout remotePdfRoot = findViewById(R.id.remote_pdf_root);
    
remotePDFViewPager = new RemotePDFViewPager(this, downloadFileUrlConnection, url, listener);
remotePDFViewPager.setId(R.id.pdfViewPager);
    
//after file loading success
PDFPagerAdapter adapter = new PDFPagerAdapter(this, FileUtil.extractFileNameFromURL(url));
remotePDFViewPager.setAdapter(adapter);
updateLayout();
    
private void updateLayout() {
    remotePdfRoot.removeAllViewsInLayout();
    remotePdfRoot.addView(remotePDFViewPager, LinearLayout.LayoutParams.MATCH_PARENT,
    LinearLayout.LayoutParams.WRAP_CONTENT);
}

我预览pdf这种方式,将有一个pagenumber。

cig3rfwq

cig3rfwq2#

您可以使用https://github.com/barteksc/AndroidPdfViewer库来实现此操作,并且可以通过以下方式获得页面计数:

`vpPdf.fromAsset("sample.pdf").onError {
        Log.i(TAG, "${it.localizedMessage}")
        it.printStackTrace()
     }.onPageError { _, t ->
        t.printStackTrace()
     }.enableSwipe(true).swipeHorizontal(true).onPageChange { page, pageCount ->
     //here page count is count of page user currently is viewing.
     }.load()`

我不得不在工作中使用这个库,它工作得非常好!!上面的代码来自Kotlin,但我想你可以将其转换为java,如果你不能转换它,请友好地评论,所以我也会帮助你!!

xa9qqrwz

xa9qqrwz3#

在Kotlin中尝试

override fun onSuccess(url: String?, destinationPath: String?) {

    adapter = PDFPagerAdapter(this, FileUtil.extractFileNameFromURL(url))
    remotePDFViewPager.adapter = adapter
    remotePDFViewPager.addOnPageChangeListener(object : OnPageChangeListener {
        override fun onPageScrolled(
            position: Int,
            positionOffset: Float,
            positionOffsetPixels: Int
        ) {
        }

        override fun onPageSelected(position: Int) {}
        override fun onPageScrollStateChanged(state: Int) {
            var totalPages = remotePDFViewPager.adapter?.count
            var currentPage = remotePDFViewPager.currentItem +1
            tvCurrentPage!!.text = currentPage.toString()+" / "+ totalPages
        }
    })

相关问题