我如何在我自己的Android应用程序中显示pdf

时间:2022-06-26 07:21:44

I have Text can someone please tell me how do i convert text to pdf and how do i show it in my android application. i saw there are many libraries which do the same can some one give me the sample code for that. thanks

我有文字可以有人请告诉我如何将文本转换为PDF格式,我如何在我的Android应用程序中显示它。我看到有很多库做同样的事情,有些人可以给我示例代码。谢谢

i have used pdfviewer lib but still didnt get the pdf. hear is my code -

我使用过pdfviewer lib但仍然没有得到pdf。听到是我的代码 -

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

@Override
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(this, Second.class);
      intent.putExtra(PdfViewerActivity.EXTRA_PDFFILENAME, path);
      startActivity(intent);
    }
    catch (Exception e) 
    {
      e.printStackTrace();
    }
}

}

1 个解决方案

#1


add PDFViewer as library in your project, copy its resources in your as guided in Here. This is working code for me. I am reading pdf from assets folder. Do customization as your requirement.

在项目中添加PDFViewer作为库,在此处按照指导复制其资源。这是我的工作代码。我正在从assets文件夹中阅读pdf。根据您的要求定制。

  1. Download pdf reader project from above link.
  2. 从上面的链接下载pdf阅读器项目。

  3. copy all resources in your project.
  4. 复制项目中的所有资源。

  5. extend your activity (in which you want to display pdf) from PdfViewerActivity.
  6. 从PdfViewerActivity扩展您的活动(您要在其中显示pdf)。

  7. copy all methods from pdfreader project in yourpdfview activity.
  8. 从yourpdfview活动中复制pdfreader项目中的所有方法。

  9. Run.

Happy Coding

AssetManager assetManager = getAssets();
InputStream in = null;
OutputStream out = null;
File file = new File(getFilesDir(), "ABC.pdf");
  try {
    in = assetManager.open("ABC.pdf");
    out = openFileOutput(file.getName(), Context.MODE_WORLD_READABLE);

    copyFile(in, out);
    in.close();
    in = null;
    out.flush();
    out.close();
    out = null;
  } catch (Exception e) {
    Log.e("tag", e.getMessage());
  }

  Intent intent = new Intent(this, YourNextActivityName.class);
  intent.putExtra(PdfViewerActivity.EXTRA_PDFFILENAME, getFilesDir() + "/ABC.pdf");
  startActivity(intent);
}

private void copyFile(InputStream in, OutputStream out) throws IOException {
  byte[] buffer = new byte[1024];
  int read;
  while ((read = in.read(buffer)) != -1) {
    out.write(buffer, 0, read);
  }
}

The methods of yourpdfview activity are:

yourpdfview活动的方法是:

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

#1


add PDFViewer as library in your project, copy its resources in your as guided in Here. This is working code for me. I am reading pdf from assets folder. Do customization as your requirement.

在项目中添加PDFViewer作为库,在此处按照指导复制其资源。这是我的工作代码。我正在从assets文件夹中阅读pdf。根据您的要求定制。

  1. Download pdf reader project from above link.
  2. 从上面的链接下载pdf阅读器项目。

  3. copy all resources in your project.
  4. 复制项目中的所有资源。

  5. extend your activity (in which you want to display pdf) from PdfViewerActivity.
  6. 从PdfViewerActivity扩展您的活动(您要在其中显示pdf)。

  7. copy all methods from pdfreader project in yourpdfview activity.
  8. 从yourpdfview活动中复制pdfreader项目中的所有方法。

  9. Run.

Happy Coding

AssetManager assetManager = getAssets();
InputStream in = null;
OutputStream out = null;
File file = new File(getFilesDir(), "ABC.pdf");
  try {
    in = assetManager.open("ABC.pdf");
    out = openFileOutput(file.getName(), Context.MODE_WORLD_READABLE);

    copyFile(in, out);
    in.close();
    in = null;
    out.flush();
    out.close();
    out = null;
  } catch (Exception e) {
    Log.e("tag", e.getMessage());
  }

  Intent intent = new Intent(this, YourNextActivityName.class);
  intent.putExtra(PdfViewerActivity.EXTRA_PDFFILENAME, getFilesDir() + "/ABC.pdf");
  startActivity(intent);
}

private void copyFile(InputStream in, OutputStream out) throws IOException {
  byte[] buffer = new byte[1024];
  int read;
  while ((read = in.read(buffer)) != -1) {
    out.write(buffer, 0, read);
  }
}

The methods of yourpdfview activity are:

yourpdfview活动的方法是:

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