【问题标题】:Codes to print PDF not working at the last step打印 PDF 的代码在最后一步不起作用
【发布时间】:2020-08-23 08:48:43
【问题描述】:

我正在编写一个应用程序,它可以创建带有 PDF 格式答案页的数学工作表。我想添加一个打印功能以允许直接从应用程序打印工作表。

在PrintDocumentAdapter中,前半部分代码检查页面某个像素的颜色,判断是否为答题页,并相应减少要打印的页数。然后它构造一个 printPDF 文档并使用 onWrite() 中的 writeTo() 方法将其发送到打印。

在最后一部分之前一切正常。它正确读取 PDF,正确检查答案页数,并将带有问题页的正确 PDF 仅发送到打印页。如果我在打印页面中选择“另存为 PDF”,它会毫无问题地将页面保存到新的 PDF 文件中。但是,如果我尝试通过单击打印按钮使用通过 USB 连接到手机的打印机进行打印,打印页面就会消失并返回到我的应用程序的主页。 (它应该将那几页传递给打印机应用程序。)我尝试了几个不同的 android 打印机驱动程序应用程序,结果是相同的。

private void printWS() throws IOException {
    findViewById(R.id.defaultTextView).setVisibility(View.INVISIBLE);
    if (!loadedFile) {
        File file = new File(getApplicationContext().getCacheDir(), getString(R.string.defaultFileName));
        parcelFileDescriptor = ParcelFileDescriptor.open(file, ParcelFileDescriptor.MODE_READ_ONLY);
    } else {
        parcelFileDescriptor = getContentResolver().openFileDescriptor(pdfUri, "r");
    }
    // Start printing
    PrintManager printManager = (PrintManager) this.getSystemService(PRINT_SERVICE);
    String jobName = getString(R.string.app_name);
    PrintAttributes printAttributes = new PrintAttributes.Builder().setMediaSize(PrintAttributes.MediaSize.ISO_A4).build();
    printManager.print(jobName, new PrintDocumentAdapter() {


        int finalTotal;


        @Override
        public void onStart() {
            if (parcelFileDescriptor != null) {
                try {
                    pdfRenderer = new PdfRenderer(parcelFileDescriptor);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            int tempTotal = pdfRenderer.getPageCount();
            Bitmap[] tempBitmap = new Bitmap[tempTotal];
            finalTotal = tempTotal;
            for (int pageNum = 0; pageNum < tempTotal; pageNum++) {
                PdfRenderer.Page tempPage = pdfRenderer.openPage(pageNum);
                tempBitmap[pageNum] = Bitmap.createBitmap(WS_WIDTH, WS_HEIGHT, Bitmap.Config.ARGB_8888);
                tempPage.render(tempBitmap[pageNum], null, null, PdfRenderer.Page.RENDER_MODE_FOR_PRINT);
                if (tempBitmap[pageNum].getPixel(0, 0) == Color.GRAY) {
                    finalTotal--;
                }
                tempPage.close();
            }
        }


        PrintedPdfDocument printingPDF;


        @Override
        public void onLayout(PrintAttributes oldAttributes, PrintAttributes newAttributes, CancellationSignal cancellationSignal, LayoutResultCallback callback, Bundle bundle) {
            printingPDF = new PrintedPdfDocument(getApplicationContext(), newAttributes);
            if (cancellationSignal.isCanceled()) {
                callback.onLayoutCancelled();
                return;
            }
            if (finalTotal > 0) {
                PrintDocumentInfo info = new PrintDocumentInfo
                        .Builder("Worksheet")
                        .setContentType(PrintDocumentInfo.CONTENT_TYPE_DOCUMENT)
                        .setPageCount(finalTotal)
                        .build();
                callback.onLayoutFinished(info, true);
            } else {
                callback.onLayoutFailed("Page count calculation failed.");
            }
        }


        @Override
        public void onWrite(PageRange[] pageRanges, ParcelFileDescriptor destination, CancellationSignal cancellationSignal, WriteResultCallback callback) {
            Bitmap[] loadedBitmap = new Bitmap[finalTotal];
            for (int pageNum = 0; pageNum < finalTotal; pageNum++) {
                PdfDocument.PageInfo printingPageInfo = new PdfDocument.PageInfo.Builder(WS_WIDTH, WS_HEIGHT, pageNum).create();
                PdfDocument.Page printingPage = printingPDF.startPage(printingPageInfo);
                if (cancellationSignal.isCanceled()) {
                    callback.onWriteCancelled();
                    printingPDF.close();
                    printingPDF = null;
                    return;
                }
                PdfRenderer.Page loadedPage = pdfRenderer.openPage(pageNum);
                loadedBitmap[pageNum] = Bitmap.createBitmap(WS_WIDTH, WS_HEIGHT, Bitmap.Config.ARGB_8888);
                loadedPage.render(loadedBitmap[pageNum], null, null, PdfRenderer.Page.RENDER_MODE_FOR_PRINT);
                Canvas canvas = printingPage.getCanvas();
                Paint paint = new Paint();
                canvas.drawBitmap(loadedBitmap[pageNum], 0, 0, paint);
                printingPDF.finishPage(printingPage);
                loadedPage.close();
            }
            try {
                printingPDF.writeTo(new FileOutputStream(destination.getFileDescriptor()));
            } catch (IOException e) {
                callback.onWriteFailed(e.toString());
                return;
            } finally {
                printingPDF.close();
                printingPDF = null;
            }
            PageRange[] writtenPages = new PageRange[]{PageRange.ALL_PAGES};
            callback.onWriteFinished(writtenPages);
        }


        @Override
        public void onFinish() {
            try {
                pdfRenderer.close();
                parcelFileDescriptor.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }, printAttributes);
}

有人可以看看我的代码并告诉我出了什么问题吗?请帮忙!谢谢!

【问题讨论】:

    标签: java android pdf printing printdocument


    【解决方案1】:

    没关系。结果发现代码没问题。我需要做的是进入手机的设置,将打印机驱动程序应用程序的“在后台运行时显示弹出窗口”更改为“允许”,然后它就完美运行了!

    我会把问题和答案留在这里,以防有人遇到同样的问题。

    【讨论】:

      猜你喜欢
      • 2021-02-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-02-26
      • 2021-04-20
      • 2016-12-24
      相关资源
      最近更新 更多