是否可以使用Print API在Android中排队多个打印作业?

时间:2016-02-26 10:18:55

标签: android printing printdocument

到目前为止,我能够打印存储在外部存储设备上的单个PDF文档。但我想给多个文件打印命令。 (排队打印作业)

所以第一个问题是

是否可以对所有文档进行排队打印而不显示每个文档的对话框?

如果是,怎么样?

提前致谢!

这是我到目前为止尝试过的代码。

import android.content.Context;
import android.os.Bundle;
import android.os.CancellationSignal;
import android.os.ParcelFileDescriptor;
import android.print.PageRange;
import android.print.PrintAttributes;
import android.print.PrintDocumentAdapter;
import android.print.PrintDocumentInfo;
import android.widget.Toast;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;


public class PDFPrintDocumentAdapter extends PrintDocumentAdapter {

      private Context context;
      private String filePath;
      private String fileName;

/**
 * @param context
 * @param fileName - for Print Document Info
 * @param filePath - PDF file to be printed, stored in external storage
 */
public PDFPrintDocumentAdapter(Context context, String fileName, String filePath) {

    this.context = context;
    this.fileName = fileName;
    this.filePath = filePath;

}

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

    if (cancellationSignal.isCanceled()) {
        callback.onLayoutCancelled();
        return;
    }

    PrintDocumentInfo pdi = new PrintDocumentInfo.Builder(fileName).setContentType(PrintDocumentInfo.CONTENT_TYPE_DOCUMENT).build();

    callback.onLayoutFinished(pdi, true);
}

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

    InputStream input = null;
    OutputStream output = null;

    try {

        input = new FileInputStream(filePath);
        output = new FileOutputStream(destination.getFileDescriptor());

        byte[] buf = new byte[1024];
        int bytesRead;

        while ((bytesRead = input.read(buf)) > 0) {
            output.write(buf, 0, bytesRead);
        }

        callback.onWriteFinished(new PageRange[]{PageRange.ALL_PAGES});

    } catch (FileNotFoundException ee) {
        //Catch exception
    } catch (Exception e) {
        //Catch exception
    } finally {
        try {
            if (null != input) {
                input.close();
            }
            if (null != output) {
                output.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}


@Override
public void onFinish() {
    super.onFinish();

    Toast.makeText(context, "Printing is completed", Toast.LENGTH_SHORT).show();
}



}

0 个答案:

没有答案