问题描述
我正在编写一个应用程序,该应用程序可以创建带有PDF格式答案页面的Math工作表。我想添加一个打印功能,以允许直接从应用程序中打印工作表。
在PrintDocumentAdapter中,代码的前半部分检查页面上某个像素的颜色,以确定它是否是答案页面,并相应减少要打印的页面数。然后,它构造一个printingPDF文档,并使用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,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,WriteResultCallback callback) {
Bitmap[] loadedBitmap = new Bitmap[finalTotal];
for (int pageNum = 0; pageNum < finalTotal; pageNum++) {
PdfDocument.PageInfo printingPageInfo = new PdfDocument.PageInfo.Builder(WS_WIDTH,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,Bitmap.Config.ARGB_8888);
loadedPage.render(loadedBitmap[pageNum],PdfRenderer.Page.RENDER_MODE_FOR_PRINT);
Canvas canvas = printingPage.getCanvas();
Paint paint = new Paint();
canvas.drawBitmap(loadedBitmap[pageNum],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);
}
有人可以看看我的代码并告诉我出了什么问题吗?请帮忙!谢谢!
解决方法
没关系。原来代码还可以。我需要做的是转到手机的设置,然后将打印机驱动程序应用程序的“在后台运行时显示弹出式窗口”更改为“允许”,然后运行良好!
如果有人遇到相同的问题,我将在此处保留问题和答案。