简体   繁体   中英

Open pdf always open document 1st page

I have a button that opens a local pdf manual like this

File file = storeFile(is, "user_manual.pdf");
Uri uri = Uri.fromFile(file);
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(uri, "application/pdf");
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);

This opens the document in Adobe Acrobat Reader that is installed and our setup guarantees that Acrobat Reader is always installed.

The problem is that the manual has 50 pages. If the user were to scroll down and hit back and select the user manual button, it takes us back to middle of the pdf which would be normally good but I have a specific requirement that it has to open the first page that has an index.

How do I open the document such that it always shows the first page of the pdf?

I have tried

intent.putExtra("page", 1); // parameter used on desktop Acrobat reader

and

intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

and

intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);

but it always opens the pdf the second time where the user left the first time.

Try to close the view on your opened file .pdf , when the user press the back button:

@Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
    //replaces the default 'Back' button action
    if(keyCode== KeyEvent.KEYCODE_BACK)
    {

        Intent intent = new Intent(PDFVIEW.this, MainActivity.class);
        startActivity(intent);
        finish();

    }
    return true;
}

I have two answers. If the first answer works, that would be great. If not, the second answer will certainly work with Adobe Reader, but it's more work.

Answer 1: Use an open parameter

Adobe has published a document entitled Parameters for Opening PDF Files and there's indeed a parameter named page . You use it like this:

intent.putExtra("page", 1);

Have you tried this:

intent.putExtra("page", "1");

Maybe the parameter needs to be passed as a string (although it's actually an integer).

Answer 2: introduce an open action

An open action allows you to trigger an action as soon as the document opens, eg you can execute some JavaScript the moment someone opens your document. In your case, you want to open the document on page one.

Adding an open action to a PDF document requires you to change the PDF document. You need some software that updates your PDF with this action. See How to set initial view properties? for a C# example. There are some Java examples here: iText 5 example / iText 7 example .

Mmmh,

I think you could use the AR command line options like:

(path to Adobe Reader) /A "page=100" "(Path To PDF file)"

or the AR activeX/ browser control:

"PDF browser controls are available through the AxAcroPDFLib.AxAcroPDF interface, which provides the following methods used to programmatically control the PDF document window: ●GoBackwardStack ●GoForwardStack ●GotoFirstPage ●GotoLastPage ....."

HTH, Reinhard

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