convert-layout在pdf-java中的应用

voj3qocg  于 2021-07-12  发布在  Java
关注(0)|答案(1)|浏览(315)

我有以下表单,使用 LinearLayout 代码:

private LinearLayout buildBox(Integer w){
    LinearLayout.LayoutParams boxParams =
            new LinearLayout.LayoutParams(
                    getDpToInt(w),
                    LinearLayout.LayoutParams.WRAP_CONTENT);
    LinearLayout whiteBox = new LinearLayout(this.getContext());
    whiteBox.setLayoutParams(boxParams);
    whiteBox.setOrientation(LinearLayout.VERTICAL);
    whiteBox.setBackgroundResource(R.color.colorAccent);
    return whiteBox;
}

我需要转换后,它被呈现为pdf或位图的形式。在同一个屏幕上,您将有一个按钮来打印它。我尝试了一些在网站上找到的解决方案,但没有成功。有人能帮我吗?

mzmfm0qo

mzmfm0qo1#

在pdf文件上写代码按钮点击喜欢,

btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.d("size"," "+llPdf.getWidth() +"  "+llPdf.getWidth());
                bitmap = loadBitmapFromView(llPdf, llPdf.getWidth(), llPdf.getHeight());
                createPdfFile();
            }
        });

并实施这些方法,

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 createPdfFile(){
        WindowManager wm = (WindowManager) getSystemService(Context.WINDOW_SERVICE);

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

        String targetPdf = "/storage/mypdflayout.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();
        }

        document.close();
        Toast.makeText(this, "PDF is created successfully!!!", Toast.LENGTH_SHORT).show();

        openGeneratedPDFFile();

    }

    private void openGeneratedPDFFile(){
        File file = new File("/sdcard/pdffromlayout.pdf");
        if (file.exists())
        {
            Intent i=new Intent(Intent.ACTION_VIEW);
            Uri uri = Uri.fromFile(file);
            i.setDataAndType(uri, "application/pdf");
            i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

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

相关问题