gpt4 book ai didi

android - 使用 PrintDocumentAdapter 和 PrintManager 更改页数

转载 作者:行者123 更新时间:2023-12-02 16:19:31 24 4
gpt4 key购买 nike

我正在扩展 PrintDocumentAdapter 以通过云打印将名册信息发送到打印机。一切都运行良好,直到更改设置以使文档中的页数减少。

例如,一张表格适合两张 A4 页,但需要三张 A5 页。云打印开始时默认选择 A4,并生成预览。如果页面尺寸更改为 A5,则会按预期生成包含三页的新预览。如果随后将页面尺寸更改回 A4,云打印会显示消息“抱歉,这不起作用。请重试。”并且必须退出。

这似乎正在发生:

  • onLayout() 使用 A4 文档属性调用。 LayoutResultCallback.onLayoutFinished() 传递一个设置为 2 的 PrintDocumentInfo pageCount。
  • 按照预期,使用 0-1 的单个 PageRange 调用 onWrite()。
  • 我将纸张尺寸更改为 A5。
  • onLayout() 使用 A5 文档属性调用。 LayoutResultCallback.onLayoutFinished() 传递的 PrintDocumentInfo pageCount 设置为 3。
  • 使用 0-1 的单个 PageRange 调用 onWrite(),这是旧范围
  • 第二次调用 onWrite(),正确的 PageRange 为 0-2。

只有当旧的 PageRange 大于返回的页数时,才会导致错误,但为什么 onWrite() 会使用无效的 PageRange 被调用呢?

我注意到我的设备上的许多应用程序只是缩放输出,以便页数在不同设置之间不会发生变化。他们这样做是因为这个问题吗?

public class PrintAdapter extends PrintDocumentAdapter {

private static final String TAG = "PrintAdapter";

private PrintedPdfBuilder mPrintedPdfBuilder;
private int mPages;

public PrintAdapter(PrintedPdfBuilder printedPdfBuilder) {
mPrintedPdfBuilder = printedPdfBuilder;
}

@Override
public void onLayout(PrintAttributes oldAttributes, PrintAttributes newAttributes,
CancellationSignal cancellationSignal, LayoutResultCallback callback,
Bundle extras) {

Log.v(TAG, "onLayout");
// Respond to cancellation request
if (cancellationSignal.isCanceled()) {
callback.onLayoutCancelled();
return;
}

int pages = mPrintedPdfBuilder.getPageCount(newAttributes);
Log.v(TAG, "page count = " + pages);
if (pages > 0) {
// Return print information to print framework
PrintDocumentInfo info = new PrintDocumentInfo
.Builder("print_output.pdf")
.setContentType(PrintDocumentInfo.CONTENT_TYPE_DOCUMENT)
.setPageCount(pages)
.build();
// Content layout reflow is complete
callback.onLayoutFinished(info, mPages != pages);
mPages = pages;
} else {
// Otherwise report an error to the print framework
callback.onLayoutFailed("Page count calculation failed.");
}
}

@Override
public void onWrite(PageRange[] ranges, ParcelFileDescriptor destination,
CancellationSignal cancellationSignal, WriteResultCallback callback) {

Log.v(TAG, "onWrite() ranges: " + Arrays.toString(ranges));
// Write PDF document to file
try {
PrintedPdfDocument pdf = mPrintedPdfBuilder.generateDocument(ranges);

// check for cancellation
if (cancellationSignal.isCanceled()) {
callback.onWriteCancelled();
mPrintedPdfBuilder.close();
return;
}
pdf.writeTo(new FileOutputStream(destination.getFileDescriptor()));
} catch (IOException e) {
callback.onWriteFailed(e.toString());
return;
} finally {
mPrintedPdfBuilder.close();
}

PageRange[] writtenPages = mPrintedPdfBuilder.getWrittenPages();
Log.v(TAG, "writtenPages: " + Arrays.toString(writtenPages));
// Signal the print framework the document is complete
callback.onWriteFinished(writtenPages);
}
}

日志猫:

