下面列出了android.graphics.pdf.PdfDocument.Page#android.print.PrintDocumentAdapter 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* Print the current web site
*/
@RequiresApi(19)
public void printWebView(@Nullable PrintListener listener) {
PrintManager printManager = (PrintManager) getContext().getSystemService(Context.PRINT_SERVICE);
PrintDocumentAdapter printAdapter = createPrintDocumentAdapter();
PrintAttributes.Builder builder = new PrintAttributes.Builder();
builder.setMediaSize(PrintAttributes.MediaSize.NA_LETTER);
if (printManager != null) {
PrintJob printJob = printManager.print(getTitle(), printAdapter, builder.build());
if (listener!=null) {
if(printJob.isCompleted()){
listener.onSuccess();
} else {
listener.onFailed();
}
}
} else {
if (listener != null) {
listener.onFailed();
}
}
}
private void printNote() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
PrintManager printManager = (PrintManager) activity.getSystemService(Context.PRINT_SERVICE);
String jobName = getString(R.string.app_name) + " Document";
// GitHub issue 197
if(editor != null){
PrintDocumentAdapter printAdapter = editor.createPrintDocumentAdapter();
printManager.print(jobName, printAdapter, new PrintAttributes.Builder().build());
}else if(editText != null){
printEditText(jobName);
}else{
Toast.makeText(activity, R.string.empty_note_description, Toast.LENGTH_LONG).show();
}
}
}
/**
* Print a {@link WebView}'s contents, also allows to create a PDF
*
* @param webview WebView
* @param jobName Name of the job (affects PDF name too)
* @return {{@link PrintJob}} or null
*/
@RequiresApi(api = Build.VERSION_CODES.KITKAT)
@SuppressWarnings("deprecation")
public PrintJob print(WebView webview, String jobName) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
PrintDocumentAdapter printAdapter;
PrintManager printManager = (PrintManager) webview.getContext().getSystemService(Context.PRINT_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
printAdapter = webview.createPrintDocumentAdapter(jobName);
} else {
printAdapter = webview.createPrintDocumentAdapter();
}
if (printManager != null) {
return printManager.print(jobName, printAdapter, new PrintAttributes.Builder().build());
}
} else {
Log.e(getClass().getName(), "ERROR: Method called on too low Android API version");
}
return null;
}
private void genPdfFile() {
PrintManager printManager = (PrintManager) getSystemService(Context.PRINT_SERVICE);
// Get a print adapter instance
PrintDocumentAdapter printAdapter;
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) {
printAdapter = mWebView.createPrintDocumentAdapter("my.pdf");
} else {
printAdapter = mWebView.createPrintDocumentAdapter();
}
// Create a print job with name and adapter instance
String jobName = getString(R.string.app_name) + " Document";
PrintAttributes attributes = new PrintAttributes.Builder()
.setMediaSize(PrintAttributes.MediaSize.ISO_A4)
.setResolution(new PrintAttributes.Resolution("id", Context.PRINT_SERVICE, 200, 200))
.setColorMode(PrintAttributes.COLOR_MODE_COLOR)
.setMinMargins(PrintAttributes.Margins.NO_MARGINS)
.build();
printManager.print(jobName, printAdapter, attributes);
}
private void createWebPrintJob(WebView webView) {
// Get a PrintManager instance
PrintManager printManager = (PrintManager) getActivity()
.getSystemService(Context.PRINT_SERVICE);
// Get a print adapter instance
PrintDocumentAdapter printAdapter = webView.createPrintDocumentAdapter();
// Create a print job with name and adapter instance
String jobName = getString(R.string.app_name) + " Document";
PrintJob printJob = printManager.print(jobName, printAdapter,
new PrintAttributes.Builder().build());
}
private void print_page(WebView view, String print_name, boolean manual) {
PrintManager printManager = (PrintManager) getSystemService(Context.PRINT_SERVICE);
PrintDocumentAdapter printAdapter = view.createPrintDocumentAdapter(print_name);
PrintAttributes.Builder builder = new PrintAttributes.Builder();
builder.setMediaSize(PrintAttributes.MediaSize.ISO_A5);
PrintJob printJob = printManager.print(print_name, printAdapter, builder.build());
if(printJob.isCompleted()){
Toast.makeText(getApplicationContext(), R.string.print_complete, Toast.LENGTH_LONG).show();
}
else if(printJob.isFailed()){
Toast.makeText(getApplicationContext(), R.string.print_failed, Toast.LENGTH_LONG).show();
}
}
/**
* Print a {@link WebView}'s contents, also allows to create a PDF
*
* @param webview WebView
* @param jobName Name of the job (affects PDF name too)
* @return {{@link PrintJob}} or null
*/
@RequiresApi(api = Build.VERSION_CODES.KITKAT)
public PrintJob print(final WebView webview, final String jobName, final boolean... landscape) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
final PrintDocumentAdapter printAdapter;
final PrintManager printManager = (PrintManager) _context.getSystemService(Context.PRINT_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
printAdapter = webview.createPrintDocumentAdapter(jobName);
} else {
printAdapter = webview.createPrintDocumentAdapter();
}
final PrintAttributes.Builder attrib = new PrintAttributes.Builder();
if (landscape != null && landscape.length > 0 && landscape[0]) {
attrib.setMediaSize(new PrintAttributes.MediaSize("ISO_A4", "android", 11690, 8270));
attrib.setMinMargins(new PrintAttributes.Margins(0, 0, 0, 0));
}
if (printManager != null) {
try {
return printManager.print(jobName, printAdapter, attrib.build());
} catch (Exception ignored) {
}
}
} else {
Log.e(getClass().getName(), "ERROR: Method called on too low Android API version");
}
return null;
}
/**
* Print a {@link WebView}'s contents, also allows to create a PDF
*
* @param webview WebView
* @param jobName Name of the job (affects PDF name too)
* @return {{@link PrintJob}} or null
*/
@RequiresApi(api = Build.VERSION_CODES.KITKAT)
public PrintJob print(final WebView webview, final String jobName, final boolean... landscape) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
final PrintDocumentAdapter printAdapter;
final PrintManager printManager = (PrintManager) _context.getSystemService(Context.PRINT_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
printAdapter = webview.createPrintDocumentAdapter(jobName);
} else {
printAdapter = webview.createPrintDocumentAdapter();
}
final PrintAttributes.Builder attrib = new PrintAttributes.Builder();
if (landscape != null && landscape.length > 0 && landscape[0]) {
attrib.setMediaSize(new PrintAttributes.MediaSize("ISO_A4", "android", 11690, 8270));
attrib.setMinMargins(new PrintAttributes.Margins(0, 0, 0, 0));
}
if (printManager != null) {
try {
return printManager.print(jobName, printAdapter, attrib.build());
} catch (Exception ignored) {
}
}
} else {
Log.e(getClass().getName(), "ERROR: Method called on too low Android API version");
}
return null;
}
private void createWebPrintJob(WebView webView, String jobName) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
// Get a PrintManager instance
PrintManager printManager = (PrintManager) getActivity()
.getSystemService(Context.PRINT_SERVICE);
// Get a print adapter instance
PrintDocumentAdapter printAdapter = webView.createPrintDocumentAdapter();
// Create a print job with name and adapter instance
PrintJob printJob = printManager.print(jobName, printAdapter,
new PrintAttributes.Builder().build());
}
}
/**
* Starts a print job for the given WebView
*
* Source: https://developer.android.com/training/printing/html-docs.html
*
* @param v the WebView to be printed
*/
@TargetApi(Build.VERSION_CODES.KITKAT)
private void createWebPrintJob(WebView v) {
// Get a PrintManager instance
PrintManager printManager = (PrintManager) getSystemService(Context.PRINT_SERVICE);
// Get a print adapter instance
PrintDocumentAdapter printAdapter = new PrintDocumentAdapterWrapper(this, v.createPrintDocumentAdapter());
// Create a print job with name and adapter instance
printJob = printManager.print(jobName, printAdapter, new PrintAttributes.Builder().build());
}
@SuppressWarnings("deprecation")
@TargetApi(Build.VERSION_CODES.KITKAT)
private void createWebPrintJob(WebView webView) {
// Get a PrintManager instance
PrintManager printManager = (PrintManager) getSystemService(PRINT_SERVICE);
// Get a print adapter instance
PrintDocumentAdapter printAdapter = webView.createPrintDocumentAdapter();
// Create a print job with name and adapter instance
String jobName = getString(R.string.document, getString(R.string.app_name));
printManager.print(jobName, printAdapter,
new PrintAttributes.Builder().build());
}
/**
* @deprecated Use {@link #createPrintDocumentAdapter(String)} which requires user
* to provide a print document name.
*/
@Deprecated
public PrintDocumentAdapter createPrintDocumentAdapter() {
checkThread();
return mProvider.createPrintDocumentAdapter("default");
}
@Override
public void print(String printJobName, PrintDocumentAdapter documentAdapter,
PrintAttributes attributes) {
dumpJobStatesForDebug();
mPrintManager.print(printJobName, documentAdapter, attributes);
}
public PrintDocumentAdapterWrapper(Activity activity, PrintDocumentAdapter adapter) {
super();
this.activity = activity;
this.delegate = adapter;
}
/**
* Creates a PrintDocumentAdapter that provides the content of this WebView for printing.
*
* The adapter works by converting the WebView contents to a PDF stream. The WebView cannot
* be drawn during the conversion process - any such draws are undefined. It is recommended
* to use a dedicated off screen WebView for the printing. If necessary, an application may
* temporarily hide a visible WebView by using a custom PrintDocumentAdapter instance
* wrapped around the object returned and observing the onStart and onFinish methods. See
* {@link android.print.PrintDocumentAdapter} for more information.
*
* @param documentName The user-facing name of the printed document. See
* {@link android.print.PrintDocumentInfo}
*/
public PrintDocumentAdapter createPrintDocumentAdapter(String documentName) {
checkThread();
return mProvider.createPrintDocumentAdapter(documentName);
}
/**
* Same as {@link android.print.PrintManager#print}, except this doesn't return a
* {@link android.print.PrintJob} since the clients don't need it.
*/
void print(String printJobName,
PrintDocumentAdapter documentAdapter,
PrintAttributes attributes);
public PrintDocumentAdapter createPrintDocumentAdapter(String documentName);