当将PDF传递给打印应用程序时,灰色字母周围有黑色点。

huangapple go评论158阅读模式
英文:

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

问题

我的应用程序生成可打印的PDF格式工作表。在打印工作表时,我发现了一个小问题。这些工作表在我的手机上看起来很完美。

然而,当使用PrintDocumentAdapter()将PDF发送到打印驱动程序应用程序时,待打印的结果图像中的字母被随机的黑点环绕,如图所示:

如何解决这个问题?或者这是打印功能的固有限制,无法避免的吗?

英文:

My app creates printable worksheets with PDF format. I found a little problem when printing worksheets. The worksheets look perfect on my phone.

当将PDF传递给打印应用程序时,灰色字母周围有黑色点。

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:
当将PDF传递给打印应用程序时,灰色字母周围有黑色点。

How can I fix this? Or is this an inherited limitation of the printing function that cannot be avoided?

答案1

得分: 0

很明显问题出在我使用了PrintedPdfDocument.writeTo(FileOutputStream())来进行打印工作,这可能存在缺陷,导致了上述的问题。

我尝试了另一种方法来进行打印,如此处所述:https://stackoverflow.com/a/49298355/13869422
采用这种方法就没有问题了!

以下是我最终的可行代码:

@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();
        }
    }
}
英文:

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.

I tried another approach to do the printing as described in here: https://stackoverflow.com/a/49298355/13869422
No more problem with this approach!

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

huangapple
  • 本文由 发表于 2020年8月24日 15:41:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/63556660.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定