08-28 11:46:44.187 9714-9714/com.meremammal.www.staffrosterwizard V/PrintAdapter: onLayout
08-28 11:46:44.276 9714-9714/com.meremammal.www.staffrosterwizard V/PrintAdapter: page count = 2
08-28 11:46:44.298 9714-9714/com.meremammal.www.staffrosterwizard V/PrintAdapter: onWrite() ranges: [PageRange[0 - 1]]
08-28 11:46:44.512 9714-9714/com.meremammal.www.staffrosterwizard V/PrintAdapter: writtenPages: [PageRange[0 - 1]]
08-28 11:46:50.418 9714-9714/com.meremammal.www.staffrosterwizard V/PrintAdapter: onLayout
08-28 11:46:50.499 9714-9714/com.meremammal.www.staffrosterwizard V/PrintAdapter: page count = 3
08-28 11:46:50.509 9714-9714/com.meremammal.www.staffrosterwizard V/PrintAdapter: onWrite() ranges: [PageRange[0 - 1]]
08-28 11:46:50.678 9714-9714/com.meremammal.www.staffrosterwizard V/PrintAdapter: writtenPages: [PageRange[0 - 1]]
08-28 11:46:50.918 9714-9714/com.meremammal.www.staffrosterwizard V/PrintAdapter: onWrite() ranges: [PageRange[0 - 2]]
08-28 11:46:51.160 9714-9714/com.meremammal.www.staffrosterwizard V/PrintAdapter: writtenPages: [PageRange[0 - 2]]
08-28 11:47:34.983 9714-9714/com.meremammal.www.staffrosterwizard V/PrintAdapter: onLayout
08-28 11:47:35.049 9714-9714/com.meremammal.www.staffrosterwizard V/PrintAdapter: page count = 2
08-28 11:47:35.056 9714-9714/com.meremammal.www.staffrosterwizard V/PrintAdapter: onWrite() ranges: [PageRange[0 - 2]]
08-28 11:47:35.248 9714-9714/com.meremammal.www.staffrosterwizard V/PrintAdapter: writtenPages: [PageRange[0 - 1]]

每次设置更改时都会调用 onLayout,但由于某种原因,然后会使用之前的 PageRange[] 调用 onWrite

最佳答案

我捕获了 NullPointerException 错误并向用户显示一条重试消息。这将捕获错误并向用户显示您的消息以及“重试”按钮,该按钮随后会成功,因为 pageRanges 已更新。

    @Override
public void onWrite(final PageRange[] pageRanges,
final ParcelFileDescriptor destination,
final CancellationSignal cancellationSignal,
final WriteResultCallback callback) {
// Iterate over each page of the document,
// check if it's in the output range.
int requestedPageCount;
for (int i = 0; i < mTotalPages; i++) {
// Check to see if this page is in the output range.
if (containsPage(pageRanges, i)) {
//########## Add in this Try Catch Block #############
try {
//######################################################
PdfDocument.PageInfo newPage = new PdfDocument.PageInfo.Builder(mPageWidth,
mPageHeight, i).create();
PdfDocument.Page page =
mPDFDocument.startPage(newPage);
if (cancellationSignal.isCanceled()) {
callback.onWriteCancelled();
mPDFDocument.close();
mPDFDocument = null;
return;
}
if (mPrintJobType == PrintType.LIST) {
drawPageList(page, i + 1, (mPageWidth < mPageHeight));
} else {
drawPageDetail(page, i);
}
mPDFDocument.finishPage(page);
//######## Catch the error here and display a message
} catch (NullPointerException e) {
//Catch a known error thrown thrown by pageRanges not being updated in a timely manner.
callback.onWriteFailed("We found a bug in the Android operating system. " +
"Please click Retry to continue.");
return;
}
//##################################################
}
}

// Write PDF document to file
try {
mPDFDocument.writeTo(new FileOutputStream(
destination.getFileDescriptor()));
} catch (IOException e) {
callback.onWriteFailed(e.toString());
return;
} finally {
mPDFDocument.close();
mPDFDocument = null;
}
callback.onWriteFinished(pageRanges);
}

关于android - 使用 PrintDocumentAdapter 和 PrintManager 更改页数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39160122/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com