Java 如何在 Android 中呈现 PDF

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2883355/
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-13 14:01:28  来源:igfitidea点击:

How to render PDF in Android

javaandroidpdf

提问by Mikey

In my application I will receive a byte stream and convert it to a pdf file in the phone memory. How do I render that to a pdf? And show it on an activity?

在我的应用程序中,我将接收一个字节流并将其转换为手机内存中的 pdf 文件。我如何将其呈现为pdf?并在活动中展示它?

采纳答案by Tim Kryger

Some phones (like the Nexus One) come with a version of Quickofficepre-installed so it may be as easy as sending the appropriate Intent once you've saved the file to the SD card.

某些手机​​(例如 Nexus One)预装了Quickoffice版本,因此一旦您将文件保存到 SD 卡,就可以像发送适当的 Intent 一样简单。

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

        Button button = (Button) findViewById(R.id.OpenPdfButton);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                File file = new File("/sdcard/example.pdf");

                if (file.exists()) {
                    Uri path = Uri.fromFile(file);
                    Intent intent = new Intent(Intent.ACTION_VIEW);
                    intent.setDataAndType(path, "application/pdf");
                    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

                    try {
                        startActivity(intent);
                    } 
                    catch (ActivityNotFoundException e) {
                        Toast.makeText(OpenPdf.this, 
                            "No Application Available to View PDF", 
                            Toast.LENGTH_SHORT).show();
                    }
                }
            }
        });
    }
}

回答by Shankar Agarwal

Open pdf file in webview.

在 webview 中打开 pdf 文件

public class MyPdfViewActivity extends Activity {

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    WebView mWebView=new WebView(MyPdfViewActivity.this);
    mWebView.getSettings().setJavaScriptEnabled(true);
    mWebView.getSettings().setPluginsEnabled(true);
    mWebView.loadUrl("https://docs.google.com/gview?embedded=true&url="+LinkTo);
    setContentView(mWebView);
  }
}

回答by Zephyr

For the local pdf files, you can render them through the third party libraries. for example, use the MuPDF library, its supported file types include PDF, PNG and JPEG.

对于本地pdf文件,您可以通过第三方库进行渲染。例如,使用MuPDF 库,其支持的文件类型包括 PDF、PNG 和 JPEG。

One shortcoming of MuPDF is that it uses native library to fulfill the target, so it won't be easy to port the application on BlackBerry platform later.

MuPDF 的一个缺点是它使用本机库来实现目标,因此以后将应用程序移植到黑莓平台上并不容易。

回答by ben75

Android-Lollipop (api 21) introduce a new API : PdfRenderer

Android-Lollipop (api 21) 引入了一个新的 API:PdfRenderer

This API allows you to create a Bitmap from a page in a PDF document.

此 API 允许您从 PDF 文档中的页面创建位图。

Shortly :

不久:

  • get a seekable file descriptor from your pdf document :

    ParcelFileDescriptor fd = ParcelFileDescriptor.open(file, ParcelFileDescriptor.MODE_READ_ONLY)
    
  • create the PdfRenderer

    PdfRenderer renderer = new PdfRenderer(fd);
    
  • prepare the Bitmap

    Bitmap bitmap = Bitmap.createBitmap(WIDTH, HEIGHT, Bitmap.Config.ARGB_4444);
    
  • get the PdfRenderer.Pageto render

    PdfRenderer.Page page = renderer.openPage(pageIndex);
    
  • render the page on the prepared bitmap

    page.render(bitmap, null, null, PdfRenderer.Page.RENDER_MODE_FOR_DISPLAY);
    
  • now you can do what you want with the bitmap.

  • note that the 2 null args may allow you to clip some portion in the page and perform a transformation (using a Matrix) of the clip
  • there is another rendering mode : RENDER_MODE_FOR_PRINT. If you need this mode there are some guidelines to use it properly : here are the details.
  • 从您的 pdf 文档中获取可查找的文件描述符:

    ParcelFileDescriptor fd = ParcelFileDescriptor.open(file, ParcelFileDescriptor.MODE_READ_ONLY)
    
  • 创建 PdfRenderer

    PdfRenderer renderer = new PdfRenderer(fd);
    
  • 准备位图

    Bitmap bitmap = Bitmap.createBitmap(WIDTH, HEIGHT, Bitmap.Config.ARGB_4444);
    
  • 获取要呈现的PdfRenderer.Page

    PdfRenderer.Page page = renderer.openPage(pageIndex);
    
  • 在准备好的位图上渲染页面

    page.render(bitmap, null, null, PdfRenderer.Page.RENDER_MODE_FOR_DISPLAY);
    
  • 现在你可以用位图做你想做的事了。

  • 请注意,2 个空参数可能允许您剪辑页面中的某些部分并执行剪辑的转换(使用Matrix
  • 还有另一种渲染模式:RENDER_MODE_FOR_PRINT。如果您需要此模式,有一些正确使用它的指南:这里是详细信息

Here is a complete example on how to use this new API

这是一个关于如何使用这个新 API 的完整示例

回答by Alecs

This library is simple and works well: Android Pdf Viewerhttps://github.com/barteksc/AndroidPdfViewer

这个库很简单而且运行良好: Android Pdf Viewer https://github.com/barteksc/AndroidPdfViewer

Old Reply...

旧回复...

I think that Joan Zapata give us the better and simple solution:

https://github.com/JoanZapata/android-pdfview

I assure you that it works!

1: https://github.com/JoanZapata/android-pdfview

我认为 Joan Zapata 为我们提供了更好更简单的解决方案:

https://github.com/JoanZapata/android-pdfview

我向你保证它有效!

1https: //github.com/JoanZapata/android-pdfview

回答by Nermeen

To open a pdf from a byte array, you can use RadaeePDF, you can do the following into your activity:

要从字节数组打开 pdf,您可以使用RadaeePDF,您可以在您的活动中执行以下操作:

private PDFReader m_vPDF = null;
private Document doc = new Document();

public void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    Global.Init(this);

    m_vPDF = new PDFReader(this);
    doc.Close();

    int ret = m_doc.OpenMem(data, password);
        switch( ret )
        {
            case -1://need input password
                finish();
                break;
            case -2://unknown encryption
                finish();
                break;
            case -3://damaged or invalid format
                finish();
                break;
            case -10://access denied or invalid file path
                finish();
                break;
            case 0://succeeded, and continue
                break;
            default://unknown error
                finish();
                break;
        }

    m_vPDF.open(doc);

    setContentView( m_vPDF );
}