下面列出了android.graphics.pdf.PdfDocument.Page#android.print.PrintDocumentInfo 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Override
public void onLayout(
PrintAttributes oldAttributes,
PrintAttributes newAttributes,
CancellationSignal cancellationSignal,
LayoutResultCallback callback,
Bundle metadata
) {
mPdfDocument = new PrintedPdfDocument(activity, newAttributes);
if (cancellationSignal.isCanceled() ) {
callback.onLayoutCancelled();
return;
}
PrintDocumentInfo info = new PrintDocumentInfo
.Builder("Identity.pdf")
.setContentType(PrintDocumentInfo.CONTENT_TYPE_DOCUMENT)
.setPageCount(1)
.build();
callback.onLayoutFinished(info, true);
}
@Override
public void onLayout(
PrintAttributes oldAttributes,
PrintAttributes newAttributes,
CancellationSignal cancellationSignal,
LayoutResultCallback callback,
Bundle metadata
) {
mPdfDocument = new PrintedPdfDocument(activity, newAttributes);
if (cancellationSignal.isCanceled() ) {
callback.onLayoutCancelled();
return;
}
PrintDocumentInfo info = new PrintDocumentInfo
.Builder("RescueCode.pdf")
.setContentType(PrintDocumentInfo.CONTENT_TYPE_DOCUMENT)
.setPageCount(1)
.build();
callback.onLayoutFinished(info, true);
}
@Override
public void pageCountEstimationDone(final int maxPages) {
// This method might be called even after onFinish, e.g. as a result of a long page
// estimation operation. We make sure that such call has no effect, since the printing
// dialog has already been dismissed and relevant cleanup has already been done.
// Also, this ensures that we do not call askUserForSettingsReply twice.
if (mPrintingState == PRINTING_STATE_FINISHED) return;
if (maxPages != PrintDocumentInfo.PAGE_COUNT_UNKNOWN) {
mLastKnownMaxPages = maxPages;
}
if (mPrintingState == PRINTING_STATE_STARTED_FROM_ONLAYOUT) {
PrintDocumentInfo info = new PrintDocumentInfo.Builder(mPrintable.getTitle())
.setContentType(PrintDocumentInfo.CONTENT_TYPE_DOCUMENT)
.setPageCount(mLastKnownMaxPages)
.build();
mOnLayoutCallback.onLayoutFinished(info, mNeedNewPdf);
} else if (mPrintingState == PRINTING_STATE_STARTED_FROM_ONWRITE) {
// Chromium PDF generation is started inside onWrite, continue that.
if (mPrintingContext == null) {
mOnWriteCallback.onWriteFailed(mErrorMessage);
resetCallbacks();
return;
}
mPrintingContext.askUserForSettingsReply(true);
}
}
public PrintDocumentInfo getDocumentInfo() {
PrintDocument document = printJob.getDocument();
if (document == null) {
return null;
} else {
return document.getInfo();
}
}
@Override
public void onFinish() {
mLastKnownMaxPages = PrintDocumentInfo.PAGE_COUNT_UNKNOWN;
mPages = null;
if (mPrintingContext != null) {
if (mPrintingState != PRINTING_STATE_READY) {
// Note that we are never making an extraneous askUserForSettingsReply call.
// If we are in the middle of a PDF generation from onLayout or onWrite, it means
// the state isn't PRINTING_STATE_READY, so we enter here and make this call (no
// extra). If we complete the PDF generation successfully from onLayout or onWrite,
// we already make the state PRINTING_STATE_READY and call askUserForSettingsReply
// inside pdfWritingDone, thus not entering here. Also, if we get an extra
// AskUserForSettings call, it's handled inside {@link
// PrintingContext#pageCountEstimationDone}.
mPrintingContext.askUserForSettingsReply(false);
}
mPrintingContext.updatePrintingContextMap(mFileDescriptor, true);
mPrintingContext = null;
}
if (mContextFromScriptInitiation != null) {
mContextFromScriptInitiation.showSystemDialogDone();
mContextFromScriptInitiation = null;
}
mPrintingState = PRINTING_STATE_FINISHED;
closeFileDescriptor(mFileDescriptor);
mFileDescriptor = -1;
resetCallbacks();
// The printmanager contract is that onFinish() is always called as the last
// callback. We set busy to false here.
mIsBusy = false;
}
PrintDocument(PrintJobId printJobId, IPrintServiceClient printServiceClient,
PrintDocumentInfo info) {
mPrintJobId = printJobId;
mPrintServiceClient = printServiceClient;
mInfo = info;
}
@Override
public void onLayoutFinished(PrintDocumentInfo info, boolean changed) {
mCallback.onLayoutFinished(info, changed);
}
/**
* Gets the {@link PrintDocumentInfo} that describes this document.
*
* @return The document info.
*/
public @NonNull PrintDocumentInfo getInfo() {
PrintService.throwIfNotCalledOnMainThread();
return mInfo;
}
void onLayoutFinished(PrintDocumentInfo info, boolean changed);