简体   繁体   English

将 PDF 传递给打印机应用程序时,灰色字母被黑点包围

[英]When passing PDF to printer app, grey letters are surrounded with black dots

My app creates printable worksheets with PDF format.我的应用程序创建 PDF 格式的可打印工作表。 I found a little problem when printing worksheets.我在打印工作表时发现了一个小问题。 The worksheets look perfect on my phone.工作表在我的手机上看起来很完美。

在此处输入图片说明

However, when sending the PDF to the printer driver app using PrintDocumentAdapter(), the letters of the resulting images waiting for printing become surrounded with random black dots, as shown in the picture:但是,当使用 PrintDocumentAdapter() 将 PDF 发送到打印机驱动程序应用程序时,等待打印的结果图像的字母会被随机的黑点包围,如图所示: 在此处输入图片说明

How can I fix this?我怎样才能解决这个问题? Or is this an inherited limitation of the printing function that cannot be avoided?或者这是打印功能的遗传限制,无法避免?

Apparently the problem was that I used PrintedPdfDocument.writeTo(FileOutputStream()) to do the printing job, which was probably defective and resulted in the above artifact.显然问题是我使用 PrintedPdfDocument.writeTo(FileOutputStream()) 来做打印工作,这可能有缺陷并导致上述工件。

I tried another approach to do the printing as described in here: https://stackoverflow.com/a/49298355/13869422 No more problem with this approach!我尝试了另一种方法来进行打印,如下所述: https : //stackoverflow.com/a/49298355/13869422这种方法没有更多问题!

Here is my final code that works:这是我的最终有效代码:

    @Override
    public void onWrite(PageRange[] pageRanges, ParcelFileDescriptor ParcelFileDescriptor, CancellationSignal cancellationSignal, WriteResultCallback writeResultCallback) {
        InputStream inputStream = null;
        OutputStream outputStream = null;
        try {
            inputStream = getContentResolver().openInputStream(pdfUri);
            outputStream = new FileOutputStream(ParcelFileDescriptor.getFileDescriptor());
            byte[] buff = new byte[16384];
            int size;
            while ((size = inputStream.read(buff)) >= 0
                    && !cancellationSignal.isCanceled()) {
                outputStream.write(buff, 0, size);
            }
            if (cancellationSignal.isCanceled()) {
                writeResultCallback.onWriteCancelled();
            } else {
                writeResultCallback.onWriteFinished(new PageRange[]{PageRange.ALL_PAGES});
            }
        } catch (Exception e) {
            writeResultCallback.onWriteFailed(e.getMessage());
        } finally {
            try {
                inputStream.close();
                outputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM