如何为整个ListView Android生成PDF文件

时间:2019-07-19 11:32:48

标签: android pdf bitmap

我已经创建了位图并为该位图创建了pdf文件。 但我需要为整个可滚动列表视图生成pdf。 下面给出的是我的代码,请给我任何建议,谢谢!! 我找不到可以将整个用户界面复制或将整个listview复制到pdf的pdf创建应用程序。如果有人可以建议在这种情况下可以做些什么,我将不胜感激。

 public class MainActivity extends AppCompatActivity {

private Button btn;
private LinearLayout llScroll;
private Bitmap bitmap;
private ListView listView;
ArrayList<String> myList;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    btn = findViewById(R.id.btn);
    llScroll = findViewById(R.id.llScroll);
    listView = (ListView)findViewById(R.id.listView);
    String[] names= new String[]{"One","Two","Three","Four","Five","Six","Seven","One","Two","Three","Four","Five","Six","Seven"};
    ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,android.R.id.text1,names);
    listView.setAdapter(arrayAdapter);
    btn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Log.d("size"," "+llScroll.getWidth() +"  "+llScroll.getWidth());
            bitmap = loadBitmapFromView(llScroll, llScroll.getWidth(), llScroll.getHeight());
            createPdf();
        }
    });

}
public static Bitmap loadBitmapFromView(View v, int width, int height) {
    Bitmap b = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
    Canvas c = new Canvas(b);
    v.draw(c);
    return b; 
}
private void createPdf(){
    DisplayMetrics displaymetrics = new DisplayMetrics();
    this.getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
    float hight = displaymetrics.heightPixels ;
    float width = displaymetrics.widthPixels ;
    int convertHighet = (int) hight, convertWidth = (int) width;
    PdfDocument document = new PdfDocument();
    PdfDocument.PageInfo pageInfo = new PdfDocument.PageInfo.Builder(convertWidth, convertHighet, 1).create();
    PdfDocument.Page page = document.startPage(pageInfo);

    Canvas canvas = page.getCanvas();

    Paint paint = new Paint();
    canvas.drawPaint(paint);

    bitmap = Bitmap.createScaledBitmap(bitmap, convertWidth, convertHighet, true);
    paint.setColor(Color.BLUE);
    canvas.drawBitmap(bitmap, 0, 0 , null);
    document.finishPage(page);
    // write the document content
    String targetPdf = "/sdcard/pdffromScroll.pdf";
    File filePath;
    filePath = new File(targetPdf);
    try {
        document.writeTo(new FileOutputStream(filePath));

    } catch (IOException e) {
        e.printStackTrace();
        Toast.makeText(this, "Something wrong: " + e.toString(), Toast.LENGTH_LONG).show();
    }
    // close the document
    document.close();
    Toast.makeText(this, "PDF of Scroll is created!!!", Toast.LENGTH_SHORT).show();

    openGeneratedPDF();

}
private void openGeneratedPDF(){
    File file = new File("/sdcard/pdffromScroll.pdf");
    if (file.exists())
    {
        Intent intent=new Intent(Intent.ACTION_VIEW);
        Uri uri = Uri.fromFile(file);
        intent.setDataAndType(uri, "application/pdf");
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

        try
        {
            startActivity(intent);
        }
        catch(ActivityNotFoundException e)
        {
            Toast.makeText(MainActivity.this, "No Application available to view pdf", Toast.LENGTH_LONG).show();
        }
    }
}}

0 个答案:

没有答案