简体   繁体   English

打开pdf始终打开文档的第一页

[英]Open pdf always open document 1st page

I have a button that opens a local pdf manual like this 我有一个按钮可以打开这样的本地pdf手册

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. 这将在已安装的Adobe Acrobat Reader中打开文档,并且我们的设置可确保始终安装Acrobat Reader。

The problem is that the manual has 50 pages. 问题是该手册有50页。 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. 如果用户要向下滚动并单击并选择用户手册按钮,则需要我们返回pdf的中间位置,这通常很好,但是我有一个特殊要求,即必须打开具有索引的第一页。

How do I open the document such that it always shows the first page of the pdf? 如何打开文档,使其始终显示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. 但它总是在用户第一次离开时第二次打开pdf。

Try to close the view on your opened file .pdf , when the user press the back button: 当用户按下“后退”按钮时,尝试关闭打开的文件.pdf的视图:

@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. 如果没有,那么第二个答案当然可以在Adobe Reader中使用,但需要更多工作。

Answer 1: Use an open parameter 答案1:使用开放参数

Adobe has published a document entitled Parameters for Opening PDF Files and there's indeed a parameter named page . Adobe已发布了一个标题为“打开PDF文件的参数 ”的文档,确实有一个名为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 答案2:介绍公开行动

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. 打开动作允许您在文档打开后立即触发动作,例如,您可以在有人打开文档时执行一些JavaScript。 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. 向PDF文档添加打开的操作需要您更改PDF文档。 You need some software that updates your PDF with this action. 您需要一些软件来通过此操作更新您的PDF。 See How to set initial view properties? 请参阅如何设置初始视图属性? for a C# example. 对于C#示例。 There are some Java examples here: iText 5 example / iText 7 example . 这里有一些Java示例: iText 5示例 / iText 7示例

Mmmh, 嗯,

I think you could use the AR command line options like: 我认为您可以使用AR命令行选项,例如:

(path to Adobe Reader) /A "page=100" "(Path To PDF file)" (Adobe Reader的路径)/ A“ page = 100”“(PDF文件的路径)”

or the AR activeX/ browser control: 或AR activeX /浏览器控件:

"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 ....." “ PDF浏览器控件可通过AxAcroPDFLib.AxAcroPDF界面获得,该界面提供了以下用于以编程方式控制PDF文档窗口的方法:●GoBackwardStack●GoForwardStack●GotoFirstPage●GotoLastPage .....”

HTH, Reinhard HTH,莱因哈德

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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