Android Realizes System Printing Function

  • 2021-10-27 08:53:46
  • OfStack

In this paper, we share the specific codes of Android system printing for your reference. The specific contents are as follows

Step 1 Print pictures

Use the PrintHelper class, such as:


private void doPhotoPrint() {
 PrintHelper photoPrinter = new PrintHelper(getActivity());
 photoPrinter.setScaleMode(PrintHelper.SCALE_MODE_FIT);
 Bitmap bitmap = BitmapFactory.decodeResource(getResources(),
   R.drawable.droids);
 photoPrinter.printBitmap("droids.jpg - test print", bitmap);
}

This method can be called in the menu bar of the application. When the printBitmap () method is called, the printing interface of Android system
Will pop up, and the user can set 1 parameters, and then print or cancel.

2. Print custom documents

1. Connect to the PrintManager class:


private void doPrint() {
 // Get a PrintManager instance
 PrintManager printManager = (PrintManager) getActivity()
   .getSystemService(Context.PRINT_SERVICE);
 
 // Set job name, which will be displayed in the print queue
 String jobName = getActivity().getString(R.string.app_name) + " Document";
 
 // Start a print job, passing in a PrintDocumentAdapter implementation
 // to handle the generation of a print document
 printManager.print(jobName, new MyPrintDocumentAdapter(getActivity()),
   null); //
}

Note: The second parameter of the print function is the adapter class that inherits the abstract class PrintDocumentAdapter, and the third parameter is the PrintAttributes object.

It can be used to set 1 properties when printing.

2. Create a print adapter class

The print adapter interacts with the print framework of the Android system to handle the lifecycle approach to printing. The printing process mainly has the following life cycle methods:

onStart (): Called when the printing process begins; onLayout (): Called when the user changes the printing settings and causes the printing results to change, such as changing the paper size and paper direction; onWrite (): Called when the result to be printed is written to a file, this method is called 1 or more times after each onLayout () call; onFinish (): Called when the printing process ends.

Note: The key methods are onLayout () and onWrite (), which are called in the main thread by default, so if the printing process is time consuming, it should be done in the background thread.

3. Override the onLayout () method

In the onLayout () method, your adapter needs to tell the system frame the text type, the total number of pages and other information, such as:


@Override
public void onLayout(PrintAttributes oldAttributes,
      PrintAttributes newAttributes,
      CancellationSignal cancellationSignal,
      LayoutResultCallback callback,
      Bundle metadata) {
 // Create a new PdfDocument with the requested page attributes
 mPdfDocument = new PrintedPdfDocument(getActivity(), newAttributes);
 
 // Respond to cancellation request
 if (cancellationSignal.isCancelled() ) {
  callback.onLayoutCancelled();
  return;
 }
 
 // Compute the expected number of printed pages
 int pages = computePageCount(newAttributes);
 
 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, true);
 } else {
  // Otherwise report an error to the print framework
  callback.onLayoutFailed("Page count calculation failed.");
 }
}

Note: The execution of onLayout () method has three results: complete, cancel, and fail. You must indicate the execution result by calling the appropriate callback method of PrintDocumentAdapter. LayoutResultCallback class. The Boolean parameter of onLayoutFinished () method indicates whether the layout content has changed.


onLayout() The main task of the method is to calculate the number of pages that need to be printed under the new setting, such as determining the number of pages by the direction of printing: 
private int computePageCount(PrintAttributes printAttributes) {
 int itemsPerPage = 4; // default item count for portrait mode
 
 MediaSize pageSize = printAttributes.getMediaSize();
 if (!pageSize.isPortrait()) {
  // Six items per page in landscape orientation
  itemsPerPage = 6;
 }
 
 // Determine number of print items
 int printItemCount = getPrintItemCount();
 
 return (int) Math.ceil(printItemCount / itemsPerPage);
}

4. Override the onWrite () method

When the print results need to be output to a file, the system will call onWrite () method, the parameters of this method indicate the page to be printed and the file to be written. Your method implementation needs to write the contents of the page to a multi-page PDF document. When this process is completed, it needs to call onWriteFinished () method, such as:


@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.
 for (int i = 0; i < totalPages; i++) {
  // Check to see if this page is in the output range.
  if (containsPage(pageRanges, i)) {
   // If so, add it to writtenPagesArray. writtenPagesArray.size()
   // is used to compute the next output page index.
   writtenPagesArray.append(writtenPagesArray.size(), i);
   PdfDocument.Page page = mPdfDocument.startPage(i);
 
   // check for cancellation
   if (cancellationSignal.isCancelled()) {
    callback.onWriteCancelled();
    mPdfDocument.close();
    mPdfDocument = null;
    return;
   }
 
   // Draw page content for printing
   drawPage(page);
 
   // Rendering is complete, so page can be finalized.
   mPdfDocument.finishPage(page);
  }
 }
 
 // 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;
 }
 PageRange[] writtenPages = computeWrittenPages();
 // Signal the print framework the document is complete
 callback.onWriteFinished(writtenPages);
 
 ...
}

drawPage () method implementation:


private void drawPage(PdfDocument.Page page) {
 Canvas canvas = page.getCanvas();
 
 // units are in points (1/72 of an inch)
 int titleBaseLine = 72;
 int leftMargin = 54;
 
 Paint paint = new Paint();
 paint.setColor(Color.BLACK);
 paint.setTextSize(36);
 canvas.drawText("Test Title", leftMargin, titleBaseLine, paint);
 
 paint.setTextSize(11);
 canvas.drawText("Test paragraph", leftMargin, titleBaseLine + 25, paint);
 
 paint.setColor(Color.BLUE);
 canvas.drawRect(100, 100, 172, 172, paint);
}

Related articles: