2

我在android中有一个打印机应用程序。主要活动包含一个网页。我通过 webview.capturePicture() 获取完整的 webview。然后我将它添加到pdf中。我用 itext 来做。我将它传递给谷歌云打印进行打印。我可以打印出来。

这是我的主要活动:

pv.btnPrint.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            Log.e("VIEWWWWWWWWWWWWWWWWWWWWW", "kaj korce..............");

            runOnUiThread(new Runnable() {
                public void run() {
                    // Show and set focus.
                    pv.btnPrint.setVisibility(View.INVISIBLE);
                    //String mPath = Environment
                            //.getExternalStorageDirectory().toString()
                        //  + "/"
                            //+ "dse.jpg";
                    /*
                     * Bitmap bitmap; View v1 = pv.wev.getRootView();
                     * v1.setDrawingCacheEnabled(true);
                     * 
                     * bitmap = Bitmap.createBitmap(v1.getDrawingCache());
                     * v1.setDrawingCacheEnabled(false); OutputStream fout =
                     * null; File imageFile = new File(mPath); try { fout =
                     * new FileOutputStream(imageFile);
                     * bitmap.compress(Bitmap.CompressFormat.JPEG, 40,
                     * fout); fout.flush(); fout.close();
                     * 
                     * } catch (FileNotFoundException e) { // TODO
                     * Auto-generated catch block e.printStackTrace(); }
                     * catch (IOException e) { // TODO Auto-generated catch
                     * block e.printStackTrace(); }
                     */

                    Picture picture = pv.wev.capturePicture();

                    Bitmap b = Bitmap.createBitmap(picture.getWidth(),
                            picture.getHeight(), Bitmap.Config.ARGB_8888);
                    
                    Canvas c = new Canvas(b);
                    picture.draw(c);
        
                    ByteArrayOutputStream out = null;
                    byte[] byteArchadeImage = null;
                    try {
                        out = new ByteArrayOutputStream();
                        
                        if (out != null) {
                            b.compress(Bitmap.CompressFormat.JPEG, 100, out);
                            byteArchadeImage = out.toByteArray();
                            out.flush();
                            out.close();
                        }
                    } catch (Exception e) {
                        Log.e("MSG", e.getLocalizedMessage());
                    }

                    Document document = new Document();
                    try {
                        PdfWriter.getInstance(document,
                                new FileOutputStream(Environment
                                        .getExternalStorageDirectory()
                                        .toString()
                                        + "/" + "dse.pdf"));
                        document.open();
                        //document.add(new Paragraph("This Is For You!!!!"));
                        try {
                            
                            Image image = Image.getInstance(byteArchadeImage);
                            
                            image.scaleAbsolute(600,
                            800);
                            image.setAlignment(Image.MIDDLE |Image.ALIGN_MIDDLE);
                            document.add(image);
                        } catch (MalformedURLException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        } catch (IOException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
                    } catch (FileNotFoundException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    } catch (DocumentException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                    document.close();
                    Intent printIntent = new Intent(
                            VividViewerActivity.this,
                            PrintDialogActivity.class);
                    Uri uri = Uri.fromFile(new File(Environment
                            .getExternalStorageDirectory().toString()
                            + "/"
                            + "dse.pdf"));
                    printIntent.setDataAndType(uri, "application/pdf");
                    // printIntent.putExtra(Intent.EXTRA_STREAM,
                    // Uri.fromFile(new
                    // File(ur)));
                    String time = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
                    printIntent.putExtra("title", "vividworks_report_"+time+".pdf");
                    startActivity(printIntent);
                }
            });

这是来自 Google 云打印的打印副本: 来自云打印的图像

现在我的问题是:

  1. 是否可以在一个页面中打印此图像的上侧并在另一页中打印下侧?

帮助我,因为这对我很重要。

4

0 回答 0