使用 PrintManager Android 4.4 直接打印 PDF

Shi*_*lpi 5 printing pdf android android-4.4-kitkat android-print-framework

http://developer.android.com/training/printing/index.html文档介绍了如何通过在 PDF 画布上呈现自定义内容并发送由此创建的 PDF 文档进行打印来打印自定义内容。但是没有关于如果我们已经有一个 PDF 文档,如何发送它进行打印的信息?

类似于位图打印,有类似printHelper.printPDF的方法吗?

小智 4

在 onWrite() 方法中使用以下代码片段应该可以做到这一点:

InputStream input = null;
OutputStream output = null;
try {
    input = new FileInputStream(new File("somefile.pdf"));
    output = new FileOutputStream(destination.getFileDescriptor());
    byte[] buf = new byte[1024];
    int bytesRead;
    while ((bytesRead = input.read(buf)) > 0) {
         output.write(buf, 0, bytesRead);
    }
} catch (Exception e) {

} finally {
    try {
        input.close();
        output.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}
Run Code Online (Sandbox Code Playgroud)

  • @sreeraag一定要看看这个答案http://stackoverflow.com/a/20719729/1394139 (2认同)