使用PrintManager Android 4.4直接打印PDF
文档说明如何通过在PDF画布上呈现自定义内容并发送由此创建的PDF文档进行打印来打印自定义内容。但是没有关于我们是否已经有PDF文档,如何将其发送打印的信息使用PrintManager Android 4.4直接打印PDF,android,pdf,printing,android-4.4-kitkat,android-print-framework,Android,Pdf,Printing,Android 4.4 Kitkat,Android Print Framework,文档说明如何通过在PDF画布上呈现自定义内容并发送由此创建的PDF文档进行打印来打印自定义内容。但是没有关于我们是否已经有PDF文档,如何将其发送打印的信息 与位图打印类似,有一些方法,如printHelper.printPDF?在onWrite()方法中使用以下代码片段即可: InputStream input = null; OutputStream output = null; try { input = new FileInputStream(new File("somefile
与位图打印类似,有一些方法,如printHelper.printPDF?在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();
}
}
这就是我所做的
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@RequiresApi(api = Build.VERSION_CODES.KITKAT)
public void clicked(View view){ //Button To start print
PrintManager printManager = (PrintManager) this.getSystemService(Context.PRINT_SERVICE);
String jobName = this.getString(R.string.app_name) + " Document";
printManager.print(jobName, pda, null);
}
PrintDocumentAdapter pda = new PrintDocumentAdapter()
{
@RequiresApi(api = Build.VERSION_CODES.KITKAT)
@Override
public void onWrite(PageRange[] pages, ParcelFileDescriptor destination, CancellationSignal cancellationSignal, WriteResultCallback callback)
{
InputStream input = null;
OutputStream output = null;
try {
input = new FileInputStream(new File("/storage/emulated/0/Download/file_name.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();
}
}
callback.onWriteFinished(new PageRange[]{PageRange.ALL_PAGES});
}
@RequiresApi(api = Build.VERSION_CODES.KITKAT)
@Override
public void onLayout(PrintAttributes oldAttributes, PrintAttributes newAttributes, CancellationSignal cancellationSignal, LayoutResultCallback callback, Bundle extras)
{
if (cancellationSignal.isCanceled())
{
callback.onLayoutCancelled();
return;
}
//int pages = computePageCount(newAttributes);
PrintDocumentInfo pdi = new PrintDocumentInfo.Builder("file_name.pdf").setContentType(PrintDocumentInfo.CONTENT_TYPE_DOCUMENT).build();
callback.onLayoutFinished(pdi, true);
}
};
}
Android清单>>>
添加权限
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
我的手机不支持sd卡,但仍然需要写入READ_EXTERNAL_STORAGE当使用上述代码时,打印按钮显示为disabledignore我之前的评论,经过对onLayout方法的一些修改,它现在可以工作了fine@Dinesh你能帮我做些什么吗method@sreeraag当然可以看看这个答案,但是点击“保存pdf”会保存0字节。