Android 实现PDF阅读器的代码示例

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11152956/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 06:10:29  来源:igfitidea点击:

Example of code to implement a PDF reader

androidpdf

提问by Dipak Keshariya

I want to implement a PDF reader in the application that I am doing, I have found several APIs, but none of them were open source.

我想在我正在做的应用程序中实现一个 PDF 阅读器,我找到了几个 API,但它们都不是开源的。

Does any of you guys know a good free alternative?

你们中有人知道一个好的免费替代品吗?



Slight adaptation of Dipak Keshariya's solutionmade by the OP

OP对Dipak Keshariya 的解决方案进行了轻微改编

First Class

头等舱

package android.pdf.reader;

import java.io.File;
import java.io.FilenameFilter;

import net.sf.andpdf.pdfviewer.PdfViewerActivity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class First extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        File images = Environment.getExternalStorageDirectory();
        File[] imagelist = images.listFiles(new FilenameFilter()
        {  
                public boolean accept(File dir, String name)  
                {  
                        return ((name.endsWith(".pdf")));
                }  
        }); 
        String[] pdflist = new String[imagelist.length]; 
        for(int i = 0;i<imagelist.length;i++)
        {
                pdflist[i] = imagelist[i].getName();
        }
        this.setListAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, pdflist));
    }

    protected void onListItemClick(ListView l, View v, int position, long id) 
    {
            super.onListItemClick(l, v, position, id);
            Object[] imagelist;
            String path = ((File) imagelist[(int)id]).getAbsolutePath();
            openPdfIntent(path);
    }

    private void openPdfIntent(String path) 
    {
        try
        {
          final Intent intent = new Intent(First.this, Second.class);
          intent.putExtra(PdfViewerActivity.EXTRA_PDFFILENAME, path);
          startActivity(intent);
        }
        catch (Exception e) 
        {
          e.printStackTrace();
        }
    }

}

Secod Class

第二类

package android.pdf.reader;

import net.sf.andpdf.pdfviewer.PdfViewerActivity;
import android.os.Bundle;

public class Second extends PdfViewerActivity 
{

@Override
public void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
}

public int getPreviousPageImageResource() {
    return R.drawable.left_arrow;
}

public int getNextPageImageResource() {
    return R.drawable.right_arrow;
}

public int getZoomInImageResource() {
    return R.drawable.zoom_in;
}

public int getZoomOutImageResource() {
    return R.drawable.zoom_out;
}

public int getPdfPasswordLayoutResource() {
    return R.layout.pdf_file_password;
}

public int getPdfPageNumberResource() {
    return R.layout.dialog_pagenumber;
}

public int getPdfPasswordEditField() {
    return R.id.etPassword;
}

public int getPdfPasswordOkButton() {
    return R.id.btOK;
}

public int getPdfPasswordExitButton() {
    return R.id.btExit;
}

public int getPdfPageNumberEditField() {
    return R.id.pagenum_edit;
}
}

Thanks.

谢谢。

回答by Dipak Keshariya

Use below code for that.

使用下面的代码。

First.java

首先.java

public class First extends ListActivity {

    String[] pdflist;
    File[] imagelist;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //setContentView(R.layout.main);

        File images = Environment.getExternalStorageDirectory();
        imagelist = images.listFiles(new FilenameFilter() {
            public boolean accept(File dir, String name) {
                return ((name.endsWith(".pdf")));
            }
        });
        pdflist = new String[imagelist.length];
        for (int i = 0; i < imagelist.length; i++) {
            pdflist[i] = imagelist[i].getName();
        }
        this.setListAdapter(new ArrayAdapter<String>(this,
                android.R.layout.simple_list_item_1, pdflist));
    }

    protected void onListItemClick(ListView l, View v, int position, long id) {
        super.onListItemClick(l, v, position, id);
        String path = imagelist[(int) id].getAbsolutePath();
        openPdfIntent(path);
    }

    private void openPdfIntent(String path) {
        try {
            final Intent intent = new Intent(First.this, Second.class);
            intent.putExtra(PdfViewerActivity.EXTRA_PDFFILENAME, path);
            startActivity(intent);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

Second.java

第二个.java

public class Second extends PdfViewerActivity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
    }

    public int getPreviousPageImageResource() {
        return R.drawable.left_arrow;
    }

    public int getNextPageImageResource() {
        return R.drawable.right_arrow;
    }

    public int getZoomInImageResource() {
        return R.drawable.zoom_in;
    }

    public int getZoomOutImageResource() {
        return R.drawable.zoom_out;
    }

    public int getPdfPasswordLayoutResource() {
        return R.layout.pdf_file_password;
    }

    public int getPdfPageNumberResource() {
        return R.layout.dialog_pagenumber;
    }

    public int getPdfPasswordEditField() {
        return R.id.etPassword;
    }

    public int getPdfPasswordOkButton() {
        return R.id.btOK;
    }

    public int getPdfPasswordExitButton() {
        return R.id.btExit;
    }

    public int getPdfPageNumberEditField() {
        return R.id.pagenum_edit;
    }
}

