英文:
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.
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:
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();
}
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论