gpt4 book ai didi

android - 如何使用 Android 4.4 打印框架打印 PDF

转载 作者:IT王子 更新时间:2023-10-29 00:05:58 27 4
gpt4 key购买 nike

如何使用 Android 4.4 打印框架打印已下载的​​ PDF?

我查看了开发者文档。但没有运气。任何示例都会有所帮助

最佳答案

在谷歌上花了几个小时后,我找到了解决方案。

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(){

@Override
public void onWrite(PageRange[] pages, ParcelFileDescriptor destination, CancellationSignal cancellationSignal, WriteResultCallback callback){
InputStream input = null;
OutputStream output = null;

try {

input = new FileInputStream(file to print);
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 {
input.close();
output.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}

@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("Name of file").setContentType(PrintDocumentInfo.CONTENT_TYPE_DOCUMENT).build();

callback.onLayoutFinished(pdi, true);
}
};

关于android - 如何使用 Android 4.4 打印框架打印 PDF,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20717189/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com