And declared both activities into your manifest file.

并将这两个活动声明到您的清单文件中。

回答by Orlymee

There is a very good post about this on SO. In particular check out the answer given by Commons.Ware, it answers your question.

在 SO 上有一篇关于此的非常好的帖子。特别是查看 Commons.Ware 给出的答案,它回答了您的问题。

Following your comments I have added the links here from the SO post I mentioned above (source for projects you could not find):

根据您的评论,我在此处添加了我上面提到的 SO 帖子中的链接(您找不到项目的来源):

So "checkout" or clone the repositories to your local file system to browse the code. As I mentioned in my comment check the licence of each library before you go any further to see if what you can and cannot do with the code.

因此,“签出”或将存储库克隆到本地文件系统以浏览代码。正如我在我的评论中提到的那样,在进一步查看您可以和不可以使用代码做什么之前,请检查每个库的许可证。

回答by Ankit Arora

This one works for me.

这个对我有用。

1) Add PdfViewer.jar into your project's build path

1) 将 PdfViewer.jar 添加到项目的构建路径中

2) Copy the following drawable resources from PdfViewer/res/drawable into YourProject/res/drawable left_arrow.png right_arrow.png zoom_in.png zoom_out.png

2)将以下drawable资源从PdfViewer/res/drawable复制到YourProject/res/drawable left_arrow.png right_arrow.png zoom_in.png zoom_out.png

3) Copy the following layout resources from PdfViewer/res/layout into YourProject/res/layout dialog_pagenumber.xml pdf_file_password.xml

3) 将以下布局资源从 PdfViewer/res/layout 复制到 YourProject/res/layout dialog_pagenumber.xml pdf_file_password.xml

4) Derive your PDF activity from net.sf.andpdf.pdfviewer.PdfViewerActivity

4) 从 net.sf.andpdf.pdfviewer.PdfViewerActivity 派生您的 PDF 活动

5) Using the default drawables and layouts:

5) 使用默认的可绘制对象和布局:

public int getPreviousPageImageResource() { return R.drawable.left_arrow; }

public int getNextPageImageResource() { return R.drawable.right_arrow; }

public int getZoomInImageResource() { return R.drawable.zoom_in; }

public int getZoomOutImageResource() { return R.drawable.zoom_out; }

public int getPdfPasswordLayoutResource() { return R.layout.pdf_file_password; }

public int getPdfPageNumberResource() { return R.layout.dialog_pagenumber; }

public int getPdfPasswordEditField() { return R.id.etPassword; }

public int getPdfPasswordOkButton() { return R.id.btOK; }

public int getPdfPasswordExitButton() { return R.id.btExit; }

public int getPdfPageNumberEditField() { return R.id.pagenum_edit; }
enter code here

6) Invoke your PdfViewActivity derived with the following code:

6) 使用以下代码调用派生的 PdfViewActivity:

Intent intent = new Intent(this, YourPdfViewerActivity.class);

intent.putExtra(PdfViewerActivity.EXTRA_PDFFILENAME, "PATH TO PDF GOES HERE");

startActivity(intent);

And you can download the source code from this link. LINK

您可以从此链接下载源代码。关联

Hope this helps :)

希望这可以帮助 :)