Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/372.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在pdf java中转换布局_Java_Android - Fatal编程技术网

在pdf java中转换布局

在pdf java中转换布局,java,android,Java,Android,我使用LinearLayout 代码: private LinearLayout buildBox(Integer w){ LinearLayout.LayoutParams boxParams = new LinearLayout.LayoutParams( getDpToInt(w), LinearLayout.LayoutParams.WRAP_CONTENT); Lin

我使用
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位图之后,我需要将其转换。在同一屏幕上,您将有一个按钮来打印它。我尝试了一些在网站上找到的解决方案,但没有成功。有人能帮我吗?

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

您可以尝试执行屏幕截图并使用PDF库将图像保存为PDF文件,也可以使用PDF库重新创建布局并填充当前包含的字段。iText的pdfHtml库可能非常适合您。我使用itextPdf创建了此布局。但是,我需要在这个布局中包含一个细节,这是我使用itext无法做到的,即在布局的一部分上放置一个侧边框,例如:仅在渲染内容的一半上放置边框。我无法放置此固定边框,因为列表不固定。出于这个原因,我考虑用xml绘制布局并将其转换为pdf。