android使用代码打印pdf的完整代码
时间: 2024-05-13 21:18:49 浏览: 16
以下是Android使用代码打印PDF的完整代码:
```java
public class PrintPDFActivity extends AppCompatActivity {
private static final String TAG = "PrintPDFActivity";
private static final String FILENAME = "sample.pdf";
private PrintManager printManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_print_pdf);
// 获取PrintManager实例
printManager = (PrintManager) getSystemService(Context.PRINT_SERVICE);
// 设置打印按钮的点击事件
Button printButton = findViewById(R.id.print_button);
printButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
printDocument();
}
});
}
/**
* 打印PDF文档
*/
private void printDocument() {
// 获取打印适配器
PrintDocumentAdapter adapter = new PdfDocumentAdapter(this, getFilesDir() + "/" + FILENAME);
// 开始打印
printManager.print("Document", adapter, new PrintAttributes.Builder().build());
}
/**
* 打印适配器
*/
private class PdfDocumentAdapter extends PrintDocumentAdapter {
private Context context;
private String pdfFilePath;
private int totalPages;
public PdfDocumentAdapter(Context context, String pdfFilePath) {
this.context = context;
this.pdfFilePath = pdfFilePath;
}
@Override
public void onLayout(PrintAttributes oldAttributes, PrintAttributes newAttributes, CancellationSignal cancellationSignal, LayoutResultCallback callback, Bundle extras) {
// 获取PDF文档的总页数
try {
PdfRenderer renderer = new PdfRenderer(ParcelFileDescriptor.open(new File(pdfFilePath), ParcelFileDescriptor.MODE_READ_ONLY));
totalPages = renderer.getPageCount();
callback.onLayoutFinished(new PrintDocumentInfo.Builder("Document").setContentType(PrintDocumentInfo.CONTENT_TYPE_DOCUMENT).setPageCount(totalPages).build(), true);
} catch (IOException e) {
Log.e(TAG, "Failed to open PDF file", e);
callback.onLayoutFailed(null);
}
}
@Override
public void onWrite(PageRange[] pages, ParcelFileDescriptor destination, CancellationSignal cancellationSignal, WriteResultCallback callback) {
// 创建PDF渲染器
try {
PdfRenderer renderer = new PdfRenderer(ParcelFileDescriptor.open(new File(pdfFilePath), ParcelFileDescriptor.MODE_READ_ONLY));
// 开始写入
for (int i = 0; i < pages.length; i++) {
if (cancellationSignal.isCanceled()) {
callback.onWriteCancelled();
renderer.close();
return;
}
PdfRenderer.Page page = renderer.openPage(pages[i].getStart());
// 创建一个Bitmap对象
Bitmap bitmap = Bitmap.createBitmap(page.getWidth(), page.getHeight(), Bitmap.Config.ARGB_8888);
// 渲染PDF页面
page.render(bitmap, null, null, PdfRenderer.Page.RENDER_MODE_FOR_PRINT);
// 写入打印数据
PrintedPdfDocument.Page printedPage = new PrintedPdfDocument.Page.Builder(new Rect(0, 0, page.getWidth(), page.getHeight()), i).build();
OutputStream outputStream = new FileOutputStream(destination.getFileDescriptor());
printedPage.getCanvas().drawBitmap(bitmap, 0, 0, null);
printedPage.writeTo(outputStream);
callback.onWriteFinished(new PageRange[]{pages[i]});
outputStream.flush();
outputStream.close();
page.close();
}
// 关闭渲染器
renderer.close();
} catch (IOException e) {
Log.e(TAG, "Failed to open PDF file", e);
callback.onWriteFailed(null);
}
}
}
}
```
该代码会在应用中打印名为sample.pdf的PDF文档。请注意,此代码需要在API级别21或更高级别上运行。
相关推荐
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)