PdfViewPager icon indicating copy to clipboard operation
PdfViewPager copied to clipboard

How to show a page number on every page in a PDF?

Open Shivansh8077 opened this issue 4 years ago • 2 comments

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();
    }
    
}

The PDF renders perfectly form my device but I cannot see the page numbers, title , total pages of the PDF when the PDF is loaded.

This is my XML file

<?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>

How do I show the page number on the pdf, now I know the function to calculate the total number of pages but how do I show it when the pdf is loaded.

Shivansh8077 avatar May 14 '21 00:05 Shivansh8077

PdfViewPager doesn't do this by itself, but you can draw a TextView on top of the PDFViewPager and show current page number on it, and it will very likely work well

voghDev avatar May 14 '21 07:05 voghDev

That did the Job perfectly. I have another question and that is whether changing the background colour of the rendered PDF is possible or not and by background I mean the remaining space between the pdf and the screen ?

Shivansh8077 avatar May 14 '21 23:05 Shivansh8077

I'm afraid it is not possible to change the background color of the Pdf document

voghDev avatar Jan 03 '23 06:01 voghDev